package db import ( "context" "encoding/json" "os" "path/filepath" "testing" "ymhut-box/server/unified-management/internal/config" ) func TestOpenImportsJSONPrototypeIntoSQLite(t *testing.T) { root := t.TempDir() path := filepath.Join(root, "unified.sqlite") prototype := state{ Admins: []adminRow{{ ID: 1, Username: "admin", PasswordHash: passwordHash("admin"), PasswordChanged: false, CreatedAt: "2026-01-01T00:00:00Z", UpdatedAt: "2026-01-01T00:00:00Z", }}, Feedbacks: []Feedback{{Code: "FB-20260101-ABCDEF", Title: "Imported", Type: "issue", Severity: "normal", Body: "hello"}}, Sources: []Source{{CategoryID: "ip", CategoryName: "IP", SourceID: "ip-demo", Name: "IP Demo", APIURL: "https://example.com/ip", Enabled: true, ClientVisible: true}}, } data, err := json.Marshal(prototype) if err != nil { t.Fatal(err) } if err := os.WriteFile(path, data, 0o640); err != nil { t.Fatal(err) } store, err := Open(&config.Config{ StorageDir: root, Database: config.DatabaseConfig{ Provider: "sqlite", SQLitePath: path, FailoverEnabled: true, HealthIntervalSec: 3600, MaxOpenConns: 1, MaxIdleConns: 1, ConnMaxLifetimeSeconds: 60, }, UploadGuard: config.UploadGuardConfig{MaxZipFiles: 80, MaxDecompressedBytes: 30 << 20, MaxSingleFileBytes: 8 << 20, MaxCompressionRatio: 120, MaxReadableTextBytes: 256 << 10, AllowUnexpectedZipFiles: true}, }) if err != nil { t.Fatal(err) } defer store.Close() if _, _, err := store.VerifyAdminPassword(context.Background(), "admin", "admin"); err != nil { t.Fatal(err) } if _, err := store.GetFeedback("FB-20260101-ABCDEF"); err != nil { t.Fatal(err) } if count, err := store.CountSources(); err != nil || count != 1 { t.Fatalf("CountSources = %d, %v", count, err) } matches, _ := filepath.Glob(path + ".json-prototype-*.bak") if len(matches) != 1 { t.Fatalf("expected prototype backup, got %v", matches) } }