package checkpoint import ( "encoding/json" "os" "path/filepath" "strings" "testing" "gitea.maximumdirect.net/eric/notarius/internal/core/source" coreworkspace "gitea.maximumdirect.net/eric/notarius/internal/core/workspace" "gitea.maximumdirect.net/eric/notarius/internal/framework/contracts" "gitea.maximumdirect.net/eric/notarius/internal/framework/pipeline" ) func TestWorkspaceRecorderWritesSuccessfulCheckpointFiles(t *testing.T) { root := t.TempDir() recorder := newTestRecorder(t, root) doc := &source.SourceDocument{ ID: "source-1", Kind: "document", Format: "text/plain", Digest: "sha256:source", Units: []source.SourceUnit{{ID: 1, Kind: "line", Text: "hello", Ref: source.SourceRef{SourceID: "source-1", StartUnitID: 1, EndUnitID: 1}}}, } if err := recorder.SourceRunning("seriatim"); err != nil { t.Fatalf("SourceRunning: %v", err) } assertManifestStatus(t, filepath.Join(root, "source", "manifest.json"), coreworkspace.StatusRunning) if err := recorder.SourceSucceeded("seriatim", doc); err != nil { t.Fatalf("SourceSucceeded: %v", err) } assertManifestStatus(t, filepath.Join(root, "source", "manifest.json"), coreworkspace.StatusSucceeded) if _, err := os.Stat(filepath.Join(root, "source", "source-document.json")); err != nil { t.Fatalf("expected source checkpoint payload: %v", err) } } func TestWorkspaceArtifactCheckpointsRoundTripCodecIdentityAndBytes(t *testing.T) { root := t.TempDir() recorder := newTestRecorder(t, root) loader := &WorkspaceLoader{root: root} schema := contracts.ArtifactSchema{ID: "dnd.spell_response", Name: "spell response", Version: "v1", JSONSchema: []byte(`{"type":"object"}`)} artifact := contracts.SerializedArtifact{Kind: "dnd.spells", Schema: schema, MediaType: "application/json", Content: []byte(`{"spell_casts":[]}`), Metadata: map[string]any{"spell_cast_count": float64(0)}} stored := pipeline.CheckpointArtifact{LaneID: "spells", ModuleKey: "dnd/spells", SourceID: "source-1", ChunkID: "chunk-1", ChunkIndex: 2, ChunkRef: source.SourceRef{SourceID: "source-1", StartUnitID: 4, EndUnitID: 8}, Artifact: artifact, SchemaDigest: contracts.DigestArtifactSchema(schema)} extractDeps := []pipeline.CheckpointFingerprint{{Name: "chunks", Value: "sha256:chunks"}} if err := recorder.ExtractSucceeded("spells", "dnd/spells", extractDeps, []pipeline.CheckpointArtifact{stored}, nil, nil); err != nil { t.Fatalf("ExtractSucceeded: %v", err) } extracted, decision := loader.Extract("spells", "dnd/spells", extractDeps) if !decision.Reused || len(extracted.Outputs) != 1 { t.Fatalf("extract decision=%#v checkpoint=%#v, want reused", decision, extracted) } got := extracted.Outputs[0] if got.Artifact.Kind != artifact.Kind || got.Artifact.Schema.ID != schema.ID || got.Artifact.Schema.Version != schema.Version || got.SchemaDigest != stored.SchemaDigest || string(got.Artifact.Content) != string(artifact.Content) || got.ChunkRef != stored.ChunkRef { t.Fatalf("artifact checkpoint = %#v, want codec identity, bytes, and provenance", got) } mergeDeps := artifactOutputDigests([]pipeline.CheckpointArtifact{stored}) if err := recorder.MergeSucceeded("spells", "merge", mergeDeps, stored, nil); err != nil { t.Fatalf("MergeSucceeded: %v", err) } merged, decision := loader.Merge("spells", "merge", mergeDeps) if !decision.Reused || string(merged.Output.Artifact.Content) != string(artifact.Content) { t.Fatalf("merge decision=%#v checkpoint=%#v, want reused", decision, merged) } normalizeDeps := artifactOutputDigests([]pipeline.CheckpointArtifact{stored}) if err := recorder.NormalizeSucceeded("spells", "normalize", normalizeDeps, stored, nil); err != nil { t.Fatalf("NormalizeSucceeded: %v", err) } normalized, decision := loader.Normalize("spells", "normalize", normalizeDeps) if !decision.Reused || normalized.Output.SchemaDigest != stored.SchemaDigest { t.Fatalf("normalize decision=%#v checkpoint=%#v, want reused", decision, normalized) } } func TestWorkspaceLoaderInvalidatesMissingCorruptAndMismatchedCheckpoints(t *testing.T) { t.Run("missing", func(t *testing.T) { loader := &WorkspaceLoader{root: t.TempDir()} if _, decision := loader.Source("seriatim"); decision.Reused || !strings.Contains(decision.Reason, "missing") { t.Fatalf("decision = %#v, want missing invalidation", decision) } }) t.Run("incompatible workspace schema remains untouched", func(t *testing.T) { root := t.TempDir() recorder := newTestRecorder(t, root) doc := &source.SourceDocument{ ID: "source-1", Kind: "document", Format: "text/plain", Digest: "sha256:source", Units: []source.SourceUnit{{ID: 1, Kind: "line", Text: "hello", Ref: source.SourceRef{SourceID: "source-1", StartUnitID: 1, EndUnitID: 1}}}, } if err := recorder.SourceSucceeded("seriatim", doc); err != nil { t.Fatalf("SourceSucceeded: %v", err) } manifestPath := filepath.Join(root, "source", "manifest.json") manifest := strings.Replace(string(readFile(t, manifestPath)), coreworkspace.WorkspaceSchemaVersion, coreworkspace.WorkspaceSchemaVersionV1, 1) if err := os.WriteFile(manifestPath, []byte(manifest), 0o644); err != nil { t.Fatalf("write legacy manifest: %v", err) } beforeManifest := readFile(t, manifestPath) payloadPath := filepath.Join(root, "source", "source-document.json") beforePayload := readFile(t, payloadPath) loader := &WorkspaceLoader{root: root} if _, decision := loader.Source("seriatim"); decision.Reused || !strings.Contains(decision.Reason, "incompatible") || !strings.Contains(decision.Reason, coreworkspace.WorkspaceSchemaVersionV1) { t.Fatalf("decision = %#v, want incompatible legacy schema invalidation", decision) } if got := readFile(t, manifestPath); string(got) != string(beforeManifest) { t.Fatal("legacy manifest changed during reuse decision") } if got := readFile(t, payloadPath); string(got) != string(beforePayload) { t.Fatal("legacy payload changed during reuse decision") } }) } func TestWorkspaceCheckpointsIgnoreLegacyChunkFiles(t *testing.T) { root := t.TempDir() legacyManifest := []byte(`{"legacy":"manifest"}`) legacyPayload := []byte(`{"legacy":"chunks"}`) legacyDir := filepath.Join(root, "chunk") if err := os.MkdirAll(legacyDir, 0o700); err != nil { t.Fatal(err) } manifestPath := filepath.Join(legacyDir, "manifest.json") payloadPath := filepath.Join(legacyDir, "chunks.json") if err := os.WriteFile(manifestPath, legacyManifest, 0o600); err != nil { t.Fatal(err) } if err := os.WriteFile(payloadPath, legacyPayload, 0o600); err != nil { t.Fatal(err) } doc := &source.SourceDocument{ID: "source-1", Kind: "document", Format: "text/plain", Units: []source.SourceUnit{{ID: 1, Kind: "line", Text: "hello", Ref: source.SourceRef{SourceID: "source-1", StartUnitID: 1, EndUnitID: 1}}}} doc.Digest, _ = source.DigestDocument(doc) recorder := newTestRecorder(t, root) if err := recorder.SourceSucceeded("seriatim", doc); err != nil { t.Fatal(err) } loaded, decision := (&WorkspaceLoader{root: root}).Source("seriatim") if !decision.Reused || loaded.Document == nil || loaded.Document.Digest != doc.Digest { t.Fatalf("source checkpoint = %#v decision = %#v", loaded, decision) } if got := readFile(t, manifestPath); string(got) != string(legacyManifest) { t.Fatalf("legacy manifest changed: %s", got) } if got := readFile(t, payloadPath); string(got) != string(legacyPayload) { t.Fatalf("legacy payload changed: %s", got) } } func TestWorkspaceRecorderRecordsRejectedExtractOutputs(t *testing.T) { root := t.TempDir() recorder := newTestRecorder(t, root) rejected := []contracts.RejectedOutput{ { Stage: string(pipeline.StageExtract), LaneID: "spells", ModuleKey: "dnd/spells", ChunkID: "chunk-1", ValidatorName: "shape", ReasonCode: "invalid_shape", Message: "bad shape", }, } if err := recorder.ExtractRunning("spells", "dnd/spells", []pipeline.CheckpointFingerprint{{Name: "chunks", Value: "sha256:chunks"}}); err != nil { t.Fatalf("ExtractRunning: %v", err) } if err := recorder.ExtractSucceeded("spells", "dnd/spells", nil, nil, rejected, nil); err != nil { t.Fatalf("ExtractSucceeded: %v", err) } var manifest coreworkspace.ExtractLaneManifest readJSON(t, filepath.Join(root, "extract", "spells", "manifest.json"), &manifest) if manifest.Status != coreworkspace.StatusSucceededWithRejections || manifest.ValidationStatus != "rejected" { t.Fatalf("extract manifest status = %q validation=%q", manifest.Status, manifest.ValidationStatus) } if len(manifest.Rejections) != 1 || manifest.Rejections[0].Count != 1 || manifest.Rejections[0].ReasonCode != "invalid_shape" { t.Fatalf("rejections = %#v", manifest.Rejections) } var payload struct { Rejected []contracts.RejectedOutput `json:"rejected"` } readJSON(t, filepath.Join(root, "extract", "spells", "outputs.json"), &payload) if len(payload.Rejected) != 1 || payload.Rejected[0].ChunkID != "chunk-1" { t.Fatalf("checkpoint rejected payload = %#v", payload.Rejected) } } func TestWorkspaceRecorderRecordsFailedStages(t *testing.T) { root := t.TempDir() recorder := newTestRecorder(t, root) if err := recorder.MergeRunning("spells", "appendorder", nil); err != nil { t.Fatalf("MergeRunning: %v", err) } if err := recorder.MergeFailed("spells", "appendorder", nil, assertErr("merge failed")); err != nil { t.Fatalf("MergeFailed: %v", err) } var manifest coreworkspace.MergeLaneManifest readJSON(t, filepath.Join(root, "merge", "spells", "manifest.json"), &manifest) if manifest.Status != coreworkspace.StatusFailed { t.Fatalf("status = %q, want failed", manifest.Status) } if !strings.Contains(manifest.Metadata["error"], "merge failed") { t.Fatalf("metadata = %#v, want error", manifest.Metadata) } } func TestWorkspaceRecorderRecordsWarningOnlyValidation(t *testing.T) { root := t.TempDir() recorder := newTestRecorder(t, root) schema := contracts.ArtifactSchema{ID: "test.artifact", Name: "test_artifact", Version: "v1", JSONSchema: []byte(`{"type":"object"}`)} output := pipeline.CheckpointArtifact{ LaneID: "events", ModuleKey: "noop", SourceID: "source-1", Artifact: contracts.SerializedArtifact{Kind: "test/artifact", Schema: schema, MediaType: "application/json", Content: []byte(`{"ok":true}`)}, SchemaDigest: contracts.DigestArtifactSchema(schema), } warnings := []contracts.Warning{{ReasonCode: "note", Message: "warning"}} if err := recorder.NormalizeSucceeded("events", "noop", nil, output, warnings); err != nil { t.Fatalf("NormalizeSucceeded: %v", err) } var manifest coreworkspace.NormalizeLaneManifest readJSON(t, filepath.Join(root, "normalize", "events", "manifest.json"), &manifest) if manifest.Status != coreworkspace.StatusSucceeded || manifest.ValidationStatus != "approved_with_warnings" { t.Fatalf("normalize manifest status = %q validation=%q", manifest.Status, manifest.ValidationStatus) } } func newTestRecorder(t *testing.T, root string) *WorkspaceRecorder { t.Helper() return &WorkspaceRecorder{root: root} } func assertManifestStatus(t *testing.T, path string, want coreworkspace.StageStatus) { t.Helper() var manifest coreworkspace.StageManifest readJSON(t, path, &manifest) if manifest.Status != want { t.Fatalf("%s status = %q, want %q", path, manifest.Status, want) } } func readJSON(t *testing.T, path string, out any) { t.Helper() data := readFile(t, path) if err := json.Unmarshal(data, out); err != nil { t.Fatalf("decode %q: %v", path, err) } } func readFile(t *testing.T, path string) []byte { t.Helper() data, err := os.ReadFile(path) if err != nil { t.Fatalf("read %q: %v", path, err) } return data } type assertErr string func (e assertErr) Error() string { return string(e) }