179 lines
7.7 KiB
Go
179 lines
7.7 KiB
Go
package app
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"os"
|
|
"path/filepath"
|
|
"reflect"
|
|
"testing"
|
|
"time"
|
|
|
|
"gitea.maximumdirect.net/eric/narratio/internal/adapters/storage"
|
|
"gitea.maximumdirect.net/eric/narratio/internal/artifacts"
|
|
"gitea.maximumdirect.net/eric/narratio/internal/manifest"
|
|
)
|
|
|
|
func TestInventoryImportInstallsValidatedBaselineAndRetriesSave(t *testing.T) {
|
|
ctx := context.Background()
|
|
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
|
|
plan, err := buildInventoryRestorePlan(ctx, cfg, current, fake, RestorePlanOptions{})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if _, err := executeRestorePlan(ctx, cfg, current, plan, nil, fake); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := installInventoryBaseline(ctx, cfg, plan, &failingRestoreManifestStore{}); err == nil {
|
|
t.Fatal("expected final manifest save to fail")
|
|
}
|
|
path := artifacts.SessionManifestPathForCampaign(cfg.Pipeline.Workspace.Root, cfg.Session.Campaign, cfg.Session.SessionID)
|
|
if _, err := os.Stat(path); !os.IsNotExist(err) {
|
|
t.Fatalf("failed save installed manifest: %v", err)
|
|
}
|
|
retry, err := buildInventoryRestorePlan(ctx, cfg, current, fake, RestorePlanOptions{})
|
|
if err != nil || retry.SkipSameCount != 1 {
|
|
t.Fatalf("retry plan = %#v, %v", retry, err)
|
|
}
|
|
if err := installInventoryBaseline(ctx, cfg, retry, &manifest.LocalStore{}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
loaded, err := (&manifest.LocalStore{}).Load(ctx, path)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
paths := artifacts.NewLocalStore(cfg.Pipeline.Workspace.Root).SessionPathsFor(cfg.Session.Campaign, cfg.Session.SessionID)
|
|
if !artifacts.SuppliedTranscriptCoverage(paths, loaded.SuppliedProducts)["trim"] {
|
|
t.Fatalf("trimmed transcript unavailable after retry: %#v", loaded.SuppliedProducts)
|
|
}
|
|
if loaded.Stages["publish"] != nil || loaded.Stages["cleanup"] != nil {
|
|
t.Fatalf("import fabricated execution: %#v", loaded.Stages)
|
|
}
|
|
}
|
|
|
|
func TestInventoryImportReplacesProductAndInvalidatesDependents(t *testing.T) {
|
|
ctx := context.Background()
|
|
cfg := restorePlanConfig(t)
|
|
paths := artifacts.NewLocalStore(cfg.Pipeline.Workspace.Root).SessionPathsFor(cfg.Session.Campaign, cfg.Session.SessionID)
|
|
localPath := filepath.Join(paths.Root, "transcripts", "final.trimmed.json")
|
|
oldData := []byte(`{"segments":[]}`)
|
|
if err := os.MkdirAll(filepath.Dir(localPath), 0755); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := os.WriteFile(localPath, oldData, 0600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
local := manifest.New(cfg.Session.SessionID, time.Now().UTC())
|
|
local.Campaign = cfg.Session.Campaign
|
|
local.SuppliedProducts = &manifest.SuppliedProductCollection{Version: manifest.SuppliedProductVersion, Products: map[string]manifest.SuppliedProduct{
|
|
artifacts.ArtifactTranscriptFinalTrimmed: {Path: "transcripts/final.trimmed.json", Checksum: restoreCommitSHA256(oldData), Size: int64(len(oldData))},
|
|
}}
|
|
for _, name := range []string{"prepare", "trim", "render", "extract", "analyze"} {
|
|
local.MarkStageSucceeded(name, time.Now().UTC(), nil)
|
|
}
|
|
setAppAnalyzeEvidence(local, "unrelated", "artifacts/unrelated.md", []byte("existing checkpoint"))
|
|
checkpoint := manifest.CloneAnalyzeArtifactCollection(local.Stages["analyze"].AnalyzeArtifacts)
|
|
if err := (&manifest.LocalStore{}).Save(ctx, paths.ManifestPath, local); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
fake := &storage.FakeBackend{}
|
|
current := seedCommittedRestoreSnapshot(t, cfg, fake, "20260519T010203Z-a1b2c3d4", map[string][]byte{"transcripts/remote.json": []byte(`{"segments":[{"text":"changed"}]}`)})
|
|
current.Commit.Artifacts[1].Source = artifacts.ArtifactTranscriptFinalTrimmed
|
|
conflict, err := buildInventoryRestorePlan(ctx, cfg, current, fake, RestorePlanOptions{})
|
|
if err != nil || conflict.ConflictCount != 1 {
|
|
t.Fatalf("conflict plan = %#v, %v", conflict, err)
|
|
}
|
|
plan, err := buildInventoryRestorePlan(ctx, cfg, current, fake, RestorePlanOptions{Force: true})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if _, err := executeRestorePlan(ctx, cfg, current, plan, nil, fake); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := installInventoryBaseline(ctx, cfg, plan, &manifest.LocalStore{}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
loaded, err := (&manifest.LocalStore{}).Load(ctx, paths.ManifestPath)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if loaded.Stages["prepare"].Status != manifest.StatusSucceeded {
|
|
t.Fatal("unrelated preparation was invalidated")
|
|
}
|
|
for _, name := range []string{"trim", "render", "extract", "analyze"} {
|
|
if loaded.Stages[name].Status != manifest.StatusStale {
|
|
t.Fatalf("%s status = %q", name, loaded.Stages[name].Status)
|
|
}
|
|
}
|
|
if !reflect.DeepEqual(loaded.Stages["analyze"].AnalyzeArtifacts, checkpoint) {
|
|
t.Fatalf("unrelated analysis checkpoint changed: %#v", loaded.Stages["analyze"].AnalyzeArtifacts)
|
|
}
|
|
if !artifacts.SuppliedTranscriptCoverage(paths, loaded.SuppliedProducts)["trim"] {
|
|
t.Fatal("replacement is not available")
|
|
}
|
|
}
|
|
|
|
func TestInventoryImportPreservesIdenticalLocalAuthority(t *testing.T) {
|
|
ctx := context.Background()
|
|
cfg := restorePlanConfig(t)
|
|
paths := artifacts.NewLocalStore(cfg.Pipeline.Workspace.Root).SessionPathsFor(cfg.Session.Campaign, cfg.Session.SessionID)
|
|
localPath := filepath.Join(paths.Root, "transcripts", "final.trimmed.json")
|
|
body := []byte(`{"segments":[]}`)
|
|
if err := os.MkdirAll(filepath.Dir(localPath), 0755); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := os.WriteFile(localPath, body, 0600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
local := manifest.New(cfg.Session.SessionID, time.Now().UTC())
|
|
local.Campaign = cfg.Session.Campaign
|
|
local.RunID = "20260519T010203Z-a1b2c3d4"
|
|
setCurrentBuiltInEvidence(t, local, "trim", artifacts.TranscriptOutputKindFinalTrimmed, artifacts.ArtifactTranscriptFinalTrimmed, localPath)
|
|
local.MarkStageSucceeded("render", time.Now().UTC(), nil)
|
|
if err := (&manifest.LocalStore{}).Save(ctx, paths.ManifestPath, local); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
fake := &storage.FakeBackend{}
|
|
current := seedCommittedRestoreSnapshot(t, cfg, fake, "20260519T010203Z-a1b2c3d4", map[string][]byte{"transcripts/remote.json": body})
|
|
current.Commit.Artifacts[1].Source = artifacts.ArtifactTranscriptFinalTrimmed
|
|
plan, err := buildInventoryRestorePlan(ctx, cfg, current, fake, RestorePlanOptions{})
|
|
if err != nil || plan.SkipSameCount != 1 {
|
|
t.Fatalf("identical plan = %#v, %v", plan, err)
|
|
}
|
|
if err := installInventoryBaseline(ctx, cfg, plan, &manifest.LocalStore{}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
loaded, err := (&manifest.LocalStore{}).Load(ctx, paths.ManifestPath)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if loaded.SuppliedProducts != nil || loaded.Stages["trim"].Status != manifest.StatusSucceeded || loaded.Stages["render"].Status != manifest.StatusSucceeded {
|
|
t.Fatalf("identical import changed existing authority: %#v", loaded)
|
|
}
|
|
}
|
|
|
|
func TestInventoryImportRejectsWrongLocalSessionIdentity(t *testing.T) {
|
|
ctx := context.Background()
|
|
cfg := restorePlanConfig(t)
|
|
paths := artifacts.NewLocalStore(cfg.Pipeline.Workspace.Root).SessionPathsFor(cfg.Session.Campaign, cfg.Session.SessionID)
|
|
local := manifest.New("another-session", time.Now().UTC())
|
|
local.Campaign = cfg.Session.Campaign
|
|
if err := (&manifest.LocalStore{}).Save(ctx, paths.ManifestPath, local); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
fake := &storage.FakeBackend{}
|
|
current := seedCommittedRestoreSnapshot(t, cfg, fake, "20260519T010203Z-a1b2c3d4", nil)
|
|
if _, err := buildInventoryRestorePlan(ctx, cfg, current, fake, RestorePlanOptions{}); err == nil {
|
|
t.Fatal("expected a local session identity mismatch")
|
|
}
|
|
}
|
|
|
|
type failingRestoreManifestStore struct{ manifest.LocalStore }
|
|
|
|
func (failingRestoreManifestStore) Save(context.Context, string, *manifest.Manifest) error {
|
|
return errors.New("synthetic save failure")
|
|
}
|