package checkpoint import ( "encoding/base64" "encoding/json" "os" "path/filepath" "reflect" "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}}}, } chunks := []source.Chunk{ { ID: "chunk-1", SourceID: "source-1", Index: 0, Ref: source.SourceRef{SourceID: "source-1", StartUnitID: 1, EndUnitID: 1}, Content: []byte("chunk content"), MediaType: "text/plain", Units: doc.Units, Annotations: source.ChunkAnnotations{ "same": json.RawMessage(`{"range":1}`), }, PlanAnnotations: source.ChunkAnnotations{ "same": json.RawMessage(`{"plan":2}`), }, }, } 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) } if err := recorder.ChunkRunning("generic", doc.Digest); err != nil { t.Fatalf("ChunkRunning: %v", err) } if err := recorder.ChunkSucceeded("generic", doc.Digest, chunks, nil); err != nil { t.Fatalf("ChunkSucceeded: %v", err) } assertManifestStatus(t, filepath.Join(root, "chunk", "manifest.json"), coreworkspace.StatusSucceeded) var chunkPayload struct { Chunks []struct { Annotations source.ChunkAnnotations `json:"annotations"` PlanAnnotations source.ChunkAnnotations `json:"plan_annotations"` Content struct { ContentBase64 string `json:"content_base64"` ContentDigest string `json:"content_digest"` } `json:"content"` } `json:"chunks"` } readJSON(t, filepath.Join(root, "chunk", "chunks.json"), &chunkPayload) if len(chunkPayload.Chunks) != 1 { t.Fatalf("checkpoint chunks = %#v, want one", chunkPayload.Chunks) } decoded, err := base64.StdEncoding.DecodeString(chunkPayload.Chunks[0].Content.ContentBase64) if err != nil { t.Fatalf("decode chunk content: %v", err) } if string(decoded) != "chunk content" { t.Fatalf("chunk content = %q, want original content", decoded) } if got, want := chunkPayload.Chunks[0].Content.ContentDigest, contentDigest([]byte("chunk content")); got != want { t.Fatalf("content digest = %q, want %q", got, want) } if !jsonEqual(t, chunkPayload.Chunks[0].Annotations["same"], json.RawMessage(`{"range":1}`)) || !jsonEqual(t, chunkPayload.Chunks[0].PlanAnnotations["same"], json.RawMessage(`{"plan":2}`)) { t.Fatalf("checkpoint annotations = %s / %s", chunkPayload.Chunks[0].Annotations["same"], chunkPayload.Chunks[0].PlanAnnotations["same"]) } loaded, decision := (&WorkspaceLoader{root: root}).Chunk("generic", doc.Digest) if !decision.Reused || len(loaded.Chunks) != 1 { t.Fatalf("loaded chunk checkpoint = %#v, decision = %#v", loaded, decision) } if string(loaded.Chunks[0].Annotations["same"]) != `{"range":1}` || string(loaded.Chunks[0].PlanAnnotations["same"]) != `{"plan":2}` { t.Fatalf("loaded canonical annotations = %s / %s", loaded.Chunks[0].Annotations["same"], loaded.Chunks[0].PlanAnnotations["same"]) } } func jsonEqual(t *testing.T, left, right []byte) bool { t.Helper() var leftValue, rightValue any if err := json.Unmarshal(left, &leftValue); err != nil { t.Fatalf("decode left JSON: %v", err) } if err := json.Unmarshal(right, &rightValue); err != nil { t.Fatalf("decode right JSON: %v", err) } return reflect.DeepEqual(leftValue, rightValue) } 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("dependency mismatch", func(t *testing.T) { root := t.TempDir() recorder := newTestRecorder(t, root) chunks := []source.Chunk{{ ID: "chunk-1", SourceID: "source-1", Content: []byte("chunk content"), MediaType: "text/plain", }} if err := recorder.ChunkSucceeded("generic", "sha256:source-a", chunks, nil); err != nil { t.Fatalf("ChunkSucceeded: %v", err) } loader := &WorkspaceLoader{root: root} if _, decision := loader.Chunk("generic", "sha256:source-b"); decision.Reused || !strings.Contains(decision.Reason, "dependency") { t.Fatalf("decision = %#v, want dependency 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") } }) t.Run("corrupt payload", func(t *testing.T) { root := t.TempDir() recorder := newTestRecorder(t, root) chunks := []source.Chunk{{ ID: "chunk-1", SourceID: "source-1", Content: []byte("chunk content"), MediaType: "text/plain", }} if err := recorder.ChunkSucceeded("generic", "sha256:source", chunks, nil); err != nil { t.Fatalf("ChunkSucceeded: %v", err) } payloadPath := filepath.Join(root, "chunk", "chunks.json") data := strings.ReplaceAll(string(readFile(t, payloadPath)), contentDigest([]byte("chunk content")), "sha256:bad") if err := os.WriteFile(payloadPath, []byte(data), 0o644); err != nil { t.Fatalf("corrupt chunk payload: %v", err) } loader := &WorkspaceLoader{root: root} if _, decision := loader.Chunk("generic", "sha256:source"); decision.Reused || !strings.Contains(decision.Reason, "invalid") { t.Fatalf("decision = %#v, want corrupt payload invalidation", decision) } }) } 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) }