/*
 * Copyright 2026 Oleg Borodin <onborodin@gmail.com>
 */
package maindb

import (
	"context"
	"testing"
	"time"

	"mstore/pkg/auxtool"
	"mstore/pkg/auxuuid"
	"mstore/pkg/descr"

	"github.com/stretchr/testify/require"
)

func TestFile(t *testing.T) {
	var err error

	dbDir := t.TempDir()
	db := NewDatabase(dbDir)

	err = db.OpenDatabase()
	require.NoError(t, err)

	err = db.InitDatabase()
	require.NoError(t, err)

	id := auxuuid.NewUUID()
	timenow := auxtool.TimeNow()
	creator := auxuuid.NewUUID()
	collection := "foo"
	newFile := &descr.File{
		ID:         id,
		Collection: "foo",
		Name:       "bare",
		Type:       "application/octet-stream",
		CreatedAt:  timenow,
		UpdatedAt:  timenow,
		CreatedBy:  creator,
		UpdatedBy:  creator,
	}
	ctx, _ := context.WithTimeout(context.Background(), 1*time.Second)
	err = db.InsertFile(ctx, newFile)
	require.NoError(t, err)

	files, err := db.ListFilesByCollection(ctx, collection)
	require.NoError(t, err)
	require.Equal(t, len(files), 1)
	require.Equal(t, files[0].ID, id)
	require.Equal(t, files[0].Type, "application/octet-stream")
	require.Equal(t, files[0].Name, "bare")
	require.Equal(t, files[0].Collection, "foo")
}
