Add canonical chunk plan source model
This commit is contained in:
70
internal/framework/pipeline/chunk_annotations_test.go
Normal file
70
internal/framework/pipeline/chunk_annotations_test.go
Normal file
@@ -0,0 +1,70 @@
|
||||
package pipeline
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"gitea.maximumdirect.net/eric/notarius/internal/core/source"
|
||||
)
|
||||
|
||||
func TestChunkCanonicalizationAndClonePreserveAnnotationScopes(t *testing.T) {
|
||||
doc := validSourceDocument()
|
||||
chunk := source.Chunk{
|
||||
ID: "chunk-1", SourceID: doc.ID, Index: 0,
|
||||
Ref: doc.Units[0].Ref, Content: []byte(`{"units":[1]}`), MediaType: "application/json", Units: doc.Units[:1],
|
||||
Annotations: source.ChunkAnnotations{"shared": json.RawMessage(` {"range": true} `)},
|
||||
PlanAnnotations: source.ChunkAnnotations{"shared": json.RawMessage(` {"plan": true} `)},
|
||||
}
|
||||
canonical, err := validateAndCanonicalizeChunkResult(doc, []source.Chunk{chunk})
|
||||
if err != nil {
|
||||
t.Fatalf("validateAndCanonicalizeChunkResult() error = %v", err)
|
||||
}
|
||||
if got := string(canonical[0].Annotations["shared"]); got != `{"range":true}` {
|
||||
t.Fatalf("range annotation = %q", got)
|
||||
}
|
||||
if got := string(canonical[0].PlanAnnotations["shared"]); got != `{"plan":true}` {
|
||||
t.Fatalf("plan annotation = %q", got)
|
||||
}
|
||||
|
||||
cloned := cloneSourceChunk(canonical[0])
|
||||
cloned.Annotations["shared"][0] = '['
|
||||
cloned.PlanAnnotations["shared"][0] = '['
|
||||
if string(canonical[0].Annotations["shared"]) != `{"range":true}` || string(canonical[0].PlanAnnotations["shared"]) != `{"plan":true}` {
|
||||
t.Fatal("cloneSourceChunk() shares annotation bytes")
|
||||
}
|
||||
}
|
||||
|
||||
func TestSerializedChunkValidationRepresentationIncludesAnnotationScopes(t *testing.T) {
|
||||
doc := validSourceDocument()
|
||||
chunks := []source.Chunk{{
|
||||
ID: "chunk-1", SourceID: doc.ID, Ref: doc.Units[0].Ref, MediaType: "application/json", Units: doc.Units[:1],
|
||||
Annotations: source.ChunkAnnotations{"same": json.RawMessage(`{"range":1}`)},
|
||||
PlanAnnotations: source.ChunkAnnotations{"same": json.RawMessage(`{"plan":2}`)},
|
||||
}}
|
||||
encoded, err := json.Marshal(chunks)
|
||||
if err != nil {
|
||||
t.Fatalf("json.Marshal(chunks) error = %v", err)
|
||||
}
|
||||
got := string(encoded)
|
||||
if !strings.Contains(got, `"annotations":{"same":{"range":1}}`) || !strings.Contains(got, `"plan_annotations":{"same":{"plan":2}}`) {
|
||||
t.Fatalf("serialized chunks = %s, want both annotation scopes", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestDebugChunkEnvelopeClonesAnnotationScopes(t *testing.T) {
|
||||
chunk := source.Chunk{
|
||||
ID: "chunk-1", SourceID: "source-1",
|
||||
Annotations: source.ChunkAnnotations{"same": json.RawMessage(`{"range":1}`)},
|
||||
PlanAnnotations: source.ChunkAnnotations{"same": json.RawMessage(`{"plan":2}`)},
|
||||
}
|
||||
envelope := debugSourceChunkEnvelope(chunk)
|
||||
if string(envelope.Annotations["same"]) != `{"range":1}` || string(envelope.PlanAnnotations["same"]) != `{"plan":2}` {
|
||||
t.Fatalf("debug annotations = %#v / %#v", envelope.Annotations, envelope.PlanAnnotations)
|
||||
}
|
||||
envelope.Annotations["same"][0] = '['
|
||||
envelope.PlanAnnotations["same"][0] = '['
|
||||
if string(chunk.Annotations["same"]) != `{"range":1}` || string(chunk.PlanAnnotations["same"]) != `{"plan":2}` {
|
||||
t.Fatal("debugSourceChunkEnvelope() shares annotation bytes")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user