288 lines
13 KiB
Go
288 lines
13 KiB
Go
package combatturns
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/json"
|
|
"os"
|
|
"reflect"
|
|
"strings"
|
|
"testing"
|
|
|
|
"gitea.maximumdirect.net/eric/notarius/internal/core/source"
|
|
"gitea.maximumdirect.net/eric/notarius/internal/framework/contracts"
|
|
"gitea.maximumdirect.net/eric/notarius/internal/framework/pipeline"
|
|
"gitea.maximumdirect.net/eric/notarius/internal/modules/dnd"
|
|
)
|
|
|
|
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.",
|
|
SourceRefs: []source.SourceRef{{SourceID: "session-alpha", StartUnitID: 1, EndUnitID: 2}},
|
|
}}}
|
|
}
|
|
|
|
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)
|
|
}
|
|
codec := New()
|
|
value, err := codec.Decode(raw)
|
|
if err != nil {
|
|
t.Fatalf("Decode() error = %v, want nil", 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)
|
|
}
|
|
var compact bytes.Buffer
|
|
if err := json.Compact(&compact, raw); err != nil {
|
|
t.Fatalf("compact durable fixture: %v", 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)
|
|
}
|
|
}
|
|
|
|
func TestCodecOwnsDurableSchemaAndRegistersExactType(t *testing.T) {
|
|
codec := New()
|
|
schema := codec.Schema()
|
|
if codec.Kind() != dnd.CombatTurnListKind || codec.MediaType() != MediaType {
|
|
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)
|
|
}
|
|
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)
|
|
}
|
|
spec, ok := registry.Spec(dnd.CombatTurnListKind)
|
|
if !ok || spec.SchemaDigest != contracts.DigestArtifactSchema(schema) {
|
|
t.Fatalf("registered spec = %#v, %t", spec, ok)
|
|
}
|
|
}
|
|
|
|
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"},
|
|
}
|
|
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) {
|
|
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)
|
|
}
|
|
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) {
|
|
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"},
|
|
}
|
|
for _, test := range tests {
|
|
t.Run(test.name, func(t *testing.T) {
|
|
if _, err := New().Encode(test.value); err == nil || !strings.Contains(err.Error(), test.want) {
|
|
t.Fatalf("Encode() error = %v, want %q", err, test.want)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestCodecSchemaAndMetadataAreDefensive(t *testing.T) {
|
|
codec := New()
|
|
first := codec.Schema()
|
|
first.JSONSchema[0] = '['
|
|
second := codec.Schema()
|
|
if !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)
|
|
}
|
|
}
|
|
|
|
func mutate(value dnd.CombatTurnList, change func(*dnd.CombatTurnList)) dnd.CombatTurnList {
|
|
change(&value)
|
|
return value
|
|
}
|