Simplify D&D combat turn extraction contract
This commit is contained in:
@@ -15,19 +15,8 @@ import (
|
||||
)
|
||||
|
||||
func validList() dnd.CombatTurnList {
|
||||
round := 1
|
||||
resolution := "The wight is hit."
|
||||
return dnd.CombatTurnList{CombatTurns: []dnd.CombatTurn{{
|
||||
Actor: "Aria",
|
||||
TurnKind: dnd.CombatTurnKindTurn,
|
||||
Round: &round,
|
||||
Actions: []dnd.CombatAction{{
|
||||
Category: dnd.CombatActionCategoryAttack,
|
||||
Declaration: "Aria swings her sword",
|
||||
Targets: []string{"wight"},
|
||||
Resolution: &resolution,
|
||||
}},
|
||||
Summary: "Aria attacks the wight.",
|
||||
Actor: "Aria", TurnKind: dnd.CombatTurnKindTurn,
|
||||
SourceRefs: []source.SourceRef{{SourceID: "session-alpha", StartUnitID: 1, EndUnitID: 2}},
|
||||
}}}
|
||||
}
|
||||
@@ -35,30 +24,26 @@ func validList() dnd.CombatTurnList {
|
||||
func TestCodecMatchesMaintainedDurableFixture(t *testing.T) {
|
||||
raw, err := os.ReadFile("testdata/dnd_combat_turns.v1.json")
|
||||
if err != nil {
|
||||
t.Fatalf("read durable fixture: %v", err)
|
||||
t.Fatal(err)
|
||||
}
|
||||
codec := New()
|
||||
value, err := codec.Decode(raw)
|
||||
if err != nil {
|
||||
t.Fatalf("Decode() error = %v, want nil", err)
|
||||
t.Fatalf("Decode() error = %v", err)
|
||||
}
|
||||
if want := validList(); !reflect.DeepEqual(value, want) {
|
||||
t.Fatalf("Decode() = %#v, want %#v", value, want)
|
||||
}
|
||||
encoded, err := codec.Encode(value)
|
||||
if err != nil {
|
||||
t.Fatalf("Encode() error = %v, want nil", err)
|
||||
t.Fatalf("Encode() error = %v", err)
|
||||
}
|
||||
var compact bytes.Buffer
|
||||
if err := json.Compact(&compact, raw); err != nil {
|
||||
t.Fatalf("compact durable fixture: %v", err)
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !bytes.Equal(encoded, compact.Bytes()) {
|
||||
t.Fatalf("Encode() = %s, want stable durable JSON %s", encoded, compact.Bytes())
|
||||
}
|
||||
second, err := codec.Encode(value)
|
||||
if err != nil || !bytes.Equal(second, encoded) {
|
||||
t.Fatalf("second Encode() = %s, %v; want deterministic bytes", second, err)
|
||||
t.Fatalf("Encode() = %s, want %s", encoded, compact.Bytes())
|
||||
}
|
||||
}
|
||||
|
||||
@@ -69,16 +54,11 @@ func TestCodecOwnsDurableSchemaAndRegistersExactType(t *testing.T) {
|
||||
t.Fatalf("codec identity = %q/%q", codec.Kind(), codec.MediaType())
|
||||
}
|
||||
if schema.ID != SchemaID || schema.Name != SchemaName || schema.Version != SchemaVersion || !json.Valid(schema.JSONSchema) {
|
||||
t.Fatalf("schema = %#v, want durable combat-turn schema", schema)
|
||||
t.Fatalf("schema = %#v", schema)
|
||||
}
|
||||
var document map[string]any
|
||||
if err := json.Unmarshal(schema.JSONSchema, &document); err != nil || document["$id"] != SchemaID {
|
||||
t.Fatalf("durable schema document = %#v, %v", document, err)
|
||||
}
|
||||
|
||||
registry := pipeline.NewArtifactCodecRegistry()
|
||||
if err := pipeline.RegisterArtifactCodec(registry, codec); err != nil {
|
||||
t.Fatalf("RegisterArtifactCodec() error = %v", err)
|
||||
t.Fatal(err)
|
||||
}
|
||||
spec, ok := registry.Spec(dnd.CombatTurnListKind)
|
||||
if !ok || spec.SchemaDigest != contracts.DigestArtifactSchema(schema) {
|
||||
@@ -86,176 +66,59 @@ func TestCodecOwnsDurableSchemaAndRegistersExactType(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestCodecStrictlyRejectsMalformedOrUnknownJSON(t *testing.T) {
|
||||
codec := New()
|
||||
validJSON := `{"combat_turns":[{"actor":"Aria","turn_kind":"turn","round":1,"actions":[{"category":"attack","declaration":"swings","targets":["wight"],"resolution":"hits"}],"summary":"attack","source_refs":[{"source_id":"session","start_unit_id":1,"end_unit_id":1}]}]}`
|
||||
tests := []struct {
|
||||
name string
|
||||
raw string
|
||||
want string
|
||||
}{
|
||||
{name: "malformed", raw: `{`, want: "decode dnd combat turn list"},
|
||||
{name: "unknown top-level", raw: `{"combat_turns":[],"unexpected":true}`, want: "unknown field"},
|
||||
{name: "unknown turn field", raw: strings.Replace(validJSON, `"summary":"attack"`, `"summary":"attack","unexpected":true`, 1), want: "unknown field"},
|
||||
{name: "unknown action field", raw: strings.Replace(validJSON, `"resolution":"hits"`, `"resolution":"hits","unexpected":true`, 1), want: "unknown field"},
|
||||
{name: "unknown source reference field", raw: strings.Replace(validJSON, `"end_unit_id":1`, `"end_unit_id":1,"unexpected":true`, 1), want: "unknown field"},
|
||||
{name: "trailing", raw: `{"combat_turns":[]} {}`, want: "multiple JSON values"},
|
||||
{name: "missing top-level array", raw: `{}`, want: "combat_turns must be present"},
|
||||
{name: "missing round", raw: strings.Replace(validJSON, `,"round":1`, ``, 1), want: "round must be present"},
|
||||
{name: "missing resolution", raw: strings.Replace(validJSON, `,"resolution":"hits"`, ``, 1), want: "resolution must be present"},
|
||||
{name: "invalid round type", raw: strings.Replace(validJSON, `"round":1`, `"round":1.5`, 1), want: "round must be an integer or null"},
|
||||
{name: "unsupported turn kind", raw: strings.Replace(validJSON, `"turn_kind":"turn"`, `"turn_kind":"unsupported"`, 1), want: "turn_kind must be supported"},
|
||||
{name: "empty actions", raw: strings.Replace(validJSON, `"actions":[{"category":"attack","declaration":"swings","targets":["wight"],"resolution":"hits"}]`, `"actions":[]`, 1), want: "actions must contain at least one action"},
|
||||
{name: "empty target", raw: strings.Replace(validJSON, `"targets":["wight"]`, `"targets":[" "]`, 1), want: "targets[0] must not be empty"},
|
||||
func TestCodecStrictlyRejectsMalformedUnknownAndInvalidJSON(t *testing.T) {
|
||||
validJSON := `{"combat_turns":[{"actor":"Aria","turn_kind":"turn","source_refs":[{"source_id":"session","start_unit_id":1,"end_unit_id":1}]}]}`
|
||||
tests := []struct{ name, raw, want string }{
|
||||
{"malformed", `{`, "decode dnd combat turn list"},
|
||||
{"unknown top-level", `{"combat_turns":[],"unexpected":true}`, "unknown field"},
|
||||
{"unknown turn field", strings.Replace(validJSON, `"turn_kind":"turn"`, `"turn_kind":"turn","unexpected":true`, 1), "unknown field"},
|
||||
{"unknown source field", strings.Replace(validJSON, `"end_unit_id":1`, `"end_unit_id":1,"unexpected":true`, 1), "unknown field"},
|
||||
{"trailing", `{"combat_turns":[]} {}`, "multiple JSON values"},
|
||||
{"missing list", `{}`, "combat_turns must be present"},
|
||||
{"unsupported kind", strings.Replace(validJSON, `"turn_kind":"turn"`, `"turn_kind":"unsupported"`, 1), "turn_kind must be supported"},
|
||||
}
|
||||
for _, test := range tests {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
if _, err := codec.Decode([]byte(test.raw)); err == nil || !strings.Contains(err.Error(), test.want) {
|
||||
if _, err := New().Decode([]byte(test.raw)); err == nil || !strings.Contains(err.Error(), test.want) {
|
||||
t.Fatalf("Decode() error = %v, want %q", err, test.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestCodecCandidatePreservesInvalidTypedValues(t *testing.T) {
|
||||
round := -1
|
||||
resolution := " "
|
||||
candidate := dnd.CombatTurnList{CombatTurns: []dnd.CombatTurn{{
|
||||
Actor: " ",
|
||||
TurnKind: "unsupported",
|
||||
Round: &round,
|
||||
Actions: []dnd.CombatAction{{
|
||||
Category: "unsupported",
|
||||
Declaration: " ",
|
||||
Targets: nil,
|
||||
Resolution: &resolution,
|
||||
}},
|
||||
Summary: " ",
|
||||
SourceRefs: []source.SourceRef{{
|
||||
SourceID: "",
|
||||
StartUnitID: 0,
|
||||
EndUnitID: -1,
|
||||
}},
|
||||
}}}
|
||||
|
||||
content, err := New().EncodeCandidate(candidate)
|
||||
if err != nil || !json.Valid(content) {
|
||||
t.Fatalf("EncodeCandidate() = %s, %v; want JSON", content, err)
|
||||
func TestCodecCandidatePreservesValidatorOwnedValuesAndCollectionPresence(t *testing.T) {
|
||||
candidates := []dnd.CombatTurnList{
|
||||
{},
|
||||
{CombatTurns: []dnd.CombatTurn{}},
|
||||
{CombatTurns: []dnd.CombatTurn{{Actor: " ", TurnKind: "unsupported", SourceRefs: nil}}},
|
||||
{CombatTurns: []dnd.CombatTurn{{Actor: " ", TurnKind: "unsupported", SourceRefs: []source.SourceRef{}}}},
|
||||
{CombatTurns: []dnd.CombatTurn{{Actor: " ", TurnKind: "unsupported", SourceRefs: []source.SourceRef{{StartUnitID: 0, EndUnitID: -1}}}}},
|
||||
}
|
||||
decoded, err := New().DecodeCandidate(content)
|
||||
if err != nil || !reflect.DeepEqual(decoded, candidate) {
|
||||
t.Fatalf("DecodeCandidate() = %#v, %v; want %#v", decoded, err, candidate)
|
||||
for _, candidate := range candidates {
|
||||
content, err := New().EncodeCandidate(candidate)
|
||||
if err != nil || !json.Valid(content) {
|
||||
t.Fatalf("EncodeCandidate() = %s, %v", content, err)
|
||||
}
|
||||
decoded, err := New().DecodeCandidate(content)
|
||||
if err != nil || !reflect.DeepEqual(decoded, candidate) {
|
||||
t.Fatalf("DecodeCandidate() = %#v, %v; want %#v", decoded, err, candidate)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestCodecCandidatePreservesNilAndPresentEmptyArrays(t *testing.T) {
|
||||
codec := New()
|
||||
for name, value := range map[string]dnd.CombatTurnList{
|
||||
"nil combat turns": {},
|
||||
"empty combat turns": {CombatTurns: []dnd.CombatTurn{}},
|
||||
} {
|
||||
t.Run(name, func(t *testing.T) {
|
||||
content, err := codec.EncodeCandidate(value)
|
||||
if err != nil {
|
||||
t.Fatalf("EncodeCandidate() error = %v", err)
|
||||
}
|
||||
decoded, err := codec.DecodeCandidate(content)
|
||||
if err != nil || !reflect.DeepEqual(decoded, value) {
|
||||
t.Fatalf("DecodeCandidate() = %#v, %v; want %#v", decoded, err, value)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
withoutTargets := validList()
|
||||
withoutTargets.CombatTurns[0].Actions[0].Targets = nil
|
||||
withEmptyTargets := validList()
|
||||
withEmptyTargets.CombatTurns[0].Actions[0].Targets = []string{}
|
||||
for name, value := range map[string]dnd.CombatTurnList{
|
||||
"nil targets": withoutTargets,
|
||||
"empty targets": withEmptyTargets,
|
||||
} {
|
||||
t.Run(name, func(t *testing.T) {
|
||||
content, err := codec.EncodeCandidate(value)
|
||||
if err != nil {
|
||||
t.Fatalf("EncodeCandidate() error = %v", err)
|
||||
}
|
||||
decoded, err := codec.DecodeCandidate(content)
|
||||
if err != nil || !reflect.DeepEqual(decoded, value) {
|
||||
t.Fatalf("DecodeCandidate() = %#v, %v; want %#v", decoded, err, value)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
withoutActions := validList()
|
||||
withoutActions.CombatTurns[0].Actions = nil
|
||||
withEmptyActions := validList()
|
||||
withEmptyActions.CombatTurns[0].Actions = []dnd.CombatAction{}
|
||||
withoutSourceRefs := validList()
|
||||
withoutSourceRefs.CombatTurns[0].SourceRefs = nil
|
||||
withEmptySourceRefs := validList()
|
||||
withEmptySourceRefs.CombatTurns[0].SourceRefs = []source.SourceRef{}
|
||||
for name, value := range map[string]dnd.CombatTurnList{
|
||||
"nil actions": withoutActions,
|
||||
"empty actions": withEmptyActions,
|
||||
"nil source refs": withoutSourceRefs,
|
||||
"empty source refs": withEmptySourceRefs,
|
||||
} {
|
||||
t.Run(name, func(t *testing.T) {
|
||||
content, err := codec.EncodeCandidate(value)
|
||||
if err != nil {
|
||||
t.Fatalf("EncodeCandidate() error = %v", err)
|
||||
}
|
||||
decoded, err := codec.DecodeCandidate(content)
|
||||
if err != nil || !reflect.DeepEqual(decoded, value) {
|
||||
t.Fatalf("DecodeCandidate() = %#v, %v; want %#v", decoded, err, value)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestCodecAcceptsExplicitNullableFieldsAndEmptyTargets(t *testing.T) {
|
||||
value := validList()
|
||||
value.CombatTurns[0].Round = nil
|
||||
value.CombatTurns[0].Actions[0].Resolution = nil
|
||||
value.CombatTurns[0].Actions[0].Targets = []string{}
|
||||
|
||||
codec := New()
|
||||
content, err := codec.Encode(value)
|
||||
if err != nil {
|
||||
t.Fatalf("Encode() error = %v, want nil for explicit nullable fields", err)
|
||||
}
|
||||
decoded, err := codec.Decode(content)
|
||||
if err != nil || !reflect.DeepEqual(decoded, value) {
|
||||
t.Fatalf("Decode() = %#v, %v; want %#v", decoded, err, value)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCodecRejectsEveryRequiredShapeBoundary(t *testing.T) {
|
||||
func TestCodecRejectsRequiredShapeAndReferenceBoundaries(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
value dnd.CombatTurnList
|
||||
want string
|
||||
}{
|
||||
{name: "nil combat turns", value: dnd.CombatTurnList{}, want: "combat_turns must be present"},
|
||||
{name: "empty actor", value: mutate(validList(), func(value *dnd.CombatTurnList) { value.CombatTurns[0].Actor = " " }), want: "actor must not be empty"},
|
||||
{name: "unsupported turn kind", value: mutate(validList(), func(value *dnd.CombatTurnList) { value.CombatTurns[0].TurnKind = "unsupported" }), want: "turn_kind must be supported"},
|
||||
{name: "non-positive round", value: mutate(validList(), func(value *dnd.CombatTurnList) { round := 0; value.CombatTurns[0].Round = &round }), want: "round must be positive or null"},
|
||||
{name: "nil actions", value: mutate(validList(), func(value *dnd.CombatTurnList) { value.CombatTurns[0].Actions = nil }), want: "actions must contain at least one action"},
|
||||
{name: "empty actions", value: mutate(validList(), func(value *dnd.CombatTurnList) { value.CombatTurns[0].Actions = []dnd.CombatAction{} }), want: "actions must contain at least one action"},
|
||||
{name: "empty summary", value: mutate(validList(), func(value *dnd.CombatTurnList) { value.CombatTurns[0].Summary = " " }), want: "summary must not be empty"},
|
||||
{name: "nil source refs", value: mutate(validList(), func(value *dnd.CombatTurnList) { value.CombatTurns[0].SourceRefs = nil }), want: "source_refs must contain at least one reference"},
|
||||
{name: "empty source refs", value: mutate(validList(), func(value *dnd.CombatTurnList) { value.CombatTurns[0].SourceRefs = []source.SourceRef{} }), want: "source_refs must contain at least one reference"},
|
||||
{name: "unsupported action category", value: mutate(validList(), func(value *dnd.CombatTurnList) { value.CombatTurns[0].Actions[0].Category = "unsupported" }), want: "category must be supported"},
|
||||
{name: "empty declaration", value: mutate(validList(), func(value *dnd.CombatTurnList) { value.CombatTurns[0].Actions[0].Declaration = " " }), want: "declaration must not be empty"},
|
||||
{name: "nil targets", value: mutate(validList(), func(value *dnd.CombatTurnList) { value.CombatTurns[0].Actions[0].Targets = nil }), want: "targets must be present"},
|
||||
{name: "empty target", value: mutate(validList(), func(value *dnd.CombatTurnList) { value.CombatTurns[0].Actions[0].Targets = []string{" "} }), want: "targets[0] must not be empty"},
|
||||
{name: "empty resolution", value: mutate(validList(), func(value *dnd.CombatTurnList) {
|
||||
resolution := " "
|
||||
value.CombatTurns[0].Actions[0].Resolution = &resolution
|
||||
}), want: "resolution must not be empty or null"},
|
||||
{name: "empty source ID", value: mutate(validList(), func(value *dnd.CombatTurnList) { value.CombatTurns[0].SourceRefs[0].SourceID = " " }), want: "source_id must not be empty"},
|
||||
{name: "non-positive source start", value: mutate(validList(), func(value *dnd.CombatTurnList) { value.CombatTurns[0].SourceRefs[0].StartUnitID = 0 }), want: "start_unit_id must be positive"},
|
||||
{name: "non-positive source end", value: mutate(validList(), func(value *dnd.CombatTurnList) { value.CombatTurns[0].SourceRefs[0].EndUnitID = 0 }), want: "end_unit_id must be positive"},
|
||||
{"nil combat turns", dnd.CombatTurnList{}, "combat_turns must be present"},
|
||||
{"empty actor", mutate(validList(), func(v *dnd.CombatTurnList) { v.CombatTurns[0].Actor = " " }), "actor must not be empty"},
|
||||
{"unsupported turn kind", mutate(validList(), func(v *dnd.CombatTurnList) { v.CombatTurns[0].TurnKind = "unsupported" }), "turn_kind must be supported"},
|
||||
{"nil source refs", mutate(validList(), func(v *dnd.CombatTurnList) { v.CombatTurns[0].SourceRefs = nil }), "source_refs must contain"},
|
||||
{"empty source ID", mutate(validList(), func(v *dnd.CombatTurnList) { v.CombatTurns[0].SourceRefs[0].SourceID = " " }), "source_id must not be empty"},
|
||||
{"non-positive start", mutate(validList(), func(v *dnd.CombatTurnList) { v.CombatTurns[0].SourceRefs[0].StartUnitID = 0 }), "start_unit_id must be positive"},
|
||||
{"non-positive end", mutate(validList(), func(v *dnd.CombatTurnList) { v.CombatTurns[0].SourceRefs[0].EndUnitID = 0 }), "end_unit_id must be positive"},
|
||||
}
|
||||
for _, test := range tests {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
@@ -270,14 +133,13 @@ func TestCodecSchemaAndMetadataAreDefensive(t *testing.T) {
|
||||
codec := New()
|
||||
first := codec.Schema()
|
||||
first.JSONSchema[0] = '['
|
||||
second := codec.Schema()
|
||||
if !json.Valid(second.JSONSchema) || second.JSONSchema[0] == '[' {
|
||||
if second := codec.Schema(); !json.Valid(second.JSONSchema) || second.JSONSchema[0] == '[' {
|
||||
t.Fatal("Schema() returned shared bytes")
|
||||
}
|
||||
metadata := codec.Metadata(validList())
|
||||
metadata["other"] = true
|
||||
if next := codec.Metadata(validList()); len(next) != 1 || next["combat_turn_count"] != 1 {
|
||||
t.Fatalf("Metadata() = %#v, want only combat_turn_count", next)
|
||||
t.Fatalf("Metadata() = %#v", next)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user