167 lines
6.8 KiB
Go
167 lines
6.8 KiB
Go
package artifacts
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"os"
|
|
"path/filepath"
|
|
"reflect"
|
|
"testing"
|
|
"time"
|
|
|
|
"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)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestSuppliedProductsResolveAfterReload(t *testing.T) {
|
|
paths := SessionPaths{Root: t.TempDir()}
|
|
m := manifest.New("session", time.Now())
|
|
m.SuppliedProducts = &manifest.SuppliedProductCollection{Version: manifest.SuppliedProductVersion, Products: map[string]manifest.SuppliedProduct{}}
|
|
add := func(source, path, content string) {
|
|
absolute := filepath.Join(paths.Root, filepath.FromSlash(path))
|
|
if err := os.MkdirAll(filepath.Dir(absolute), 0755); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := os.WriteFile(absolute, []byte(content), 0600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
checksum, err := SHA256File(absolute)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
m.SuppliedProducts.Products[source] = manifest.SuppliedProduct{Path: path, Size: int64(len(content)), Checksum: checksum}
|
|
}
|
|
add(ArtifactTranscriptFinalTrimmed, TranscriptPathFinalTrimmed, `{"segments":[]}`)
|
|
add(ArtifactBoundsSession, "artifacts/session_bounds.json", `{}`)
|
|
add(ConfiguredArtifactSourceID("summary"), "artifacts/summary.md", "summary")
|
|
add(ExtractionArtifactSourceID("events"), "artifacts/events.json", `{"events":[]}`)
|
|
store := &manifest.LocalStore{}
|
|
manifestPath := filepath.Join(paths.Root, "manifest.json")
|
|
if err := store.Save(context.Background(), manifestPath, m); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
loaded, err := store.Load(context.Background(), manifestPath)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
for _, source := range []string{ArtifactTranscriptFinalTrimmed, ArtifactBoundsSession} {
|
|
resolved, err := ResolveSessionArtifact(paths, loaded, source)
|
|
if err != nil || resolved.Provenance != ArtifactProvenanceSupplied {
|
|
t.Fatalf("resolve %s: %+v %v", source, resolved, err)
|
|
}
|
|
}
|
|
if _, err := ResolveSessionArtifact(paths, loaded, ArtifactTranscriptFinal); !errors.Is(err, ErrSessionArtifactNotFound) {
|
|
t.Fatalf("absent intermediate: %v", err)
|
|
}
|
|
configured := map[string]ConfiguredArtifactDefinition{"summary": {Enabled: true, OutputPath: "artifacts/summary.md"}}
|
|
extraction := map[string]ExtractionArtifactDefinition{"events": {Filename: "events.json"}, "npcs": {Filename: "npcs.json"}}
|
|
catalog, err := BootstrapRuntimeCatalog(configured, EffectiveArtifactSet{}, extraction)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
catalog.HydrateAnalyzeArtifacts(paths, loaded, configured)
|
|
catalog.HydrateExtractionArtifacts(paths, loaded, extraction)
|
|
for _, source := range []string{ConfiguredArtifactSourceID("summary"), ExtractionArtifactSourceID("events")} {
|
|
resolved, err := ResolveSessionArtifactWithCatalog(paths, loaded, source, catalog)
|
|
if err != nil || resolved.Provenance != ArtifactProvenanceSupplied {
|
|
t.Fatalf("resolve %s: %+v %v", source, resolved, err)
|
|
}
|
|
}
|
|
if SuppliedExtractionComplete(paths, loaded, extraction) {
|
|
t.Fatal("partial extraction reported complete")
|
|
}
|
|
if _, err := ResolveSessionArtifactWithCatalog(paths, loaded, ExtractionArtifactSourceID("npcs"), catalog); !errors.Is(err, ErrSessionArtifactNotFound) {
|
|
t.Fatalf("missing lane: %v", err)
|
|
}
|
|
add(ExtractionArtifactSourceID("npcs"), "artifacts/npcs.json", `{}`)
|
|
loaded.SuppliedProducts = manifest.CloneSuppliedProducts(m.SuppliedProducts)
|
|
if !SuppliedExtractionComplete(paths, loaded, extraction) {
|
|
t.Fatal("complete supplied extraction not recognized")
|
|
}
|
|
}
|