package topdata import ( "os" "path/filepath" "testing" ) const damagetypesRegistryTestTypes = `{ "rows": [ {"id": 0, "key": "damagetype:bludgeoning", "label": "Bludgeoning", "group_label": "Physical", "damage_type_group": 0}, {"id": 1, "key": "damagetype:piercing", "label": "Piercing", "group_label": "Physical", "damage_type_group": 0} ] } ` // validate-topdata must never modify source lockfiles. Collection in read-only // mode must leave the registry lock byte-identical even though the rows carry // explicit ids that would otherwise mark the lock "modified". func TestCollectGeneratedRegistryDatasetsReadOnlyDoesNotWriteLock(t *testing.T) { dataDir := t.TempDir() regDir := filepath.Join(dataDir, "damagetypes", "registry") mkdirAll(t, regDir) writeFile(t, filepath.Join(regDir, "types.json"), damagetypesRegistryTestTypes) lockPath := filepath.Join(regDir, "lock.json") original := "{\n \"damagetype:bludgeoning\": 0,\n \"damagetype:piercing\": 1\n}\n" writeFile(t, lockPath, original) if _, err := collectGeneratedRegistryDatasets(dataDir, false); err != nil { t.Fatalf("read-only collection failed: %v", err) } got, err := os.ReadFile(lockPath) if err != nil { t.Fatalf("read lockfile: %v", err) } if string(got) != original { t.Fatalf("read-only collection must not modify lockfile.\nwant: %q\ngot: %q", original, string(got)) } } // build-topdata still persists allocated ids to source lockfiles. func TestCollectGeneratedRegistryDatasetsPersistWritesLock(t *testing.T) { dataDir := t.TempDir() regDir := filepath.Join(dataDir, "damagetypes", "registry") mkdirAll(t, regDir) writeFile(t, filepath.Join(regDir, "types.json"), damagetypesRegistryTestTypes) lockPath := filepath.Join(regDir, "lock.json") writeFile(t, lockPath, "{\n \"damagetype:bludgeoning\": 0\n}\n") if _, err := collectGeneratedRegistryDatasets(dataDir, true); err != nil { t.Fatalf("persisting collection failed: %v", err) } lockData, err := loadLockfile(lockPath) if err != nil { t.Fatalf("load lockfile: %v", err) } if lockData["damagetype:piercing"] != 1 { t.Fatalf("persisting collection should record allocated id; got %#v", lockData) } }