package store

import (
	"context"
	"path/filepath"
	"testing"
)

func TestMigrationVersion(t *testing.T) {
	dir := t.TempDir()
	db, err := Open(filepath.Join(dir, "test.db"))
	if err != nil {
		t.Fatalf("open db: %v", err)
	}
	defer db.Close()

	ctx := context.Background()
	version, err := MigrationVersion(ctx, db)
	if err != nil {
		t.Fatalf("migration version: %v", err)
	}
	if version != "0008_audit_tamper_evidence.sql" {
		t.Errorf("MigrationVersion = %q, want 0008_audit_tamper_evidence.sql", version)
	}

	// Empty the migrations table → should return ("", nil).
	if _, err := db.ExecContext(ctx, "DELETE FROM schema_migrations"); err != nil {
		t.Fatalf("clear migrations: %v", err)
	}
	version, err = MigrationVersion(ctx, db)
	if err != nil {
		t.Fatalf("migration version after clear: %v", err)
	}
	if version != "" {
		t.Errorf("MigrationVersion after clear = %q, want empty", version)
	}
}
