package app import ( "context" "os" "path/filepath" "slices" "testing" "time" "gitea.maximumdirect.net/eric/narratio/internal/adapters/storage" "gitea.maximumdirect.net/eric/narratio/internal/artifactmodel" "gitea.maximumdirect.net/eric/narratio/internal/artifacts" "gitea.maximumdirect.net/eric/narratio/internal/config" "gitea.maximumdirect.net/eric/narratio/internal/manifest" ) func TestInventoryRestoreMapsSourceInsteadOfRemoteDestination(t *testing.T) { cfg := restorePlanConfig(t) fake := &storage.FakeBackend{} current := seedCommittedRestoreSnapshot(t, cfg, fake, "20260519T010203Z-a1b2c3d4", map[string][]byte{"transcripts/renamed.json": []byte(`{"segments":[]}`)}) current.Commit.Artifacts[1].Source = artifacts.ArtifactTranscriptFinalTrimmed plan, err := buildInventoryRestorePlan(context.Background(), cfg, current, fake, RestorePlanOptions{}) if err != nil { t.Fatal(err) } if len(plan.Actions) != 1 || plan.Actions[0].SourceID != artifacts.ArtifactTranscriptFinalTrimmed || plan.Actions[0].LocalRelativePath == "transcripts/renamed.json" { t.Fatalf("unexpected inventory actions: %#v", plan.Actions) } } func TestInventoryRestoreProjectsSelectedTranscriptProducts(t *testing.T) { cfg := restorePlanConfig(t) cfg.Pipeline.Render = &config.RenderConfig{Variants: []string{"trimmed"}} fake := &storage.FakeBackend{} current := seedCommittedRestoreSnapshot(t, cfg, fake, "20260519T010203Z-a1b2c3d4", map[string][]byte{ "transcripts/one.json": []byte(`{"segments":[]}`), "transcripts/two.md": []byte("synthetic transcript"), }) for i := range current.Commit.Artifacts { item := ¤t.Commit.Artifacts[i] switch filepath.Ext(item.DestinationKey) { case ".json": if item.Type == artifacts.RemoteArtifactTypePublishedOutput { item.Source = artifacts.ArtifactTranscriptFinalTrimmed } case ".md": item.Source = artifactmodel.SourceTranscriptFinalTrimmedMarkdown } } plan, err := buildInventoryRestorePlan(context.Background(), cfg, current, fake, RestorePlanOptions{}) if err != nil { t.Fatal(err) } for _, name := range []string{"transcribe", "merge", "polish", "normalize", "trim", "render"} { if !slices.Contains(plan.Satisfied, name) { t.Fatalf("%q absent from projected coverage: %#v", name, plan.Satisfied) } } if plan.candidate == nil || plan.candidate.SuppliedProducts == nil || len(plan.candidate.SuppliedProducts.Products) != 2 { t.Fatalf("projected baseline does not contain both products: %#v", plan.candidate) } if _, err := os.Stat(artifacts.SessionManifestPathForCampaign(cfg.Pipeline.Workspace.Root, cfg.Session.Campaign, cfg.Session.SessionID)); !os.IsNotExist(err) { t.Fatalf("planning wrote a manifest: %v", err) } } func TestInventoryRestoreConflictsDoNotContributeCoverage(t *testing.T) { cfg := restorePlanConfig(t) fake := &storage.FakeBackend{} current := seedCommittedRestoreSnapshot(t, cfg, fake, "20260519T010203Z-a1b2c3d4", map[string][]byte{"transcripts/remote.json": []byte(`{"segments":[]}`)}) current.Commit.Artifacts[1].Source = artifacts.ArtifactTranscriptFinalTrimmed first, err := buildInventoryRestorePlan(context.Background(), cfg, current, fake, RestorePlanOptions{}) if err != nil || len(first.Actions) != 1 { t.Fatalf("initial plan = %#v, %v", first, err) } local := first.Actions[0].LocalPath if err := os.MkdirAll(filepath.Dir(local), 0755); err != nil { t.Fatal(err) } if err := os.WriteFile(local, []byte("different"), 0600); err != nil { t.Fatal(err) } conflict, err := buildInventoryRestorePlan(context.Background(), cfg, current, fake, RestorePlanOptions{}) if err != nil || conflict.ConflictCount != 1 || slices.Contains(conflict.Satisfied, "trim") { t.Fatalf("conflicting plan = %#v, %v", conflict, err) } forced, err := buildInventoryRestorePlan(context.Background(), cfg, current, fake, RestorePlanOptions{Force: true}) if err != nil || forced.DownloadCount != 1 || !slices.Contains(forced.Satisfied, "trim") { t.Fatalf("forced plan = %#v, %v", forced, err) } } func TestInventoryRestoreUsesCurrentConfiguredProductPath(t *testing.T) { cfg := restorePlanConfig(t) cfg.ArtifactCatalog = &config.ArtifactCatalog{Artifacts: map[string]config.ScriptoriumArtifactConfig{ "recap": {Enabled: true, OutputPath: "artifacts/current-recap.md"}, }} fake := &storage.FakeBackend{} current := seedCommittedRestoreSnapshot(t, cfg, fake, "20260519T010203Z-a1b2c3d4", map[string][]byte{"artifacts/remote-name.md": []byte("synthetic recap")}) current.Commit.Artifacts[1].Source = artifacts.ConfiguredArtifactSourceID("recap") plan, err := buildInventoryRestorePlan(context.Background(), cfg, current, fake, RestorePlanOptions{}) if err != nil { t.Fatal(err) } if len(plan.Actions) != 1 || plan.Actions[0].LocalRelativePath != "artifacts/current-recap.md" || plan.Actions[0].RemoteKey != current.Commit.Artifacts[1].DestinationKey { t.Fatalf("configured product plan = %#v", plan.Actions) } } func TestInventoryRestoreRejectsCollidingProductPaths(t *testing.T) { cfg := restorePlanConfig(t) cfg.ArtifactCatalog = &config.ArtifactCatalog{Artifacts: map[string]config.ScriptoriumArtifactConfig{ "first": {Enabled: true, OutputPath: "artifacts/shared.md"}, "second": {Enabled: true, OutputPath: "artifacts/shared.md"}, }} fake := &storage.FakeBackend{} current := seedCommittedRestoreSnapshot(t, cfg, fake, "20260519T010203Z-a1b2c3d4", map[string][]byte{"artifacts/a.md": []byte("a"), "artifacts/b.md": []byte("b")}) current.Commit.Artifacts[1].Source = artifacts.ConfiguredArtifactSourceID("first") current.Commit.Artifacts[2].Source = artifacts.ConfiguredArtifactSourceID("second") if _, err := buildInventoryRestorePlan(context.Background(), cfg, current, fake, RestorePlanOptions{}); err == nil { t.Fatal("expected colliding local products to fail planning") } } func TestInventoryRestoreCandidatePreservesLocalProgress(t *testing.T) { cfg := restorePlanConfig(t) paths := artifacts.NewLocalStore(cfg.Pipeline.Workspace.Root).SessionPathsFor(cfg.Session.Campaign, cfg.Session.SessionID) if err := os.MkdirAll(paths.Root, 0755); err != nil { t.Fatal(err) } local := manifest.New(cfg.Session.SessionID, time.Now().UTC()) local.Campaign = cfg.Session.Campaign local.Stages["prepare"] = &manifest.StageRecord{Name: "prepare", Status: manifest.StatusSucceeded} manifestPath := artifacts.SessionManifestPathForCampaign(cfg.Pipeline.Workspace.Root, cfg.Session.Campaign, cfg.Session.SessionID) if err := (&manifest.LocalStore{}).Save(context.Background(), manifestPath, local); err != nil { t.Fatal(err) } before, err := os.ReadFile(manifestPath) if err != nil { t.Fatal(err) } fake := &storage.FakeBackend{} current := seedCommittedRestoreSnapshot(t, cfg, fake, "20260519T010203Z-a1b2c3d4", map[string][]byte{"transcripts/remote.json": []byte(`{"segments":[]}`)}) current.Commit.Artifacts[1].Source = artifacts.ArtifactTranscriptFinalTrimmed plan, err := buildInventoryRestorePlan(context.Background(), cfg, current, fake, RestorePlanOptions{}) if err != nil { t.Fatal(err) } if plan.candidate.Stages["prepare"] == nil || plan.candidate.Stages["prepare"].Status != manifest.StatusSucceeded { t.Fatalf("local progress lost from candidate: %#v", plan.candidate.Stages) } after, err := os.ReadFile(manifestPath) if err != nil || string(before) != string(after) { t.Fatalf("planning changed local manifest: %v", err) } }