Files
notarius/internal/modules/generic/output/json/encoder_test.go

626 lines
24 KiB
Go
Raw Normal View History

package json
import (
"context"
stdjson "encoding/json"
"reflect"
"strings"
"testing"
2026-07-23 15:02:01 +00:00
"gitea.maximumdirect.net/eric/notarius/internal/core/source"
"gitea.maximumdirect.net/eric/notarius/internal/framework/chunkmap"
"gitea.maximumdirect.net/eric/notarius/internal/framework/contracts"
"gitea.maximumdirect.net/eric/notarius/internal/framework/evidencecontext"
"gitea.maximumdirect.net/eric/notarius/internal/framework/pipeline"
)
func TestModuleSpecAndRegister(t *testing.T) {
want := pipeline.ModuleSpec{
Key: Key,
Stage: pipeline.StageOutput,
ExecutionClass: contracts.ExecutionClassDeterministic,
Requires: []string{"normalized"},
Provides: []string{"encoded"},
}
if got := ModuleSpec(); !reflect.DeepEqual(got, want) {
t.Fatalf("ModuleSpec() = %#v, want %#v", got, want)
}
registry := pipeline.NewOutputEncoderRegistry()
if err := Register(registry); err != nil {
t.Fatalf("Register() error = %v, want nil", err)
}
spec, ok := registry.Spec(Key)
if !ok {
t.Fatalf("Spec(%q) ok = false, want true", Key)
}
if !reflect.DeepEqual(spec, want) {
t.Fatalf("registered spec = %#v, want %#v", spec, want)
}
if err := registry.ValidateOptions(Key, map[string]any{"unexpected": true}); err == nil || !strings.Contains(err.Error(), "unknown option") {
t.Fatalf("ValidateOptions() error = %v, want unknown option error", err)
}
2026-07-23 15:02:01 +00:00
if err := registry.ValidateOptions(Key, map[string]any{"include_chunk_map": true}); err != nil {
t.Fatalf("ValidateOptions() error = %v, want nil", err)
}
}
func TestDecodeOptions(t *testing.T) {
for _, test := range []struct {
name string
options map[string]any
want Options
wantErr string
}{
{name: "omitted", want: Options{}},
{name: "disabled", options: map[string]any{"include_chunk_map": false}, want: Options{}},
{name: "enabled", options: map[string]any{"include_chunk_map": true}, want: Options{IncludeChunkMap: true}},
{name: "wrong type", options: map[string]any{"include_chunk_map": "true"}, wantErr: "must be a boolean"},
{name: "unknown", options: map[string]any{"unknown": true}, wantErr: "unknown option"},
} {
t.Run(test.name, func(t *testing.T) {
got, err := DecodeOptions(test.options)
if test.wantErr != "" {
if err == nil || !strings.Contains(err.Error(), test.wantErr) {
t.Fatalf("DecodeOptions() error = %v, want %q", err, test.wantErr)
}
return
}
if err != nil {
t.Fatalf("DecodeOptions() error = %v, want nil", err)
}
if !reflect.DeepEqual(got, test.want) {
2026-07-23 15:02:01 +00:00
t.Fatalf("DecodeOptions() = %#v, want %#v", got, test.want)
}
})
}
}
func TestDecodeEvidenceContextOptions(t *testing.T) {
for _, test := range []struct {
name string
options map[string]any
want pipeline.EvidenceContextPolicy
wantErr string
}{
{name: "disabled", options: map[string]any{"evidence_context": map[string]any{"enabled": false}}},
{name: "enabled default window", options: map[string]any{"evidence_context": map[string]any{"enabled": true, "lanes": []any{"npcs"}}}, want: pipeline.EvidenceContextPolicy{Enabled: true, WindowUnits: 3, LaneIDs: []string{"npcs"}}},
{name: "explicit zero window and normalized lanes", options: map[string]any{"evidence_context": map[string]any{"enabled": true, "lanes": []any{" spells ", "npcs"}, "window_units": 0}}, want: pipeline.EvidenceContextPolicy{Enabled: true, WindowUnits: 0, LaneIDs: []string{"npcs", "spells"}}},
{name: "ConfigKit window integer", options: map[string]any{"evidence_context": map[string]any{"enabled": true, "lanes": []any{"npcs"}, "window_units": int64(2)}}, want: pipeline.EvidenceContextPolicy{Enabled: true, WindowUnits: 2, LaneIDs: []string{"npcs"}}},
{name: "duplicate lanes", options: map[string]any{"evidence_context": map[string]any{"enabled": true, "lanes": []any{"npcs", " npcs "}}}, wantErr: "duplicated"},
{name: "unknown nested option", options: map[string]any{"evidence_context": map[string]any{"enabled": false, "extra": true}}, wantErr: "unknown option"},
{name: "disabled nested fields", options: map[string]any{"evidence_context": map[string]any{"enabled": false, "lanes": []any{"npcs"}}}, wantErr: "not allowed"},
{name: "invalid object", options: map[string]any{"evidence_context": true}, wantErr: "must be an object"},
{name: "invalid lane type", options: map[string]any{"evidence_context": map[string]any{"enabled": true, "lanes": "npcs"}}, wantErr: "must be an array"},
{name: "invalid window type", options: map[string]any{"evidence_context": map[string]any{"enabled": true, "lanes": []any{"npcs"}, "window_units": "3"}}, wantErr: "must be an integer"},
} {
t.Run(test.name, func(t *testing.T) {
got, err := DecodeOptions(test.options)
if test.wantErr != "" {
if err == nil || !strings.Contains(err.Error(), test.wantErr) {
t.Fatalf("DecodeOptions() error = %v, want %q", err, test.wantErr)
}
return
}
if err != nil {
t.Fatalf("DecodeOptions() error = %v, want nil", err)
}
if !reflect.DeepEqual(got.EvidenceContext, test.want) {
t.Fatalf("EvidenceContext = %#v, want %#v", got.EvidenceContext, test.want)
}
})
}
}
func TestProfileValidationRejectsUnknownEvidenceLaneAndCopiesInputs(t *testing.T) {
registry := pipeline.NewOutputEncoderRegistry()
if err := Register(registry); err != nil {
t.Fatal(err)
}
options := map[string]any{"evidence_context": map[string]any{"enabled": true, "lanes": []any{"npcs"}}}
if err := registry.ValidateProfileOptions(Key, pipeline.OutputProfileOptionContext{LaneIDs: []string{"npcs", "spells"}}, options); err != nil {
t.Fatalf("ValidateProfileOptions() error = %v, want nil", err)
}
if err := registry.ValidateProfileOptions(Key, pipeline.OutputProfileOptionContext{LaneIDs: []string{"spells"}}, options); err == nil || !strings.Contains(err.Error(), "not configured") {
t.Fatalf("ValidateProfileOptions() error = %v, want unknown lane failure", err)
}
}
func TestEncodeDeliverableSelectionAndArtifactCount(t *testing.T) {
chunkMap := acceptedChunkMapArtifact(t)
evidence := acceptedEvidenceContextArtifact(t)
request := contracts.OutputRequest{
NormalizeOutputs: []contracts.SerializedOutput{normalizeOutput("spells", `{"spell_casts":[]}`), normalizeOutput("notes/items", `{"items":[]}`)},
ChunkMap: &chunkMap, EvidenceContext: &evidence,
}
for _, test := range []struct {
name string
chunkMap, evidence bool
want []string
}{
{"lanes only", false, false, []string{"lanes/notes_items.json", "lanes/spells.json"}},
{"chunk map", true, false, []string{"chunk-map.json", "lanes/notes_items.json", "lanes/spells.json"}},
{"evidence", false, true, []string{"evidence-context.json", "lanes/notes_items.json", "lanes/spells.json"}},
{"both auxiliaries", true, true, []string{"chunk-map.json", "evidence-context.json", "lanes/notes_items.json", "lanes/spells.json"}},
} {
t.Run(test.name, func(t *testing.T) {
result, err := NewWithOptions(Options{IncludeChunkMap: test.chunkMap, EvidenceContext: pipeline.EvidenceContextPolicy{Enabled: test.evidence}}).Encode(context.Background(), request)
if err != nil {
t.Fatal(err)
}
if got := outputFileNames(result.Files); !reflect.DeepEqual(got, test.want) {
t.Fatalf("deliverable files=%v, want %v", got, test.want)
}
outcome := pipeline.RunOutput{OutputFiles: result.Files, FallbackCount: 1}
if outcome.ArtifactCount() != len(test.want) || outcome.FallbackCount != 1 {
t.Fatalf("outcome=%#v, want %d artifacts and one qualified fallback", outcome, len(test.want))
}
})
}
}
func TestEncodeEmptyOutputHasNoReportingSidecars(t *testing.T) {
result, err := New().Encode(context.Background(), contracts.OutputRequest{})
if err != nil || len(result.Files) != 0 {
t.Fatalf("empty output files=%v error=%v", outputFileNames(result.Files), err)
}
}
2026-07-07 18:58:23 +00:00
func TestEncodeReturnsLogicalFilesForNormalizedOutputs(t *testing.T) {
req := contracts.OutputRequest{
NormalizeOutputs: []contracts.SerializedOutput{
2026-07-07 18:58:23 +00:00
normalizeOutput("spells", `{"spell_casts":[{"spell":"Cure Wounds"}]}`),
normalizeOutput("notes/items", `{"items":[{"name":"Torch","metadata":{"context":"intrinsic domain data"}}]}`),
},
}
result, err := New().Encode(context.Background(), req)
if err != nil {
t.Fatalf("Encode() error = %v, want nil", err)
}
wantNames := []string{
2026-07-07 19:27:28 +00:00
"lanes/notes_items.json",
"lanes/spells.json",
}
if got := outputFileNames(result.Files); !reflect.DeepEqual(got, wantNames) {
t.Fatalf("file names = %#v, want %#v", got, wantNames)
}
2026-07-07 18:58:23 +00:00
for _, file := range result.Files {
if !strings.HasSuffix(string(file.Bytes), "\n") {
t.Fatalf("%s does not end with newline: %q", file.Name, string(file.Bytes))
}
2026-07-07 18:58:23 +00:00
if file.ContentType == contentTypeJSON && !stdjson.Valid(file.Bytes) {
t.Fatalf("%s has invalid JSON: %s", file.Name, file.Bytes)
}
}
2026-07-07 19:27:28 +00:00
spells := decodeObject(t, fileBytes(t, result.Files, "lanes/spells.json"))
2026-07-07 18:58:23 +00:00
spellCasts := spells["spell_casts"].([]any)
if spellCasts[0].(map[string]any)["spell"] != "Cure Wounds" {
t.Fatalf("spells output = %#v, want raw normalized content", spells)
}
items := decodeObject(t, fileBytes(t, result.Files, "lanes/notes_items.json"))
metadata := items["items"].([]any)[0].(map[string]any)["metadata"].(map[string]any)
if metadata["context"] != "intrinsic domain data" {
t.Fatalf("intrinsic artifact metadata was lost: %#v", items)
}
}
2026-07-23 15:02:01 +00:00
func TestEncodeChunkMapExportIsOptIn(t *testing.T) {
artifact := acceptedChunkMapArtifact(t)
request := contracts.OutputRequest{
ChunkMap: &artifact,
}
for _, test := range []struct {
name string
encoder *Encoder
}{
{name: "default", encoder: New()},
{name: "explicitly disabled", encoder: NewWithOptions(Options{IncludeChunkMap: false})},
} {
t.Run(test.name, func(t *testing.T) {
result, err := test.encoder.Encode(context.Background(), request)
if err != nil {
t.Fatalf("Encode() error = %v, want nil", err)
}
if got, want := outputFileNames(result.Files), []string{}; !reflect.DeepEqual(got, want) {
2026-07-23 15:02:01 +00:00
t.Fatalf("file names = %#v, want %#v", got, want)
}
})
}
}
func TestEncodeIncludesValidatedChunkMap(t *testing.T) {
artifact := acceptedChunkMapArtifact(t)
result, err := NewWithOptions(Options{IncludeChunkMap: true}).Encode(context.Background(), contracts.OutputRequest{
NormalizeOutputs: []contracts.SerializedOutput{
normalizeOutput("spells", `{"spell_casts":[]}`),
},
ChunkMap: &artifact,
})
if err != nil {
t.Fatalf("Encode() error = %v, want nil", err)
}
if got, want := outputFileNames(result.Files), []string{"chunk-map.json", "lanes/spells.json"}; !reflect.DeepEqual(got, want) {
2026-07-23 15:02:01 +00:00
t.Fatalf("file names = %#v, want %#v", got, want)
}
chunkMapFile := fileBytes(t, result.Files, chunkMapFileName)
if !stdjson.Valid(chunkMapFile) || !strings.HasSuffix(string(chunkMapFile), "\n") || !strings.Contains(string(chunkMapFile), "\n \"source_id\"") {
t.Fatalf("chunk map file = %q, want pretty valid newline-terminated JSON", chunkMapFile)
}
if _, err := chunkmap.New().Decode(chunkMapFile); err != nil {
t.Fatalf("Decode(chunk map file) error = %v, want nil", err)
}
}
func TestEncodePreservesChunkMapAnnotationNumbers(t *testing.T) {
artifact := acceptedChunkMapArtifactWithPlanAnnotation(t, stdjson.RawMessage(`{"decimal":1.0,"large":9007199254740993}`))
result, err := NewWithOptions(Options{IncludeChunkMap: true}).Encode(context.Background(), contracts.OutputRequest{
ChunkMap: &artifact,
})
if err != nil {
t.Fatalf("Encode() error = %v", err)
}
value, err := chunkmap.New().Decode(fileBytes(t, result.Files, chunkMapFileName))
if err != nil {
t.Fatalf("Decode(emitted chunk map) error = %v", err)
}
if got, want := string(value.PlanAnnotations["test/numbers"]), `{"decimal":1.0,"large":9007199254740993}`; got != want {
t.Fatalf("numeric annotation = %s, want %s", got, want)
}
}
2026-07-23 15:02:01 +00:00
func TestEncodeOmitsChunkMapWithoutAcceptedArtifact(t *testing.T) {
result, err := NewWithOptions(Options{IncludeChunkMap: true}).Encode(context.Background(), contracts.OutputRequest{})
2026-07-23 15:02:01 +00:00
if err != nil {
t.Fatalf("Encode() error = %v, want nil", err)
}
if got := outputFileNames(result.Files); containsString(got, chunkMapFileName) {
t.Fatalf("file names = %#v, want no chunk map", got)
}
}
func TestEncodeRejectsInvalidChunkMapArtifact(t *testing.T) {
artifact := acceptedChunkMapArtifact(t)
for _, test := range []struct {
name string
mutate func(*contracts.SerializedArtifact)
}{
{name: "kind", mutate: func(artifact *contracts.SerializedArtifact) { artifact.Kind = "other/chunk-map" }},
{name: "schema", mutate: func(artifact *contracts.SerializedArtifact) { artifact.Schema.Version = "v2" }},
{name: "media type", mutate: func(artifact *contracts.SerializedArtifact) { artifact.MediaType = "text/plain" }},
{name: "content", mutate: func(artifact *contracts.SerializedArtifact) { artifact.Content = []byte(`{}`) }},
} {
t.Run(test.name, func(t *testing.T) {
candidate := contracts.CloneSerializedArtifact(artifact)
test.mutate(&candidate)
_, err := NewWithOptions(Options{IncludeChunkMap: true}).Encode(context.Background(), contracts.OutputRequest{ChunkMap: &candidate})
if err == nil {
t.Fatal("Encode() error = nil, want invalid chunk map error")
}
})
}
}
func TestEncodeIncludesValidatedEvidenceContext(t *testing.T) {
artifact := acceptedEvidenceContextArtifact(t)
result, err := NewWithOptions(Options{EvidenceContext: pipeline.EvidenceContextPolicy{Enabled: true}}).Encode(context.Background(), contracts.OutputRequest{EvidenceContext: &artifact})
if err != nil {
t.Fatalf("Encode() error = %v", err)
}
if got := string(fileBytes(t, result.Files, evidenceContextFileName)); !strings.HasSuffix(got, "\n") || !stdjson.Valid([]byte(got)) {
t.Fatalf("evidence context file = %q, want pretty valid newline-terminated JSON", got)
}
value, err := evidencecontext.New().Decode(fileBytes(t, result.Files, evidenceContextFileName))
if err != nil {
t.Fatalf("Decode(evidence context file) error = %v", err)
}
if len(value) != 1 || value[0].ID != 7 || value[0].Text != "Source content retained only in the evidence artifact." {
t.Fatalf("evidence context = %#v, want the published source-unit array", value)
}
}
func TestEncodeRejectsInvalidEvidenceContextArtifactWithoutContentLeakage(t *testing.T) {
artifact := acceptedEvidenceContextArtifact(t)
for _, test := range []struct {
name string
mutate func(*contracts.SerializedArtifact)
}{
{name: "kind", mutate: func(artifact *contracts.SerializedArtifact) { artifact.Kind = "other/evidence" }},
{name: "schema identity", mutate: func(artifact *contracts.SerializedArtifact) { artifact.Schema.Version = "v2" }},
{name: "schema digest", mutate: func(artifact *contracts.SerializedArtifact) { artifact.Schema.JSONSchema[0] = '[' }},
{name: "media type", mutate: func(artifact *contracts.SerializedArtifact) { artifact.MediaType = "text/plain" }},
{name: "payload", mutate: func(artifact *contracts.SerializedArtifact) {
artifact.Content = []byte(`{"source_id":"secret transcript text"}`)
}},
} {
t.Run(test.name, func(t *testing.T) {
candidate := contracts.CloneSerializedArtifact(artifact)
test.mutate(&candidate)
_, err := NewWithOptions(Options{EvidenceContext: pipeline.EvidenceContextPolicy{Enabled: true}}).Encode(context.Background(), contracts.OutputRequest{EvidenceContext: &candidate})
if err == nil || err.Error() != "json output encoder: evidence context artifact is invalid" {
t.Fatalf("Encode() error = %v, want fixed evidence artifact error", err)
}
if strings.Contains(err.Error(), "secret transcript text") {
t.Fatalf("Encode() leaked artifact content: %v", err)
}
})
}
}
func TestEncodeOmitsEvidenceContextWhenArtifactIsAbsent(t *testing.T) {
result, err := New().Encode(context.Background(), contracts.OutputRequest{})
if err != nil {
t.Fatalf("Encode() error = %v", err)
}
if got := outputFileNames(result.Files); containsString(got, evidenceContextFileName) {
t.Fatalf("file names = %#v, want no evidence context", got)
}
}
2026-07-23 15:02:01 +00:00
func TestEncodeChunkMapDoesNotMutateRequest(t *testing.T) {
artifact := acceptedChunkMapArtifact(t)
artifact.Metadata = map[string]any{"owner": "caller"}
request := contracts.OutputRequest{ChunkMap: &artifact}
before := contracts.CloneSerializedArtifact(artifact)
result, err := NewWithOptions(Options{IncludeChunkMap: true}).Encode(context.Background(), request)
if err != nil {
t.Fatalf("Encode() error = %v, want nil", err)
}
if !reflect.DeepEqual(*request.ChunkMap, before) {
t.Fatalf("chunk map artifact mutated:\nbefore: %#v\nafter: %#v", before, *request.ChunkMap)
}
request.ChunkMap.Content[0] = '['
request.ChunkMap.Schema.JSONSchema[0] = '['
request.ChunkMap.Metadata["owner"] = "changed"
if _, err := chunkmap.New().Decode(fileBytes(t, result.Files, chunkMapFileName)); err != nil {
t.Fatalf("chunk map output changed after request mutation: %v", err)
}
}
func TestEncodePrettyPrintsJSON(t *testing.T) {
result, err := New().Encode(context.Background(), contracts.OutputRequest{NormalizeOutputs: []contracts.SerializedOutput{normalizeOutput("events", `{"value":1}`)}})
if err != nil {
t.Fatal(err)
2026-07-07 19:27:28 +00:00
}
content := string(fileBytes(t, result.Files, "lanes/events.json"))
if !strings.Contains(content, "\n \"value\": 1\n") {
t.Fatalf("artifact JSON=%q, want two-space indentation", content)
2026-07-07 19:27:28 +00:00
}
}
2026-07-07 18:58:23 +00:00
func TestEncodeRejectsLaneIDWithoutSafeFileName(t *testing.T) {
_, err := New().Encode(context.Background(), contracts.OutputRequest{
NormalizeOutputs: []contracts.SerializedOutput{normalizeOutput("///", `{"value":true}`)},
})
if err == nil {
2026-07-07 18:58:23 +00:00
t.Fatal("Encode() error = nil, want unsafe lane id error")
}
if !strings.Contains(err.Error(), "json output encoder") || !strings.Contains(err.Error(), "safe file name") {
t.Fatalf("Encode() error = %q, want safe file name context", err.Error())
}
}
func TestEncodeSanitizesParentPathSequences(t *testing.T) {
result, err := New().Encode(context.Background(), contracts.OutputRequest{
NormalizeOutputs: []contracts.SerializedOutput{normalizeOutput("dnd..spell.", `{"value":true}`)},
})
if err != nil {
t.Fatalf("Encode() error = %v, want nil", err)
}
2026-07-07 19:27:28 +00:00
if got := outputFileNames(result.Files); !containsString(got, "lanes/dnd__spell.json") {
2026-07-07 18:58:23 +00:00
t.Fatalf("file names = %#v, want sanitized output filename", got)
}
}
2026-07-07 19:27:28 +00:00
func TestEncodeRejectsInvalidJSONAndUnsupportedMediaTypes(t *testing.T) {
tests := []struct {
name string
output contracts.SerializedOutput
2026-07-07 19:27:28 +00:00
want string
}{
{
name: "invalid JSON",
output: normalizeOutput("spells", `{"spell_casts":[`),
want: "invalid JSON",
},
{
name: "unsupported media type",
output: func() contracts.SerializedOutput {
2026-07-07 19:27:28 +00:00
output := normalizeOutput("spells", `{"spell_casts":[]}`)
output.Artifact.MediaType = "text/plain"
2026-07-07 19:27:28 +00:00
return output
}(),
want: "unsupported media type",
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
_, err := New().Encode(context.Background(), contracts.OutputRequest{
NormalizeOutputs: []contracts.SerializedOutput{test.output},
2026-07-07 19:27:28 +00:00
})
if err == nil {
t.Fatal("Encode() error = nil, want error")
}
if !strings.Contains(err.Error(), test.want) {
t.Fatalf("Encode() error = %q, want %q", err.Error(), test.want)
}
})
}
}
func TestEncodeRejectsSanitizedFilenameCollisions(t *testing.T) {
_, err := New().Encode(context.Background(), contracts.OutputRequest{
NormalizeOutputs: []contracts.SerializedOutput{
2026-07-07 18:58:23 +00:00
normalizeOutput("a/b", `{"value":"slash"}`),
normalizeOutput("a?b", `{"value":"question"}`),
},
})
if err == nil {
t.Fatal("Encode() error = nil, want duplicate file error")
}
if !strings.Contains(err.Error(), "duplicate output file") {
t.Fatalf("Encode() error = %q, want duplicate file context", err.Error())
}
}
func TestEncodeDoesNotMutateInputs(t *testing.T) {
req := contracts.OutputRequest{
NormalizeOutputs: []contracts.SerializedOutput{
2026-07-07 18:58:23 +00:00
normalizeOutput("spells", `{"name":"original"}`),
},
}
before := mustMarshal(t, req)
result, err := New().Encode(context.Background(), req)
if err != nil {
t.Fatalf("Encode() error = %v, want nil", err)
}
after := mustMarshal(t, req)
if before != after {
t.Fatalf("request mutated:\nbefore: %s\nafter: %s", before, after)
}
req.NormalizeOutputs[0].Artifact.Content[0] = '['
req.NormalizeOutputs[0].Artifact.Metadata["name"] = "changed"
2026-07-07 19:27:28 +00:00
if !stdjson.Valid(fileBytes(t, result.Files, "lanes/spells.json")) {
2026-07-07 18:58:23 +00:00
t.Fatal("output changed after request mutation")
}
}
func normalizeOutput(laneID string, content string) contracts.SerializedOutput {
return contracts.SerializedOutput{
2026-07-07 18:58:23 +00:00
LaneID: laneID,
NormalizerKey: "noop",
SourceID: "source-1",
Artifact: contracts.SerializedArtifact{Schema: contracts.ArtifactSchema{
2026-07-07 18:58:23 +00:00
ID: "schema-id",
Name: "schema-name",
Version: "v1",
},
2026-07-07 18:58:23 +00:00
Content: []byte(content),
MediaType: contentTypeJSON,
Metadata: map[string]any{"name": laneID},
},
}
}
2026-07-23 15:02:01 +00:00
func acceptedChunkMapArtifact(t *testing.T) contracts.SerializedArtifact {
return acceptedChunkMapArtifactWithPlanAnnotation(t, nil)
}
func acceptedChunkMapArtifactWithPlanAnnotation(t *testing.T, annotation stdjson.RawMessage) contracts.SerializedArtifact {
2026-07-23 15:02:01 +00:00
t.Helper()
document := &source.SourceDocument{
ID: "source-1",
Kind: "text",
Format: "text/plain",
Units: []source.SourceUnit{{
ID: 1,
Kind: "text",
Text: "Accepted source content.",
Ref: source.SourceRef{SourceID: "source-1", StartUnitID: 1, EndUnitID: 1},
}},
}
digest, err := source.DigestDocument(document)
if err != nil {
t.Fatal(err)
}
document.Digest = digest
plan := source.ChunkPlan{SourceDigest: digest, Ranges: []source.ChunkRange{{StartUnitID: 1, EndUnitID: 1}}}
if annotation != nil {
plan.Annotations = source.ChunkAnnotations{"test/numbers": append(stdjson.RawMessage(nil), annotation...)}
}
2026-07-23 15:02:01 +00:00
chunks, err := source.MaterializeChunkPlan(document, plan)
if err != nil {
t.Fatal(err)
}
artifact, err := chunkmap.Serialize(chunkmap.BuildRequest{
Source: document, Plan: plan, Chunks: chunks, RequestedChunker: "generic/units",
Producer: chunkmap.Producer{InputModule: "test/input", ChunkModule: "generic/units"},
})
if err != nil {
t.Fatal(err)
}
return artifact
}
func acceptedEvidenceContextArtifact(t *testing.T) contracts.SerializedArtifact {
t.Helper()
document := &source.SourceDocument{
ID: "source-1",
Kind: "text",
Format: "text/plain",
Units: []source.SourceUnit{{
ID: 7,
Kind: "text",
Text: "Source content retained only in the evidence artifact.",
Ref: source.SourceRef{SourceID: "source-1", StartUnitID: 7, EndUnitID: 7},
}},
}
digest, err := source.DigestDocument(document)
if err != nil {
t.Fatal(err)
}
document.Digest = digest
artifact, err := evidencecontext.Serialize(evidencecontext.BuildRequest{
Source: document, WindowUnits: 3,
SourceRefs: []source.SourceRef{{SourceID: "source-1", StartUnitID: 7, EndUnitID: 7}},
})
if err != nil {
t.Fatal(err)
}
return artifact
}
func outputFileNames(files []contracts.OutputFile) []string {
names := make([]string, 0, len(files))
for _, file := range files {
names = append(names, file.Name)
}
return names
}
func containsString(values []string, want string) bool {
for _, value := range values {
if value == want {
return true
}
}
return false
}
func fileBytes(t *testing.T, files []contracts.OutputFile, name string) []byte {
t.Helper()
for _, file := range files {
if file.Name == name {
return file.Bytes
}
}
t.Fatalf("file %q not found in %#v", name, outputFileNames(files))
return nil
}
func decodeObject(t *testing.T, data []byte) map[string]any {
t.Helper()
var got map[string]any
if err := stdjson.Unmarshal(data, &got); err != nil {
t.Fatalf("Unmarshal() error = %v, want nil\n%s", err, data)
}
return got
}
func mustMarshal(t *testing.T, value any) string {
t.Helper()
data, err := stdjson.Marshal(value)
if err != nil {
t.Fatalf("Marshal() error = %v, want nil", err)
}
return string(data)
}