Adopt registry-backed NPC occurrence artifacts

This commit is contained in:
2026-08-05 18:55:58 +00:00
parent 3f4a1f2647
commit 5e2ccffc0f
42 changed files with 676 additions and 528 deletions

View File

@@ -1,17 +1,21 @@
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"$id": "notarius.dnd.npc_interactions",
"$id": "notarius.dnd.npc_occurrences",
"type": "object",
"additionalProperties": false,
"required": ["interactions"],
"required": ["occurrences"],
"properties": {
"interactions": {
"occurrences": {
"type": "array",
"items": {
"type": "object",
"additionalProperties": false,
"required": ["name", "kind", "source_refs"],
"required": ["npc_id", "name", "kind", "source_refs"],
"properties": {
"npc_id": {
"type": "string",
"minLength": 1
},
"name": {
"type": "string",
"minLength": 1

View File

@@ -1,4 +1,4 @@
// Package npcinteractions encodes durable D&D NPC interaction artifacts.
// Package npcinteractions encodes durable D&D NPC occurrence artifacts.
package npcinteractions
import (
@@ -12,25 +12,25 @@ import (
)
const (
SchemaID = "notarius.dnd.npc_interactions"
SchemaName = "notarius_dnd_npc_interactions_v1"
SchemaID = "notarius.dnd.npc_occurrences"
SchemaName = "notarius_dnd_npc_occurrences_v1"
SchemaVersion = "v1"
MediaType = "application/json"
)
//go:embed assets/schemas/dnd_npc_interactions.v1.json
//go:embed assets/schemas/dnd_npc_occurrences.v1.json
var schemaAssets embed.FS
var _ contracts.ArtifactCodec[dnd.NPCInteractionList] = (*Codec)(nil)
var _ contracts.ArtifactCodec[dnd.NPCOccurrenceList] = (*Codec)(nil)
type Codec struct{}
func New() *Codec { return &Codec{} }
func (c *Codec) Kind() contracts.ArtifactKind { return dnd.NPCInteractionListKind }
func (c *Codec) Kind() contracts.ArtifactKind { return dnd.NPCOccurrenceListKind }
func (c *Codec) Schema() contracts.ArtifactSchema {
raw, err := schemaAssets.ReadFile("assets/schemas/dnd_npc_interactions.v1.json")
raw, err := schemaAssets.ReadFile("assets/schemas/dnd_npc_occurrences.v1.json")
if err != nil {
return contracts.ArtifactSchema{}
}
@@ -44,56 +44,59 @@ func (c *Codec) Schema() contracts.ArtifactSchema {
func (c *Codec) MediaType() string { return MediaType }
func (c *Codec) Metadata(value dnd.NPCInteractionList) map[string]any {
return map[string]any{"interaction_count": len(value.Interactions)}
func (c *Codec) Metadata(value dnd.NPCOccurrenceList) map[string]any {
return map[string]any{"occurrence_count": len(value.Occurrences)}
}
func (c *Codec) Encode(value dnd.NPCInteractionList) ([]byte, error) {
func (c *Codec) Encode(value dnd.NPCOccurrenceList) ([]byte, error) {
if err := validate(value); err != nil {
return nil, fmt.Errorf("encode dnd npc interaction list: %w", err)
return nil, fmt.Errorf("encode dnd npc occurrence list: %w", err)
}
return c.EncodeCandidate(value)
}
// EncodeCandidate provides the durable representation before semantic
// validators have approved a value.
func (c *Codec) EncodeCandidate(value dnd.NPCInteractionList) ([]byte, error) {
return candidatejson.EncodeCandidate("dnd npc interaction list", value)
func (c *Codec) EncodeCandidate(value dnd.NPCOccurrenceList) ([]byte, error) {
return candidatejson.EncodeCandidate("dnd npc occurrence list", value)
}
func (c *Codec) Decode(content []byte) (dnd.NPCInteractionList, error) {
func (c *Codec) Decode(content []byte) (dnd.NPCOccurrenceList, error) {
value, err := c.DecodeCandidate(content)
if err != nil {
return dnd.NPCInteractionList{}, err
return dnd.NPCOccurrenceList{}, err
}
if err := validate(value); err != nil {
return dnd.NPCInteractionList{}, fmt.Errorf("decode dnd npc interaction list: %w", err)
return dnd.NPCOccurrenceList{}, fmt.Errorf("decode dnd npc occurrence list: %w", err)
}
return value, nil
}
// DecodeCandidate reads one strict durable JSON value before semantic
// validators have approved it.
func (c *Codec) DecodeCandidate(content []byte) (dnd.NPCInteractionList, error) {
return candidatejson.DecodeCandidate[dnd.NPCInteractionList]("dnd npc interaction list", content)
func (c *Codec) DecodeCandidate(content []byte) (dnd.NPCOccurrenceList, error) {
return candidatejson.DecodeCandidate[dnd.NPCOccurrenceList]("dnd npc occurrence list", content)
}
func validate(value dnd.NPCInteractionList) error {
if value.Interactions == nil {
return fmt.Errorf("interactions must be present")
func validate(value dnd.NPCOccurrenceList) error {
if value.Occurrences == nil {
return fmt.Errorf("occurrences must be present")
}
for index, interaction := range value.Interactions {
prefix := fmt.Sprintf("interactions[%d]", index)
if strings.TrimSpace(interaction.Name) == "" {
for index, occurrence := range value.Occurrences {
prefix := fmt.Sprintf("occurrences[%d]", index)
if strings.TrimSpace(occurrence.NPCID) == "" {
return fmt.Errorf("%s.npc_id must not be empty", prefix)
}
if strings.TrimSpace(occurrence.Name) == "" {
return fmt.Errorf("%s.name must not be empty", prefix)
}
if !validInteractionKind(interaction.Kind) {
if !validOccurrenceKind(occurrence.Kind) {
return fmt.Errorf("%s.kind must be supported", prefix)
}
if len(interaction.SourceRefs) == 0 {
if len(occurrence.SourceRefs) == 0 {
return fmt.Errorf("%s.source_refs must contain at least one reference", prefix)
}
for refIndex, ref := range interaction.SourceRefs {
for refIndex, ref := range occurrence.SourceRefs {
refPrefix := fmt.Sprintf("%s.source_refs[%d]", prefix, refIndex)
if strings.TrimSpace(ref.SourceID) == "" {
return fmt.Errorf("%s.source_id must not be empty", refPrefix)
@@ -109,14 +112,14 @@ func validate(value dnd.NPCInteractionList) error {
return nil
}
func validInteractionKind(value dnd.NPCInteractionKind) bool {
func validOccurrenceKind(value dnd.NPCOccurrenceKind) bool {
switch value {
case dnd.NPCInteractionKindMentioned,
dnd.NPCInteractionKindNoncombatPresence,
dnd.NPCInteractionKindDialogue,
dnd.NPCInteractionKindCombatAlly,
dnd.NPCInteractionKindCombatOpponent,
dnd.NPCInteractionKindOther:
case dnd.NPCOccurrenceKindMentioned,
dnd.NPCOccurrenceKindNoncombatPresence,
dnd.NPCOccurrenceKindDialogue,
dnd.NPCOccurrenceKindCombatAlly,
dnd.NPCOccurrenceKindCombatOpponent,
dnd.NPCOccurrenceKindOther:
return true
default:
return false

View File

@@ -15,21 +15,21 @@ import (
"gitea.maximumdirect.net/eric/notarius/internal/modules/dnd"
)
func validList() dnd.NPCInteractionList {
return dnd.NPCInteractionList{Interactions: []dnd.NPCInteraction{
func validList() dnd.NPCOccurrenceList {
return dnd.NPCOccurrenceList{Occurrences: []dnd.NPCOccurrence{
{
Name: "Mira Thorn", Kind: dnd.NPCInteractionKindDialogue,
NPCID: "npc:test-mira", Name: "Mira Thorn", Kind: dnd.NPCOccurrenceKindDialogue,
SourceRefs: []source.SourceRef{{SourceID: "session-alpha", StartUnitID: 1, EndUnitID: 2}},
},
{
Name: "Hooded Guard", Kind: dnd.NPCInteractionKindCombatOpponent,
NPCID: "npc:test-guard", Name: "Hooded Guard", Kind: dnd.NPCOccurrenceKindCombatOpponent,
SourceRefs: []source.SourceRef{{SourceID: "session-alpha", StartUnitID: 3, EndUnitID: 3}},
},
}}
}
func TestCodecMatchesMaintainedDurableFixture(t *testing.T) {
raw, err := os.ReadFile("testdata/dnd_npc_interactions.v1.json")
raw, err := os.ReadFile("testdata/dnd_npc_occurrences.v1.json")
if err != nil {
t.Fatal(err)
}
@@ -57,7 +57,7 @@ func TestCodecMatchesMaintainedDurableFixture(t *testing.T) {
func TestCodecOwnsDurableSchemaAndRegistersExactType(t *testing.T) {
codec := New()
schema := codec.Schema()
if codec.Kind() != dnd.NPCInteractionListKind || codec.MediaType() != MediaType {
if codec.Kind() != dnd.NPCOccurrenceListKind || 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) {
@@ -67,11 +67,11 @@ func TestCodecOwnsDurableSchemaAndRegistersExactType(t *testing.T) {
if err := pipeline.RegisterArtifactCodec(registry, codec); err != nil {
t.Fatal(err)
}
spec, ok := registry.Spec(dnd.NPCInteractionListKind)
spec, ok := registry.Spec(dnd.NPCOccurrenceListKind)
if !ok || spec.SchemaDigest != contracts.DigestArtifactSchema(schema) {
t.Fatalf("registered spec = %#v, %t", spec, ok)
}
if _, err := registry.Encode(dnd.NPCInteractionListKind, dnd.NPCRegistry{}); err == nil {
if _, err := registry.Encode(dnd.NPCOccurrenceListKind, dnd.NPCRegistry{}); err == nil {
t.Fatal("Encode() error = nil, want exact type rejection")
} else {
var typeErr *pipeline.ArtifactCodecTypeError
@@ -83,17 +83,17 @@ func TestCodecOwnsDurableSchemaAndRegistersExactType(t *testing.T) {
func TestCodecSupportsEmptyListAndPreservesCollectionPresenceInCandidates(t *testing.T) {
codec := New()
empty := dnd.NPCInteractionList{Interactions: []dnd.NPCInteraction{}}
empty := dnd.NPCOccurrenceList{Occurrences: []dnd.NPCOccurrence{}}
content, err := codec.Encode(empty)
if err != nil || string(content) != `{"interactions":[]}` {
if err != nil || string(content) != `{"occurrences":[]}` {
t.Fatalf("Encode() = %s, %v", content, err)
}
for _, candidate := range []dnd.NPCInteractionList{
for _, candidate := range []dnd.NPCOccurrenceList{
{},
empty,
{Interactions: []dnd.NPCInteraction{{Name: " ", Kind: "unsupported", SourceRefs: nil}}},
{Interactions: []dnd.NPCInteraction{{Name: " ", Kind: "unsupported", SourceRefs: []source.SourceRef{}}}},
{Interactions: []dnd.NPCInteraction{{Name: " ", Kind: "unsupported", SourceRefs: []source.SourceRef{{StartUnitID: 0, EndUnitID: -1}}}}},
{Occurrences: []dnd.NPCOccurrence{{NPCID: "npc:test", Name: " ", Kind: "unsupported", SourceRefs: nil}}},
{Occurrences: []dnd.NPCOccurrence{{NPCID: "npc:test", Name: " ", Kind: "unsupported", SourceRefs: []source.SourceRef{}}}},
{Occurrences: []dnd.NPCOccurrence{{NPCID: "npc:test", Name: " ", Kind: "unsupported", SourceRefs: []source.SourceRef{{StartUnitID: 0, EndUnitID: -1}}}}},
} {
content, err := codec.EncodeCandidate(candidate)
if err != nil || !json.Valid(content) {
@@ -107,13 +107,13 @@ func TestCodecSupportsEmptyListAndPreservesCollectionPresenceInCandidates(t *tes
}
func TestCodecStrictlyRejectsMalformedUnknownAndTrailingJSON(t *testing.T) {
validJSON := `{"interactions":[{"name":"Mira Thorn","kind":"dialogue","source_refs":[{"source_id":"session","start_unit_id":1,"end_unit_id":1}]}]}`
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 }{
{"malformed", `{`, "decode dnd npc interaction list"},
{"unknown top-level", `{"interactions":[],"unexpected":true}`, "unknown field"},
{"malformed", `{`, "decode dnd npc occurrence list"},
{"unknown top-level", `{"occurrences":[],"unexpected":true}`, "unknown field"},
{"unknown interaction field", strings.Replace(validJSON, `"kind":"dialogue"`, `"kind":"dialogue","unexpected":true`, 1), "unknown field"},
{"unknown source reference field", strings.Replace(validJSON, `"end_unit_id":1`, `"end_unit_id":1,"unexpected":true`, 1), "unknown field"},
{"trailing", `{"interactions":[]} {}`, "multiple JSON values"},
{"trailing", `{"occurrences":[]} {}`, "multiple JSON values"},
} {
t.Run(test.name, func(t *testing.T) {
if _, err := New().Decode([]byte(test.raw)); err == nil || !strings.Contains(err.Error(), test.want) {
@@ -126,16 +126,17 @@ func TestCodecStrictlyRejectsMalformedUnknownAndTrailingJSON(t *testing.T) {
func TestCodecRejectsRequiredShapeEnumAndReferenceBoundaries(t *testing.T) {
tests := []struct {
name string
value dnd.NPCInteractionList
value dnd.NPCOccurrenceList
want string
}{
{"nil interactions", dnd.NPCInteractionList{}, "interactions must be present"},
{"blank name", mutate(validList(), func(v *dnd.NPCInteractionList) { v.Interactions[0].Name = " " }), "name must not be empty"},
{"unsupported kind", mutate(validList(), func(v *dnd.NPCInteractionList) { v.Interactions[0].Kind = "unsupported" }), "kind must be supported"},
{"nil source refs", mutate(validList(), func(v *dnd.NPCInteractionList) { v.Interactions[0].SourceRefs = nil }), "source_refs must contain"},
{"empty source ID", mutate(validList(), func(v *dnd.NPCInteractionList) { v.Interactions[0].SourceRefs[0].SourceID = " " }), "source_id must not be empty"},
{"non-positive start", mutate(validList(), func(v *dnd.NPCInteractionList) { v.Interactions[0].SourceRefs[0].StartUnitID = 0 }), "start_unit_id must be positive"},
{"non-positive end", mutate(validList(), func(v *dnd.NPCInteractionList) { v.Interactions[0].SourceRefs[0].EndUnitID = 0 }), "end_unit_id must be positive"},
{"nil occurrences", dnd.NPCOccurrenceList{}, "occurrences must be present"},
{"blank NPC ID", mutate(validList(), func(v *dnd.NPCOccurrenceList) { v.Occurrences[0].NPCID = " " }), "npc_id must not be empty"},
{"blank name", mutate(validList(), func(v *dnd.NPCOccurrenceList) { v.Occurrences[0].Name = " " }), "name must not be empty"},
{"unsupported kind", mutate(validList(), func(v *dnd.NPCOccurrenceList) { v.Occurrences[0].Kind = "unsupported" }), "kind must be supported"},
{"nil source refs", mutate(validList(), func(v *dnd.NPCOccurrenceList) { v.Occurrences[0].SourceRefs = nil }), "source_refs must contain"},
{"empty source ID", mutate(validList(), func(v *dnd.NPCOccurrenceList) { v.Occurrences[0].SourceRefs[0].SourceID = " " }), "source_id must not be empty"},
{"non-positive start", mutate(validList(), func(v *dnd.NPCOccurrenceList) { v.Occurrences[0].SourceRefs[0].StartUnitID = 0 }), "start_unit_id must be positive"},
{"non-positive end", mutate(validList(), func(v *dnd.NPCOccurrenceList) { v.Occurrences[0].SourceRefs[0].EndUnitID = 0 }), "end_unit_id must be positive"},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
@@ -147,16 +148,16 @@ func TestCodecRejectsRequiredShapeEnumAndReferenceBoundaries(t *testing.T) {
}
func TestCodecAcceptsEveryInteractionKind(t *testing.T) {
for _, kind := range []dnd.NPCInteractionKind{
dnd.NPCInteractionKindMentioned,
dnd.NPCInteractionKindNoncombatPresence,
dnd.NPCInteractionKindDialogue,
dnd.NPCInteractionKindCombatAlly,
dnd.NPCInteractionKindCombatOpponent,
dnd.NPCInteractionKindOther,
for _, kind := range []dnd.NPCOccurrenceKind{
dnd.NPCOccurrenceKindMentioned,
dnd.NPCOccurrenceKindNoncombatPresence,
dnd.NPCOccurrenceKindDialogue,
dnd.NPCOccurrenceKindCombatAlly,
dnd.NPCOccurrenceKindCombatOpponent,
dnd.NPCOccurrenceKindOther,
} {
value := validList()
value.Interactions[0].Kind = kind
value.Occurrences[0].Kind = kind
if _, err := New().Encode(value); err != nil {
t.Fatalf("Encode(%q) error = %v", kind, err)
}
@@ -172,12 +173,12 @@ func TestCodecSchemaAndMetadataAreDefensive(t *testing.T) {
}
metadata := codec.Metadata(validList())
metadata["other"] = true
if next := codec.Metadata(validList()); len(next) != 1 || next["interaction_count"] != 2 {
if next := codec.Metadata(validList()); len(next) != 1 || next["occurrence_count"] != 2 {
t.Fatalf("Metadata() = %#v", next)
}
}
func mutate(value dnd.NPCInteractionList, change func(*dnd.NPCInteractionList)) dnd.NPCInteractionList {
func mutate(value dnd.NPCOccurrenceList, change func(*dnd.NPCOccurrenceList)) dnd.NPCOccurrenceList {
change(&value)
return value
}

View File

@@ -1,6 +1,7 @@
{
"interactions": [
"occurrences": [
{
"npc_id": "npc:test-mira",
"name": "Mira Thorn",
"kind": "dialogue",
"source_refs": [
@@ -8,6 +9,7 @@
]
},
{
"npc_id": "npc:test-guard",
"name": "Hooded Guard",
"kind": "combat_opponent",
"source_refs": [