358 lines
12 KiB
Go
358 lines
12 KiB
Go
package manifest
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"errors"
|
|
"os"
|
|
"path/filepath"
|
|
"reflect"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
|
|
"gitea.maximumdirect.net/eric/narratio/internal/artifactmodel"
|
|
)
|
|
|
|
func TestLocalStoreCreateSaveLoadRoundTrip(t *testing.T) {
|
|
store := &LocalStore{}
|
|
ctx := context.Background()
|
|
|
|
m, err := store.Create(ctx, "2026-05-03")
|
|
if err != nil {
|
|
t.Fatalf("Create() error = %v", err)
|
|
}
|
|
|
|
now := time.Date(2026, 5, 3, 12, 0, 0, 0, time.UTC)
|
|
m.MarkStageRunning("prepare", now)
|
|
m.MarkStageSucceeded("prepare", now.Add(2*time.Second), []ArtifactRecord{{Kind: "transcript", LocalPath: "transcripts/base.json"}})
|
|
m.Campaign = "forsaken"
|
|
m.RunID = "20260515T031522Z-a1b2c3d4"
|
|
m.LocalWorkDir = "/var/lib/narratio/work/forsaken/2026-05-03/20260515T031522Z-a1b2c3d4"
|
|
m.LocalSpoolDir = "/var/spool/narratio/forsaken/2026-05-03/20260515T031522Z-a1b2c3d4/audio"
|
|
m.S3Bucket = "my-dnd-archive"
|
|
m.S3SessionPrefix = "dnd/campaigns/forsaken/sessions/2026-05-03/"
|
|
m.S3RunPrefix = "dnd/campaigns/forsaken/sessions/2026-05-03/runs/20260515T031522Z-a1b2c3d4/"
|
|
m.EffectiveConfig = &EffectiveConfigProvenance{
|
|
SelectedProfile: &SelectedProfileProvenance{Name: "production", Source: "default"},
|
|
EffectiveConfigDigest: "001122",
|
|
}
|
|
|
|
path := filepath.Join(t.TempDir(), "manifest.json")
|
|
if err := store.Save(ctx, path, m); err != nil {
|
|
t.Fatalf("Save() error = %v", err)
|
|
}
|
|
|
|
loaded, err := store.Load(ctx, path)
|
|
if err != nil {
|
|
t.Fatalf("Load() error = %v", err)
|
|
}
|
|
|
|
if loaded.SessionID != "2026-05-03" {
|
|
t.Fatalf("SessionID = %q, want %q", loaded.SessionID, "2026-05-03")
|
|
}
|
|
if loaded.Campaign != "forsaken" {
|
|
t.Fatalf("Campaign = %q, want %q", loaded.Campaign, "forsaken")
|
|
}
|
|
if loaded.RunID != "20260515T031522Z-a1b2c3d4" {
|
|
t.Fatalf("RunID = %q, want run id", loaded.RunID)
|
|
}
|
|
if loaded.EffectiveConfig == nil || loaded.EffectiveConfig.SelectedProfile == nil || loaded.EffectiveConfig.SelectedProfile.Name != "production" || loaded.EffectiveConfig.EffectiveConfigDigest != "001122" {
|
|
t.Fatalf("effective config provenance = %#v", loaded.EffectiveConfig)
|
|
}
|
|
stage, ok := loaded.Stages["prepare"]
|
|
if !ok {
|
|
t.Fatalf("stage prepare not found")
|
|
}
|
|
if stage.Status != StatusSucceeded {
|
|
t.Fatalf("status = %q, want %q", stage.Status, StatusSucceeded)
|
|
}
|
|
if len(stage.Outputs) != 1 {
|
|
t.Fatalf("outputs len = %d, want 1", len(stage.Outputs))
|
|
}
|
|
if loaded.UpdatedAt.IsZero() {
|
|
t.Fatal("UpdatedAt is zero")
|
|
}
|
|
}
|
|
|
|
func TestLocalStorePreservesOptionalArtifactMetadata(t *testing.T) {
|
|
store := &LocalStore{}
|
|
ctx := context.Background()
|
|
dir := t.TempDir()
|
|
path := filepath.Join(dir, "manifest.json")
|
|
data := `{
|
|
"schema_version": "narratio.session-manifest.v1",
|
|
"session_id": "2026-05-03",
|
|
"created_at": "2026-05-03T10:00:00Z",
|
|
"updated_at": "2026-05-03T10:01:00Z",
|
|
"stages": {
|
|
"render": {
|
|
"name": "render",
|
|
"status": "succeeded",
|
|
"created_at": "2026-05-03T10:00:00Z",
|
|
"updated_at": "2026-05-03T10:01:00Z",
|
|
"outputs": [{"kind":"scriptorium_artifact","local_path":"artifacts/recap.md"}]
|
|
}
|
|
}
|
|
}`
|
|
if err := os.WriteFile(path, []byte(data), 0o644); err != nil {
|
|
t.Fatalf("WriteFile() manifest error = %v", err)
|
|
}
|
|
|
|
loaded, err := store.Load(ctx, path)
|
|
if err != nil {
|
|
t.Fatalf("Load() manifest error = %v", err)
|
|
}
|
|
oldOutput := loaded.Stages["render"].Outputs[0]
|
|
if oldOutput.Contract != nil || oldOutput.ExternalProvenance != nil {
|
|
t.Fatalf("old output metadata = %#v, %#v; want nil", oldOutput.Contract, oldOutput.ExternalProvenance)
|
|
}
|
|
if err := store.Save(ctx, path, loaded); err != nil {
|
|
t.Fatalf("Save() manifest error = %v", err)
|
|
}
|
|
roundTripped, err := os.ReadFile(path)
|
|
if err != nil {
|
|
t.Fatalf("ReadFile() round-tripped old manifest error = %v", err)
|
|
}
|
|
if strings.Contains(string(roundTripped), `"contract"`) || strings.Contains(string(roundTripped), `"external_provenance"`) {
|
|
t.Fatalf("old manifest gained fabricated metadata:\n%s", roundTripped)
|
|
}
|
|
|
|
loaded.Stages["render"].Outputs[0].Contract = &artifactmodel.ContractMetadata{
|
|
MediaType: "application/json",
|
|
SchemaID: "example.recap",
|
|
SchemaVersion: "v1",
|
|
}
|
|
loaded.Stages["render"].Outputs[0].ExternalProvenance = &artifactmodel.ExternalProvenance{
|
|
System: "example",
|
|
RunID: "external-run",
|
|
PipelineID: "pipeline",
|
|
ArtifactID: "recap",
|
|
}
|
|
metadataPath := filepath.Join(dir, "metadata-manifest.json")
|
|
if err := store.Save(ctx, metadataPath, loaded); err != nil {
|
|
t.Fatalf("Save() metadata manifest error = %v", err)
|
|
}
|
|
withMetadata, err := store.Load(ctx, metadataPath)
|
|
if err != nil {
|
|
t.Fatalf("Load() metadata manifest error = %v", err)
|
|
}
|
|
got := withMetadata.Stages["render"].Outputs[0]
|
|
if got.Contract == nil || got.Contract.SchemaID != "example.recap" {
|
|
t.Fatalf("contract = %#v, want persisted contract", got.Contract)
|
|
}
|
|
if got.ExternalProvenance == nil || got.ExternalProvenance.RunID != "external-run" {
|
|
t.Fatalf("external provenance = %#v, want persisted provenance", got.ExternalProvenance)
|
|
}
|
|
}
|
|
|
|
func TestLocalStoreSaveUpdatesTimestamp(t *testing.T) {
|
|
store := &LocalStore{}
|
|
ctx := context.Background()
|
|
|
|
createdAt := time.Date(2026, 5, 3, 10, 0, 0, 0, time.UTC)
|
|
m := New("2026-05-03", createdAt)
|
|
m.UpdatedAt = createdAt
|
|
|
|
path := filepath.Join(t.TempDir(), "manifest.json")
|
|
if err := store.Save(ctx, path, m); err != nil {
|
|
t.Fatalf("first Save() error = %v", err)
|
|
}
|
|
firstUpdated := m.UpdatedAt
|
|
|
|
time.Sleep(5 * time.Millisecond)
|
|
if err := store.Save(ctx, path, m); err != nil {
|
|
t.Fatalf("second Save() error = %v", err)
|
|
}
|
|
|
|
if !m.UpdatedAt.After(firstUpdated) {
|
|
t.Fatalf("UpdatedAt = %v, want after %v", m.UpdatedAt, firstUpdated)
|
|
}
|
|
}
|
|
|
|
func TestLocalStoreSaveAtomicPractical(t *testing.T) {
|
|
store := &LocalStore{}
|
|
ctx := context.Background()
|
|
|
|
dir := t.TempDir()
|
|
path := filepath.Join(dir, "manifest.json")
|
|
|
|
m := New("session-a", time.Date(2026, 5, 3, 10, 0, 0, 0, time.UTC))
|
|
if err := store.Save(ctx, path, m); err != nil {
|
|
t.Fatalf("Save() error = %v", err)
|
|
}
|
|
|
|
data, err := os.ReadFile(path)
|
|
if err != nil {
|
|
t.Fatalf("ReadFile() error = %v", err)
|
|
}
|
|
var decoded Manifest
|
|
if err := json.Unmarshal(data, &decoded); err != nil {
|
|
t.Fatalf("saved manifest is not valid JSON: %v", err)
|
|
}
|
|
|
|
entries, err := os.ReadDir(dir)
|
|
if err != nil {
|
|
t.Fatalf("ReadDir() error = %v", err)
|
|
}
|
|
for _, e := range entries {
|
|
if strings.HasPrefix(e.Name(), ".manifest.json.tmp-") {
|
|
t.Fatalf("found unexpected temp file after save: %s", e.Name())
|
|
}
|
|
}
|
|
|
|
m.SessionID = "session-b"
|
|
if err := store.Save(ctx, path, m); err != nil {
|
|
t.Fatalf("second Save() error = %v", err)
|
|
}
|
|
|
|
loaded, err := store.Load(ctx, path)
|
|
if err != nil {
|
|
t.Fatalf("Load() error = %v", err)
|
|
}
|
|
if loaded.SessionID != "session-b" {
|
|
t.Fatalf("SessionID = %q, want %q", loaded.SessionID, "session-b")
|
|
}
|
|
}
|
|
|
|
func TestWriteJSONAtomicallyChecksCancellationBeforeReplacement(t *testing.T) {
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
cancel()
|
|
|
|
dir := t.TempDir()
|
|
path := filepath.Join(dir, "manifest.json")
|
|
err := writeJSONAtomically(ctx, path, []byte("{\"session_id\":\"session\"}\n"))
|
|
if !errors.Is(err, context.Canceled) {
|
|
t.Fatalf("writeJSONAtomically() error = %v, want context cancellation", err)
|
|
}
|
|
if _, err := os.Stat(path); !os.IsNotExist(err) {
|
|
t.Fatalf("replacement destination exists after cancellation: stat err = %v", err)
|
|
}
|
|
entries, err := os.ReadDir(dir)
|
|
if err != nil {
|
|
t.Fatalf("ReadDir() error = %v", err)
|
|
}
|
|
for _, entry := range entries {
|
|
if strings.HasPrefix(entry.Name(), ".manifest.json.tmp-") {
|
|
t.Fatalf("temporary manifest remained after cancellation: %s", entry.Name())
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestLoadRejectsInvalidManifest(t *testing.T) {
|
|
store := &LocalStore{}
|
|
ctx := context.Background()
|
|
|
|
path := filepath.Join(t.TempDir(), "manifest.json")
|
|
if err := os.WriteFile(path, []byte(`{"schema_version":"narratio.session-manifest.v1","updated_at":"2026-05-03T10:00:00Z","stages":{}}`), 0o644); err != nil {
|
|
t.Fatalf("WriteFile() error = %v", err)
|
|
}
|
|
|
|
_, err := store.Load(ctx, path)
|
|
if err == nil {
|
|
t.Fatal("expected error, got nil")
|
|
}
|
|
if !strings.Contains(err.Error(), "session_id is required") {
|
|
t.Fatalf("error = %q, want session_id validation", err.Error())
|
|
}
|
|
}
|
|
|
|
func TestLocalStoreRejectsRetiredCleanupFieldAndRoundTripsCanonicalV1(t *testing.T) {
|
|
store := &LocalStore{}
|
|
canonical := `{"schema_version":"narratio.session-manifest.v1","session_id":"2026-05-03","created_at":"2026-05-03T10:00:00Z","updated_at":"2026-05-03T10:01:00Z","stages":{}}`
|
|
|
|
loaded, err := store.LoadReader(context.Background(), strings.NewReader(canonical))
|
|
if err != nil {
|
|
t.Fatalf("load canonical manifest: %v", err)
|
|
}
|
|
encoded, err := json.Marshal(loaded)
|
|
if err != nil {
|
|
t.Fatalf("marshal canonical manifest: %v", err)
|
|
}
|
|
roundTripped, err := store.LoadReader(context.Background(), strings.NewReader(string(encoded)))
|
|
if err != nil {
|
|
t.Fatalf("reload canonical manifest: %v", err)
|
|
}
|
|
if !reflect.DeepEqual(roundTripped, loaded) {
|
|
t.Fatalf("round-tripped manifest = %#v, want %#v", roundTripped, loaded)
|
|
}
|
|
|
|
retired := strings.TrimSuffix(canonical, "}") + `,"post_publish_cleanup":{"committed_run_id":"20260905T010203Z-a1b2c3d4"}}`
|
|
_, err = store.LoadReader(context.Background(), strings.NewReader(retired))
|
|
if err == nil || !strings.Contains(err.Error(), `unknown field "post_publish_cleanup"`) {
|
|
t.Fatalf("load retired cleanup field error = %v, want strict unknown-field rejection", err)
|
|
}
|
|
}
|
|
|
|
func TestLocalStoreRejectsUnsupportedSchemaVersions(t *testing.T) {
|
|
store := &LocalStore{}
|
|
now := time.Date(2026, 5, 3, 10, 0, 0, 0, time.UTC)
|
|
|
|
for _, test := range []struct {
|
|
name string
|
|
schema string
|
|
load func(*strings.Reader) error
|
|
save func() error
|
|
want string
|
|
}{
|
|
{
|
|
name: "missing session schema",
|
|
schema: `{"session_id":"session","created_at":"2026-05-03T10:00:00Z","updated_at":"2026-05-03T10:00:00Z","stages":{}}`,
|
|
load: func(source *strings.Reader) error {
|
|
_, err := store.LoadReader(context.Background(), source)
|
|
return err
|
|
},
|
|
save: func() error {
|
|
return store.Save(context.Background(), filepath.Join(t.TempDir(), "manifest.json"), &Manifest{SessionID: "session", CreatedAt: now, UpdatedAt: now, Stages: map[string]*StageRecord{}})
|
|
},
|
|
want: "session manifest schema_version is required",
|
|
},
|
|
{
|
|
name: "unknown session schema",
|
|
schema: `{"schema_version":"narratio.session-manifest.v99","session_id":"session","created_at":"2026-05-03T10:00:00Z","updated_at":"2026-05-03T10:00:00Z","stages":{}}`,
|
|
load: func(source *strings.Reader) error {
|
|
_, err := store.LoadReader(context.Background(), source)
|
|
return err
|
|
},
|
|
save: func() error {
|
|
m := New("session", now)
|
|
m.SchemaVersion = "narratio.session-manifest.v99"
|
|
return store.Save(context.Background(), filepath.Join(t.TempDir(), "manifest.json"), m)
|
|
},
|
|
want: "unsupported session manifest schema_version",
|
|
},
|
|
} {
|
|
t.Run(test.name, func(t *testing.T) {
|
|
if err := test.load(strings.NewReader(test.schema)); err == nil || !strings.Contains(err.Error(), test.want) {
|
|
t.Fatalf("load error = %v, want %q", err, test.want)
|
|
}
|
|
if err := test.save(); err == nil || !strings.Contains(err.Error(), test.want) {
|
|
t.Fatalf("save error = %v, want %q", err, test.want)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestLocalStoreRejectsIncompleteStageIdentity(t *testing.T) {
|
|
now := time.Date(2026, 5, 3, 10, 0, 0, 0, time.UTC)
|
|
store := &LocalStore{}
|
|
for _, test := range []struct {
|
|
name string
|
|
stages map[string]*StageRecord
|
|
want string
|
|
}{
|
|
{name: "nil map", want: "stages is required"},
|
|
{name: "nil entry", stages: map[string]*StageRecord{"prepare": nil}, want: "stages.prepare is nil"},
|
|
{name: "missing name", stages: map[string]*StageRecord{"prepare": {CreatedAt: now, UpdatedAt: now}}, want: "stages.prepare.name is required"},
|
|
{name: "mismatched name", stages: map[string]*StageRecord{"prepare": {Name: "render", CreatedAt: now, UpdatedAt: now}}, want: "does not match map key"},
|
|
} {
|
|
t.Run(test.name, func(t *testing.T) {
|
|
m := New("session", now)
|
|
m.Stages = test.stages
|
|
if err := store.Save(context.Background(), filepath.Join(t.TempDir(), "manifest.json"), m); err == nil || !strings.Contains(err.Error(), test.want) {
|
|
t.Fatalf("Save() error = %v, want %q", err, test.want)
|
|
}
|
|
})
|
|
}
|
|
}
|