Add candidate artifact codec decoding
This commit is contained in:
@@ -59,9 +59,13 @@ func TestCodecOwnsDurableSchemaAndExactType(t *testing.T) {
|
||||
func TestCodecCandidateEncodingMetadataAndStrictDecode(t *testing.T) {
|
||||
codec := New()
|
||||
candidate := dnd.ItemRegistry{Items: []dnd.Item{{Name: "Silver Key"}}}
|
||||
if content, err := codec.EncodeCandidate(candidate); err != nil || string(content) != `{"items":[{"id":"","name":"Silver Key","source_refs":null}]}` {
|
||||
content, err := codec.EncodeCandidate(candidate)
|
||||
if err != nil || string(content) != `{"items":[{"id":"","name":"Silver Key","source_refs":null}]}` {
|
||||
t.Fatalf("EncodeCandidate() = %s, %v", content, err)
|
||||
}
|
||||
if decoded, err := codec.DecodeCandidate(content); err != nil || !reflect.DeepEqual(decoded, candidate) {
|
||||
t.Fatalf("DecodeCandidate() = %#v, %v; want %#v", decoded, err, candidate)
|
||||
}
|
||||
if _, err := codec.Encode(candidate); err == nil || !strings.Contains(err.Error(), "item ID pattern") {
|
||||
t.Fatalf("Encode() error = %v, want durable validation", err)
|
||||
}
|
||||
@@ -80,6 +84,39 @@ func TestCodecCandidateEncodingMetadataAndStrictDecode(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestCodecCandidateDecodeOwnsValues(t *testing.T) {
|
||||
codec := New()
|
||||
input := validRegistry()
|
||||
content, err := codec.EncodeCandidate(input)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
first, err := codec.DecodeCandidate(content)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
second, err := codec.DecodeCandidate(content)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
nameOffset := bytes.Index(content, []byte("Silver Key"))
|
||||
if nameOffset < 0 {
|
||||
t.Fatal("candidate JSON does not contain item name")
|
||||
}
|
||||
content[nameOffset] = 'X'
|
||||
if first.Items[0].Name != "Silver Key" {
|
||||
t.Fatal("DecodeCandidate() retained input bytes")
|
||||
}
|
||||
first.Items[0].Name = "changed"
|
||||
first.Items[0].SourceRefs[0].SourceID = "changed"
|
||||
if input.Items[0].Name != "Silver Key" || input.Items[0].SourceRefs[0].SourceID != "session-alpha" {
|
||||
t.Fatal("DecodeCandidate() retained input values")
|
||||
}
|
||||
if second.Items[0].Name != "Silver Key" || second.Items[0].SourceRefs[0].SourceID != "session-alpha" {
|
||||
t.Fatal("DecodeCandidate() returned aliased values")
|
||||
}
|
||||
}
|
||||
|
||||
func TestCodecSchemaAndMetadataAreDefensive(t *testing.T) {
|
||||
codec := New()
|
||||
first := codec.Schema()
|
||||
|
||||
Reference in New Issue
Block a user