273 lines
12 KiB
Go
273 lines
12 KiB
Go
|
|
package app
|
||
|
|
|
||
|
|
import (
|
||
|
|
"context"
|
||
|
|
"os"
|
||
|
|
"path/filepath"
|
||
|
|
"strings"
|
||
|
|
"testing"
|
||
|
|
"time"
|
||
|
|
|
||
|
|
"gitea.maximumdirect.net/eric/narratio/internal/adapters/scriptorium"
|
||
|
|
"gitea.maximumdirect.net/eric/narratio/internal/adapters/storage"
|
||
|
|
"gitea.maximumdirect.net/eric/narratio/internal/artifacts"
|
||
|
|
"gitea.maximumdirect.net/eric/narratio/internal/config"
|
||
|
|
"gitea.maximumdirect.net/eric/narratio/internal/manifest"
|
||
|
|
"gitea.maximumdirect.net/eric/narratio/internal/stage"
|
||
|
|
)
|
||
|
|
|
||
|
|
func TestLockedProductsRemainUsableAfterImportAndRepublish(t *testing.T) {
|
||
|
|
ctx := context.Background()
|
||
|
|
producerRoot := t.TempDir()
|
||
|
|
pipeline, campaign, session := writeValidConfigFilesWithScriptoriumArtifacts(t, producerRoot)
|
||
|
|
cfg, err := config.LoadWithSessionOptions(pipeline, campaign, session, config.SessionLoadOptions{})
|
||
|
|
if err != nil {
|
||
|
|
t.Fatal(err)
|
||
|
|
}
|
||
|
|
on := true
|
||
|
|
cfg.Pipeline.Publish.Enabled, cfg.Pipeline.Publish.UploadRun = &on, &on
|
||
|
|
rules := []config.PublishOutputRule{
|
||
|
|
{Source: artifacts.ArtifactTranscriptFinalTrimmed, Dest: "review/transcript.json", Required: &on},
|
||
|
|
{Source: artifacts.ConfiguredArtifactSourceID("session_recap"), Dest: "review/recap.md", Required: &on},
|
||
|
|
}
|
||
|
|
cfg.Pipeline.Publish.Outputs = rules
|
||
|
|
cfg.ArtifactCatalog.PublishOutputs = rules
|
||
|
|
paths := artifacts.NewLocalStore(producerRoot).SessionPathsFor(cfg.Session.Campaign, cfg.Session.SessionID)
|
||
|
|
transcript := filepath.Join(paths.TranscriptsDir, "final.trimmed.json")
|
||
|
|
recap := filepath.Join(paths.ArtifactsDir, "session_recap.md")
|
||
|
|
aTranscript, aRecap := `{"segments":[]}`, "reviewed recap\n"
|
||
|
|
mustWriteTestFile(t, transcript, aTranscript)
|
||
|
|
mustWriteTestFile(t, recap, aRecap)
|
||
|
|
m := manifest.New(cfg.Session.SessionID, time.Now().UTC())
|
||
|
|
m.Campaign = cfg.Session.Campaign
|
||
|
|
m.RunID = "20260519T010203Z-a1b2c3d4"
|
||
|
|
m.LocalWorkDir = artifacts.SessionRunRootForCampaign(producerRoot, m.Campaign, m.SessionID, m.RunID)
|
||
|
|
if err := os.MkdirAll(m.LocalWorkDir, 0755); err != nil {
|
||
|
|
t.Fatal(err)
|
||
|
|
}
|
||
|
|
for _, name := range []string{"prepare", "transcribe", "merge", "polish", "normalize", "trim", "render", "analyze"} {
|
||
|
|
m.MarkStageSucceeded(name, time.Now().UTC(), nil)
|
||
|
|
}
|
||
|
|
setCurrentBuiltInEvidence(t, m, "trim", artifacts.TranscriptOutputKindFinalTrimmed, artifacts.ArtifactTranscriptFinalTrimmed, transcript)
|
||
|
|
setAppAnalyzeEvidence(m, "session_recap", "artifacts/session_recap.md", []byte(aRecap))
|
||
|
|
fake := &storage.FakeBackend{}
|
||
|
|
env := &stage.Env{Config: cfg, ArtifactStore: artifacts.NewLocalStore(producerRoot), ObjectStore: fake}
|
||
|
|
publish, err := stage.Select("publish")
|
||
|
|
if err != nil {
|
||
|
|
t.Fatal(err)
|
||
|
|
}
|
||
|
|
if _, err := publish.Run(ctx, env, m); err != nil {
|
||
|
|
t.Fatal(err)
|
||
|
|
}
|
||
|
|
prefix, err := artifacts.ResolvePublishSessionPrefix(cfg, m)
|
||
|
|
if err != nil {
|
||
|
|
t.Fatal(err)
|
||
|
|
}
|
||
|
|
for _, rule := range rules {
|
||
|
|
lock, err := artifacts.NewRemoteLock(rule.Source, "reviewed", time.Now().UTC())
|
||
|
|
if err != nil {
|
||
|
|
t.Fatal(err)
|
||
|
|
}
|
||
|
|
if err := artifacts.AddRemoteLock(ctx, fake, prefix, lock, false); err != nil {
|
||
|
|
t.Fatal(err)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
m.RunID = "20260520T010203Z-a1b2c3d4"
|
||
|
|
m.LocalWorkDir = artifacts.SessionRunRootForCampaign(producerRoot, m.Campaign, m.SessionID, m.RunID)
|
||
|
|
if err := os.MkdirAll(m.LocalWorkDir, 0755); err != nil {
|
||
|
|
t.Fatal(err)
|
||
|
|
}
|
||
|
|
normalized := filepath.Join(paths.TranscriptsDir, "final.json")
|
||
|
|
mustWriteTestFile(t, normalized, `{"segments":[{"text":"new"}]}`)
|
||
|
|
setCurrentBuiltInEvidence(t, m, "normalize", artifacts.TranscriptOutputKindFinal, artifacts.ArtifactTranscriptFinal, normalized)
|
||
|
|
off := false
|
||
|
|
cfg.Pipeline.Trim.Enabled = &off
|
||
|
|
trim, err := stage.Select("trim")
|
||
|
|
if err != nil {
|
||
|
|
t.Fatal(err)
|
||
|
|
}
|
||
|
|
trimmed, err := trim.Run(ctx, env, m)
|
||
|
|
if err != nil {
|
||
|
|
t.Fatal(err)
|
||
|
|
}
|
||
|
|
m.MarkStageSucceeded("trim", time.Now().UTC(), mapResultOutputs("trim", trimmed, m.RunID))
|
||
|
|
env.Scriptorium = &scriptorium.FakeRunner{}
|
||
|
|
env.ManifestStore = &manifest.LocalStore{}
|
||
|
|
env.SelectedArtifactKeys = []string{"session_recap"}
|
||
|
|
env.Force = true
|
||
|
|
analyze, err := stage.Select("analyze")
|
||
|
|
if err != nil {
|
||
|
|
t.Fatal(err)
|
||
|
|
}
|
||
|
|
generated, err := analyze.Run(ctx, env, m)
|
||
|
|
if err != nil {
|
||
|
|
t.Fatal(err)
|
||
|
|
}
|
||
|
|
m.MarkStageSucceeded("analyze", time.Now().UTC(), mapResultOutputs("analyze", generated, m.RunID))
|
||
|
|
if data, err := os.ReadFile(recap); err != nil || string(data) == aRecap {
|
||
|
|
t.Fatalf("new local recap was not generated: %q, %v", data, err)
|
||
|
|
}
|
||
|
|
env.Force = false
|
||
|
|
env.SelectedArtifactKeys = nil
|
||
|
|
if _, err := publish.Run(ctx, env, m); err != nil {
|
||
|
|
t.Fatal(err)
|
||
|
|
}
|
||
|
|
selected, err := artifacts.LoadCurrentInventory(ctx, fake, prefix, artifacts.CurrentStateValidation{})
|
||
|
|
if err != nil {
|
||
|
|
t.Fatal(err)
|
||
|
|
}
|
||
|
|
carried := 0
|
||
|
|
for _, declaration := range selected.Commit.Artifacts {
|
||
|
|
if declaration.Type == artifacts.RemoteArtifactTypeSessionManifest {
|
||
|
|
t.Fatal("publication included an execution snapshot")
|
||
|
|
}
|
||
|
|
if declaration.Type == artifacts.RemoteArtifactTypePublishedOutput && !strings.Contains(declaration.DestinationKey, "20260519T010203Z-a1b2c3d4") {
|
||
|
|
t.Fatalf("locked product was not carried from its original publication: %#v", declaration)
|
||
|
|
}
|
||
|
|
if declaration.Type == artifacts.RemoteArtifactTypePublishedOutput {
|
||
|
|
carried++
|
||
|
|
}
|
||
|
|
}
|
||
|
|
if carried != len(rules) {
|
||
|
|
t.Fatalf("carried %d products, want %d", carried, len(rules))
|
||
|
|
}
|
||
|
|
cfg.Pipeline.Workspace.Root = t.TempDir()
|
||
|
|
cfg.Session.Inputs.AudioDir = ""
|
||
|
|
deps := normalizeCommandDependencies(restoreCommandDependencies(fake))
|
||
|
|
if _, err := restoreSessionState(ctx, cfg, RestorePlanOptions{}, deps); err != nil {
|
||
|
|
t.Fatal(err)
|
||
|
|
}
|
||
|
|
importedPaths := artifacts.NewLocalStore(cfg.Pipeline.Workspace.Root).SessionPathsFor(cfg.Session.Campaign, cfg.Session.SessionID)
|
||
|
|
mustReadEquals(t, filepath.Join(importedPaths.TranscriptsDir, "final.trimmed.json"), aTranscript)
|
||
|
|
mustReadEquals(t, filepath.Join(importedPaths.ArtifactsDir, "session_recap.md"), aRecap)
|
||
|
|
imported, err := (&manifest.LocalStore{}).Load(ctx, importedPaths.ManifestPath)
|
||
|
|
if err != nil {
|
||
|
|
t.Fatal(err)
|
||
|
|
}
|
||
|
|
if !artifacts.SuppliedTranscriptCoverage(importedPaths, imported.SuppliedProducts)["trim"] {
|
||
|
|
t.Fatal("carried transcript is not available after import")
|
||
|
|
}
|
||
|
|
catalog := artifacts.NewArtifactCatalog()
|
||
|
|
definitions := artifacts.ConfiguredArtifactDefinitions(cfg.ConcreteArtifacts())
|
||
|
|
if err := catalog.RegisterConfiguredArtifacts(definitions); err != nil {
|
||
|
|
t.Fatal(err)
|
||
|
|
}
|
||
|
|
catalog.HydrateAnalyzeArtifacts(importedPaths, imported, definitions)
|
||
|
|
if _, err := artifacts.ResolveSessionArtifactWithCatalog(importedPaths, imported, artifacts.ConfiguredArtifactSourceID("session_recap"), catalog); err != nil {
|
||
|
|
t.Fatalf("carried configured product unavailable: %v", err)
|
||
|
|
}
|
||
|
|
for _, rule := range rules {
|
||
|
|
if err := artifacts.RemoveRemoteLock(ctx, fake, prefix, rule.Source); err != nil {
|
||
|
|
t.Fatal(err)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
imported.RunID = "20260521T010203Z-a1b2c3d4"
|
||
|
|
imported.LocalWorkDir = artifacts.SessionRunRootForCampaign(cfg.Pipeline.Workspace.Root, imported.Campaign, imported.SessionID, imported.RunID)
|
||
|
|
if err := os.MkdirAll(imported.LocalWorkDir, 0755); err != nil {
|
||
|
|
t.Fatal(err)
|
||
|
|
}
|
||
|
|
env.ArtifactStore = artifacts.NewLocalStore(cfg.Pipeline.Workspace.Root)
|
||
|
|
if _, err := publish.Run(ctx, env, imported); err != nil {
|
||
|
|
t.Fatalf("republish imported products: %v", err)
|
||
|
|
}
|
||
|
|
if err := os.WriteFile(filepath.Join(importedPaths.ArtifactsDir, "session_recap.md"), []byte("tampered\n"), 0600); err != nil {
|
||
|
|
t.Fatal(err)
|
||
|
|
}
|
||
|
|
imported.RunID = "20260522T010203Z-a1b2c3d4"
|
||
|
|
imported.LocalWorkDir = artifacts.SessionRunRootForCampaign(cfg.Pipeline.Workspace.Root, imported.Campaign, imported.SessionID, imported.RunID)
|
||
|
|
if err := os.MkdirAll(imported.LocalWorkDir, 0755); err != nil {
|
||
|
|
t.Fatal(err)
|
||
|
|
}
|
||
|
|
if _, err := publish.Run(ctx, env, imported); err == nil {
|
||
|
|
t.Fatal("republish accepted a tampered required product")
|
||
|
|
}
|
||
|
|
current, err := artifacts.LoadCurrentInventory(ctx, fake, prefix, artifacts.CurrentStateValidation{})
|
||
|
|
if err != nil || current.RunID != "20260521T010203Z-a1b2c3d4" {
|
||
|
|
t.Fatalf("failed republish changed current publication: %#v, %v", current, err)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
func TestImportedPartialExtractionRegeneratesCompleteBundle(t *testing.T) {
|
||
|
|
ctx := context.Background()
|
||
|
|
root := t.TempDir()
|
||
|
|
pipeline, campaign, session := writeValidConfigFiles(t, root)
|
||
|
|
cfg, err := config.LoadWithSessionOptions(pipeline, campaign, session, config.SessionLoadOptions{})
|
||
|
|
if err != nil {
|
||
|
|
t.Fatal(err)
|
||
|
|
}
|
||
|
|
cfg.Session.Inputs.AudioDir = ""
|
||
|
|
binary := filepath.Join(root, "notarius")
|
||
|
|
configPath := filepath.Join(root, "notarius.yml")
|
||
|
|
mustWriteTestFile(t, binary, "#!/bin/sh\nexit 0\n")
|
||
|
|
if err := os.Chmod(binary, 0755); err != nil {
|
||
|
|
t.Fatal(err)
|
||
|
|
}
|
||
|
|
mustWriteTestFile(t, configPath, "pipelines: {}\n")
|
||
|
|
cfg.Pipeline.Notarius = &config.NotariusConfig{Enabled: true, Binary: binary, ConfigPath: configPath, WorkingDirectory: root, PipelineID: "dnd-session", Timeout: "1m", Outputs: map[string]config.NotariusOutputConfig{
|
||
|
|
"npc_registry": {LaneID: "npc-registry"}, "encounters": {LaneID: "encounters"},
|
||
|
|
}}
|
||
|
|
fake := &storage.FakeBackend{}
|
||
|
|
current := seedCommittedRestoreSnapshot(t, cfg, fake, "20260519T010203Z-a1b2c3d4", map[string][]byte{
|
||
|
|
"transcripts/final.trimmed.json": []byte(`{"segments":[]}`), "artifacts/old-npcs.json": []byte(`{"records":[]}`),
|
||
|
|
})
|
||
|
|
setRestoreFixtureSources(t, fake, current, map[string]string{"artifacts/old-npcs.json": artifacts.ExtractionArtifactSourceID("npc_registry")})
|
||
|
|
if _, err := restoreSessionState(ctx, cfg, RestorePlanOptions{}, normalizeCommandDependencies(restoreCommandDependencies(fake))); err != nil {
|
||
|
|
t.Fatal(err)
|
||
|
|
}
|
||
|
|
paths := artifacts.NewLocalStore(root).SessionPathsFor(cfg.Session.Campaign, cfg.Session.SessionID)
|
||
|
|
before, err := (&manifest.LocalStore{}).Load(ctx, paths.ManifestPath)
|
||
|
|
if err != nil {
|
||
|
|
t.Fatal(err)
|
||
|
|
}
|
||
|
|
if artifacts.SuppliedExtractionComplete(paths, before, artifacts.ExtractionDefinitionsFromConfig(cfg.Pipeline.Notarius)) {
|
||
|
|
t.Fatal("partial extraction was treated as complete")
|
||
|
|
}
|
||
|
|
runner := &materializingNotariusRunner{cfg: cfg.Pipeline.Notarius}
|
||
|
|
result, err := executePlan(ctx, cfg, mustBoundedPlan(t, "extract", "extract"), RunOptions{Env: &Env{Notarius: runner}})
|
||
|
|
if err != nil {
|
||
|
|
t.Fatal(err)
|
||
|
|
}
|
||
|
|
if len(runner.requests) != 1 {
|
||
|
|
t.Fatalf("extraction requests = %d", len(runner.requests))
|
||
|
|
}
|
||
|
|
after, err := (&manifest.LocalStore{}).Load(ctx, result.ManifestPath)
|
||
|
|
if err != nil {
|
||
|
|
t.Fatal(err)
|
||
|
|
}
|
||
|
|
if after.Stages["extract"] == nil || after.Stages["extract"].Status != manifest.StatusSucceeded || len(after.Stages["extract"].Outputs) != 2 {
|
||
|
|
t.Fatalf("generated extraction evidence = %#v", after.Stages["extract"])
|
||
|
|
}
|
||
|
|
if _, ok := after.SuppliedProducts.Products[artifacts.ExtractionArtifactSourceID("npc_registry")]; ok {
|
||
|
|
t.Fatal("local extraction did not retire the imported product")
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
func TestImportedTrimmedTranscriptDoesNotInventNormalizedInput(t *testing.T) {
|
||
|
|
ctx := context.Background()
|
||
|
|
root := t.TempDir()
|
||
|
|
pipeline, campaign, session := writeValidConfigFiles(t, root)
|
||
|
|
cfg, err := config.LoadWithSessionOptions(pipeline, campaign, session, config.SessionLoadOptions{})
|
||
|
|
if err != nil {
|
||
|
|
t.Fatal(err)
|
||
|
|
}
|
||
|
|
cfg.Session.Inputs.AudioDir = ""
|
||
|
|
fake := &storage.FakeBackend{}
|
||
|
|
seedCommittedRestoreSnapshot(t, cfg, fake, "20260519T010203Z-a1b2c3d4", map[string][]byte{"transcripts/final.trimmed.json": []byte(`{"segments":[]}`)})
|
||
|
|
if _, err := restoreSessionState(ctx, cfg, RestorePlanOptions{}, normalizeCommandDependencies(restoreCommandDependencies(fake))); err != nil {
|
||
|
|
t.Fatal(err)
|
||
|
|
}
|
||
|
|
plan := mustBoundedPlan(t, "render", "render")
|
||
|
|
input := &executionInput{plan: plan}
|
||
|
|
effective, err := resolveEffectiveArtifacts(cfg, nil)
|
||
|
|
if err != nil {
|
||
|
|
t.Fatal(err)
|
||
|
|
}
|
||
|
|
preview, err := buildSessionPlan(ctx, cfg, input, nil, effective, commandDependencies{})
|
||
|
|
if err != nil || !strings.Contains(strings.Join(preview.InputIssues, " "), artifacts.ArtifactTranscriptFinal) {
|
||
|
|
t.Fatalf("default render preview = %#v, %v", preview, err)
|
||
|
|
}
|
||
|
|
if _, err := executePlan(ctx, cfg, plan, RunOptions{}); err == nil || !strings.Contains(err.Error(), artifacts.ArtifactTranscriptFinal) {
|
||
|
|
t.Fatalf("render accepted absent normalized transcript: %v", err)
|
||
|
|
}
|
||
|
|
}
|