Add candidate artifact codec decoding
This commit is contained in:
@@ -22,6 +22,7 @@ const (
|
||||
var schemaAssets embed.FS
|
||||
|
||||
var _ contracts.ArtifactCodec[dnd.NPCOccurrenceList] = (*Codec)(nil)
|
||||
var _ contracts.CandidateArtifactCodec[dnd.NPCOccurrenceList] = (*Codec)(nil)
|
||||
|
||||
type Codec struct{}
|
||||
|
||||
|
||||
@@ -106,6 +106,39 @@ func TestCodecSupportsEmptyListAndPreservesCollectionPresenceInCandidates(t *tes
|
||||
}
|
||||
}
|
||||
|
||||
func TestCodecCandidateDecodeOwnsValues(t *testing.T) {
|
||||
codec := New()
|
||||
input := validList()
|
||||
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("Mira Thorn"))
|
||||
if nameOffset < 0 {
|
||||
t.Fatal("candidate JSON does not contain NPC name")
|
||||
}
|
||||
content[nameOffset] = 'X'
|
||||
if first.Occurrences[0].Name != "Mira Thorn" {
|
||||
t.Fatal("DecodeCandidate() retained input bytes")
|
||||
}
|
||||
first.Occurrences[0].Name = "changed"
|
||||
first.Occurrences[0].SourceRefs[0].SourceID = "changed"
|
||||
if input.Occurrences[0].Name != "Mira Thorn" || input.Occurrences[0].SourceRefs[0].SourceID != "session-alpha" {
|
||||
t.Fatal("DecodeCandidate() retained input values")
|
||||
}
|
||||
if second.Occurrences[0].Name != "Mira Thorn" || second.Occurrences[0].SourceRefs[0].SourceID != "session-alpha" {
|
||||
t.Fatal("DecodeCandidate() returned aliased values")
|
||||
}
|
||||
}
|
||||
|
||||
func TestCodecStrictlyRejectsMalformedUnknownAndTrailingJSON(t *testing.T) {
|
||||
validJSON := `{"occurrences":[{"npc_id":"npc:test-mira","name":"Mira Thorn","kind":"dialogue","source_refs":[{"source_id":"session","start_unit_id":1,"end_unit_id":1}]}]}`
|
||||
for _, test := range []struct{ name, raw, want string }{
|
||||
|
||||
Reference in New Issue
Block a user