aboutsummaryrefslogtreecommitdiff
path: root/internal/item/hardening_id_mismatch_test.go
diff options
context:
space:
mode:
authorhistoria <[not public]>2026-09-18 05:31:09 -0400
committerhistoria <[not public]>2026-09-18 05:31:09 -0400
commit153acb7f644d755c45533b7c5311e3ffa47e8973 (patch)
tree7645e6f7b65bc3830b078e3b9336d2e6b199d584 /internal/item/hardening_id_mismatch_test.go
parent87d08f0151e643951769e323995f1c5ed89ee200 (diff)
downloadthehouseoficarus-153acb7f644d755c45533b7c5311e3ffa47e8973.tar.gz
fix: harden engine across casino, economy, concurrency, security, and content
Diffstat (limited to 'internal/item/hardening_id_mismatch_test.go')
-rw-r--r--internal/item/hardening_id_mismatch_test.go88
1 files changed, 88 insertions, 0 deletions
diff --git a/internal/item/hardening_id_mismatch_test.go b/internal/item/hardening_id_mismatch_test.go
new file mode 100644
index 0000000..5123fc0
--- /dev/null
+++ b/internal/item/hardening_id_mismatch_test.go
@@ -0,0 +1,88 @@
+package item
+
+import (
+ "bytes"
+ "log"
+ "os"
+ "path/filepath"
+ "strings"
+ "testing"
+)
+
+// Regression: an item file whose declared id: disagrees with its filename was
+// silently loaded under the filename with no diagnostic. ItemStore must warn,
+// because the author has no other sign that the id they wrote is ignored.
+
+func captureLog(t *testing.T, fn func()) string {
+ t.Helper()
+ var buf bytes.Buffer
+ old := log.Writer()
+ log.SetOutput(&buf)
+ defer log.SetOutput(old)
+ fn()
+ return buf.String()
+}
+
+func writeItemFile(t *testing.T, dir, name, body string) {
+ t.Helper()
+ path := filepath.Join(dir, "items", name)
+ if err := os.MkdirAll(filepath.Dir(path), 0o755); err != nil {
+ t.Fatal(err)
+ }
+ if err := os.WriteFile(path, []byte(body), 0o644); err != nil {
+ t.Fatal(err)
+ }
+}
+
+func TestItemStoreWarnsOnDeclaredIDMismatch(t *testing.T) {
+ dir := t.TempDir()
+ writeItemFile(t, dir, "foo.yaml", "id: bar\nname: Foo\n")
+ s := NewItemStore(dir)
+
+ out := captureLog(t, func() {
+ def, err := s.Load("foo")
+ if err != nil {
+ t.Fatalf("load: %v", err)
+ }
+ if def.ID != "foo" {
+ t.Fatalf("filename must win: ID = %q, want foo", def.ID)
+ }
+ })
+ if !strings.Contains(out, "declares id") {
+ t.Fatalf("expected an id-mismatch warning, got log %q", out)
+ }
+}
+
+func TestItemStoreDoesNotWarnWhenIDMatchesOrAbsent(t *testing.T) {
+ dir := t.TempDir()
+ writeItemFile(t, dir, "foo.yaml", "id: foo\nname: Foo\n")
+ writeItemFile(t, dir, "baz.yaml", "name: Baz\n")
+ s := NewItemStore(dir)
+
+ out := captureLog(t, func() {
+ if _, err := s.Load("foo"); err != nil {
+ t.Fatal(err)
+ }
+ if _, err := s.Load("baz"); err != nil {
+ t.Fatal(err)
+ }
+ })
+ if strings.Contains(out, "declares id") {
+ t.Fatalf("unexpected id-mismatch warning: %q", out)
+ }
+}
+
+func TestItemStoreLoadAllWarnsOnDeclaredIDMismatch(t *testing.T) {
+ dir := t.TempDir()
+ writeItemFile(t, dir, "foo.yaml", "id: bar\nname: Foo\n")
+ s := NewItemStore(dir)
+
+ out := captureLog(t, func() {
+ if _, err := s.LoadAll(); err != nil {
+ t.Fatal(err)
+ }
+ })
+ if !strings.Contains(out, "declares id") {
+ t.Fatalf("expected an id-mismatch warning from LoadAll, got %q", out)
+ }
+}