package artifacts import ( "os" "path/filepath" "reflect" "testing" "gitea.maximumdirect.net/eric/narratio/internal/manifest" ) func TestSuppliedTranscriptCoverage(t *testing.T) { cases := []struct { source string want map[string]bool }{ {ArtifactTranscriptBase, map[string]bool{"transcribe": true, "merge": true}}, {ArtifactTranscriptPolished, map[string]bool{"transcribe": true, "merge": true, "polish": true}}, {ArtifactTranscriptFinal, map[string]bool{"transcribe": true, "merge": true, "polish": true, "normalize": true}}, {ArtifactTranscriptFinalTrimmed, map[string]bool{"transcribe": true, "merge": true, "polish": true, "normalize": true, "trim": true}}, {ArtifactTranscriptFinalMarkdown, map[string]bool{}}, {ArtifactTranscriptFinalTrimmedMarkdown, map[string]bool{}}, {ArtifactBoundsSession, map[string]bool{}}, } for _, tc := range cases { t.Run(tc.source, func(t *testing.T) { paths := SessionPaths{Root: t.TempDir()} spec := artifactRegistry[tc.source] path := filepath.Join(paths.Root, filepath.FromSlash(spec.CanonicalRelPath)) if err := os.MkdirAll(filepath.Dir(path), 0755); err != nil { t.Fatal(err) } content := []byte(`{"segments":[]}`) if spec.ContentKind == contentText { content = []byte("transcript") } if err := os.WriteFile(path, content, 0600); err != nil { t.Fatal(err) } checksum, err := SHA256File(path) if err != nil { t.Fatal(err) } products := &manifest.SuppliedProductCollection{Version: manifest.SuppliedProductVersion, Products: map[string]manifest.SuppliedProduct{tc.source: {Path: spec.CanonicalRelPath, Checksum: checksum, Size: int64(len(content))}}} if got := SuppliedTranscriptCoverage(paths, products); !reflect.DeepEqual(got, tc.want) { t.Fatalf("coverage %v, want %v", got, tc.want) } if err := ValidateSuppliedProduct(paths, tc.source, "", products.Products[tc.source]); err != nil { t.Fatal(err) } if err := os.WriteFile(path, []byte("tampered"), 0600); err != nil { t.Fatal(err) } if got := SuppliedTranscriptCoverage(paths, products); len(got) != 0 { t.Fatalf("tampered coverage: %v", got) } if err := os.Remove(path); err != nil { t.Fatal(err) } if err := ValidateSuppliedProduct(paths, tc.source, "", products.Products[tc.source]); err == nil { t.Fatal("missing product accepted") } }) } } func TestSuppliedIndependentProducts(t *testing.T) { paths := SessionPaths{Root: t.TempDir()} for _, tc := range []struct{ source, path, content string }{ {ConfiguredArtifactSourceID("summary"), "artifacts/summary.md", "summary"}, {ExtractionArtifactSourceID("events"), "artifacts/events.json", `{"events":[]}`}, } { path := filepath.Join(paths.Root, filepath.FromSlash(tc.path)) if err := os.MkdirAll(filepath.Dir(path), 0755); err != nil { t.Fatal(err) } if err := os.WriteFile(path, []byte(tc.content), 0600); err != nil { t.Fatal(err) } checksum, err := SHA256File(path) if err != nil { t.Fatal(err) } record := manifest.SuppliedProduct{Path: tc.path, Checksum: checksum, Size: int64(len(tc.content))} if err := ValidateSuppliedProduct(paths, tc.source, tc.path, record); err != nil { t.Fatal(err) } if err := ValidateSuppliedProduct(paths, tc.source, "artifacts/other", record); err == nil { t.Fatal("accepted noncanonical product") } products := &manifest.SuppliedProductCollection{Version: manifest.SuppliedProductVersion, Products: map[string]manifest.SuppliedProduct{tc.source: record}} if got := SuppliedTranscriptCoverage(paths, products); len(got) != 0 { t.Fatalf("independent product coverage: %v", got) } } }