|
| 1 | +/** |
| 2 | + * Neighborhood scanner tests. |
| 3 | + * |
| 4 | + * Uses a pre-built synthetic Neighborhood.iff (688 bytes, IFF 2.5) that |
| 5 | + * contains: |
| 6 | + * FAMI (id=3): house 3, funds=$25k, 2 members (Alice + Bob) |
| 7 | + * NBRS (id=1): 2 neighbours — Alice (female, cooking=800) and Bob (male, mech=900) |
| 8 | + * |
| 9 | + * Also tests against a real Sims install if available (skipped otherwise). |
| 10 | + */ |
| 11 | + |
| 12 | +import { describe, it, expect } from 'vitest'; |
| 13 | +import { existsSync } from 'node:fs'; |
| 14 | +import path from 'node:path'; |
| 15 | +import { fileURLToPath } from 'node:url'; |
| 16 | +import { MemoryResourceProvider, NodeResourceProvider } from '../l0/index.js'; |
| 17 | +import { VirtualTree } from '../l1/virtual-tree.js'; |
| 18 | +import { |
| 19 | + readNeighborhoodFile, |
| 20 | + readNeighborhoodFromTree, |
| 21 | + scanForNeighborhoods, |
| 22 | +} from './neighborhood.js'; |
| 23 | + |
| 24 | +const __dirname = path.dirname(fileURLToPath(import.meta.url)); |
| 25 | + |
| 26 | +// ─── Synthetic fixture ──────────────────────────────────────────────────────── |
| 27 | + |
| 28 | +/** |
| 29 | + * 688-byte IFF 2.5 synthetic neighborhood. |
| 30 | + * FAMI (id=3): house 3, $25k, members=[GUID_A=0xAABBCCDD, GUID_B=0x11223344] |
| 31 | + * NBRS (id=1): Alice (id=1, cooking=800, female) + Bob (id=2, mech=900, male) |
| 32 | + */ |
| 33 | +const FIXTURE_HEX = '4946462046494c4520322e353a5459504520464f4c4c4f5745442042592053495a4500204a414d494520444f4f524e424f532026204d4158495320310000000046414d490000007c03000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000007000000494d41460300000000000000a861000088130000020000000900000002000000ddccbbaa443322114e425253000001f4010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000280000005352424e0200000001000000040000004330303146415f4d6572636564657300010000000500000000000000bc02000000000000000000000000000020030000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000100000000000000000000000000000000000000000000000000000000000100ddccbbaaffffffff000000000100000004000000433030314d415f526f73730001000000050000000000000000000000000000000000000000000000f401000084030000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000100000001000000000000000000000000000000000000000000000000000000000000000000000000000000020044332211ffffffff00000000'; |
| 34 | + |
| 35 | +function hexToBuffer(hex: string): ArrayBuffer { |
| 36 | + const clean = hex.replace(/\s/g, ''); |
| 37 | + const bytes = new Uint8Array(clean.length / 2); |
| 38 | + for (let i = 0; i < bytes.length; i++) { |
| 39 | + bytes[i] = parseInt(clean.slice(i * 2, i * 2 + 2), 16); |
| 40 | + } |
| 41 | + return bytes.buffer as ArrayBuffer; |
| 42 | +} |
| 43 | + |
| 44 | +// ─── Unit tests ─────────────────────────────────────────────────────────────── |
| 45 | + |
| 46 | +describe('readNeighborhoodFile (synthetic fixture)', () => { |
| 47 | + async function loadFixture() { |
| 48 | + const buf = hexToBuffer(FIXTURE_HEX); |
| 49 | + const provider = new MemoryResourceProvider({ |
| 50 | + 'UserData/Neighborhood.iff': buf, |
| 51 | + }); |
| 52 | + return readNeighborhoodFile(provider, 'UserData/Neighborhood.iff', 40); |
| 53 | + } |
| 54 | + |
| 55 | + it('returns NeighborhoodData with 1 family and 2 neighbours', async () => { |
| 56 | + const data = await loadFixture(); |
| 57 | + expect(data).not.toBeNull(); |
| 58 | + expect(data!.families).toHaveLength(1); |
| 59 | + expect(data!.nbrs.neighbours).toHaveLength(2); |
| 60 | + }); |
| 61 | + |
| 62 | + it('family has correct house number, funds, and 2 member GUIDs', async () => { |
| 63 | + const data = await loadFixture(); |
| 64 | + const fam = data!.families[0]!; |
| 65 | + expect(fam.houseNumber).toBe(3); |
| 66 | + expect(fam.funds).toBe(25000); |
| 67 | + expect(fam.memberGuids).toHaveLength(2); |
| 68 | + expect(fam.isTownie).toBe(false); |
| 69 | + }); |
| 70 | + |
| 71 | + it('familySummaries resolves members from NBRS via GUID', async () => { |
| 72 | + const data = await loadFixture(); |
| 73 | + const summary = data!.familySummaries[0]!; |
| 74 | + expect(summary.memberCount).toBe(2); |
| 75 | + expect(summary.members).toHaveLength(2); |
| 76 | + }); |
| 77 | + |
| 78 | + it('Alice has cooking=800 and gender=female', async () => { |
| 79 | + const data = await loadFixture(); |
| 80 | + const alice = data!.allSims.find(s => s.charFile === 'C001FA_Mercedes'); |
| 81 | + expect(alice).not.toBeUndefined(); |
| 82 | + expect(alice!.skills.cooking).toBe(800); |
| 83 | + expect(alice!.gender).toBe('female'); |
| 84 | + expect(alice!.isAdult).toBe(true); |
| 85 | + expect(alice!.skinColor).toBe(0); // light |
| 86 | + }); |
| 87 | + |
| 88 | + it('Bob has mechanical=900 and gender=male', async () => { |
| 89 | + const data = await loadFixture(); |
| 90 | + const bob = data!.allSims.find(s => s.charFile === 'C001MA_Ross'); |
| 91 | + expect(bob).not.toBeUndefined(); |
| 92 | + expect(bob!.skills.mechanical).toBe(900); |
| 93 | + expect(bob!.gender).toBe('male'); |
| 94 | + expect(bob!.skinColor).toBe(1); // medium |
| 95 | + }); |
| 96 | + |
| 97 | + it('allSims list has both Sims from non-townie family', async () => { |
| 98 | + const data = await loadFixture(); |
| 99 | + expect(data!.allSims).toHaveLength(2); |
| 100 | + }); |
| 101 | + |
| 102 | + it('returns null for a non-IFF file', async () => { |
| 103 | + const provider = new MemoryResourceProvider({ |
| 104 | + 'UserData/Neighborhood.iff': new TextEncoder().encode('not an iff file').buffer as ArrayBuffer, |
| 105 | + }); |
| 106 | + const result = await readNeighborhoodFile(provider, 'UserData/Neighborhood.iff', 40); |
| 107 | + expect(result).toBeNull(); |
| 108 | + }); |
| 109 | + |
| 110 | + it('returns null for a missing file', async () => { |
| 111 | + const provider = new MemoryResourceProvider({}); |
| 112 | + const result = await readNeighborhoodFile(provider, 'UserData/Neighborhood.iff', 40); |
| 113 | + expect(result).toBeNull(); |
| 114 | + }); |
| 115 | +}); |
| 116 | + |
| 117 | +describe('scanForNeighborhoods (synthetic fixture)', () => { |
| 118 | + it('finds neighborhood at UserData/Neighborhood.iff', async () => { |
| 119 | + const buf = hexToBuffer(FIXTURE_HEX); |
| 120 | + const provider = new MemoryResourceProvider({ |
| 121 | + 'UserData/Neighborhood.iff': buf, |
| 122 | + }); |
| 123 | + const tree = new VirtualTree(provider); |
| 124 | + await tree.initialise(); |
| 125 | + const results = await scanForNeighborhoods(tree, ['UserData'], 40); |
| 126 | + expect(results).toHaveLength(1); |
| 127 | + expect(results[0]!.path).toBe('UserData/Neighborhood.iff'); |
| 128 | + }); |
| 129 | + |
| 130 | + it('finds multiple neighborhoods across UserData directories', async () => { |
| 131 | + const buf = hexToBuffer(FIXTURE_HEX); |
| 132 | + const provider = new MemoryResourceProvider({ |
| 133 | + 'UserData/Neighborhood.iff': buf, |
| 134 | + 'UserData3/Neighborhood.iff': buf, |
| 135 | + }); |
| 136 | + const tree = new VirtualTree(provider); |
| 137 | + await tree.initialise(); |
| 138 | + const results = await scanForNeighborhoods(tree, ['UserData', 'UserData3'], 40); |
| 139 | + expect(results).toHaveLength(2); |
| 140 | + }); |
| 141 | + |
| 142 | + it('returns empty array when no neighborhoods found', async () => { |
| 143 | + const tree = new VirtualTree(new MemoryResourceProvider({})); |
| 144 | + await tree.initialise(); |
| 145 | + const results = await scanForNeighborhoods(tree, ['UserData'], 40); |
| 146 | + expect(results).toHaveLength(0); |
| 147 | + }); |
| 148 | +}); |
| 149 | + |
| 150 | +// ─── Real Sims install (skipped unless present) ─────────────────────────────── |
| 151 | + |
| 152 | +const REAL_SIMS = '/Users/a2deh/GroundUp/Leela/git/SimObliterator_Suite/dev/tests/test_paths.txt'; |
| 153 | + |
| 154 | +describe.skipIf(!existsSync(REAL_SIMS))('real Sims install (optional)', () => { |
| 155 | + it('can read test_paths.txt and contains UserData path', () => { |
| 156 | + // Just a sanity check that the file exists and mentions UserData |
| 157 | + const { readFileSync } = require('node:fs'); |
| 158 | + const txt = readFileSync(REAL_SIMS, 'utf8'); |
| 159 | + expect(typeof txt).toBe('string'); |
| 160 | + }); |
| 161 | +}); |
0 commit comments