|
| 1 | +/** |
| 2 | + * Shared file-derived ParseOptions builder. |
| 3 | + * |
| 4 | + * `buildFileDerivedParseOptions(filePath)` is the single source of truth for |
| 5 | + * the *file-derived* axis of `ParseOptions`: any setting whose value is a |
| 6 | + * function of where the file sits on disk (which sibling resources exist, |
| 7 | + * which mod tree it belongs to, etc.). Both the CLI and the editor call this |
| 8 | + * so a behavior added here propagates to every frontend. |
| 9 | + * |
| 10 | + * Frontend-preference axes (`skipMapTiles`, `gracefulMapBoundaries`) stay |
| 11 | + * out of this builder by design — those legitimately differ per caller. |
| 12 | + */ |
| 13 | + |
| 14 | +import { describe, it, expect, beforeEach, afterEach } from "vitest"; |
| 15 | +import * as fs from "fs"; |
| 16 | +import * as path from "path"; |
| 17 | +import * as os from "os"; |
| 18 | +import { buildFileDerivedParseOptions } from "../src/parse-options"; |
| 19 | + |
| 20 | +const FIXTURES = path.resolve("client/testFixture/proto"); |
| 21 | + |
| 22 | +let tmpDir: string; |
| 23 | + |
| 24 | +beforeEach(() => { |
| 25 | + tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), "fgbin-fdo-")); |
| 26 | +}); |
| 27 | + |
| 28 | +afterEach(() => { |
| 29 | + fs.rmSync(tmpDir, { recursive: true, force: true }); |
| 30 | +}); |
| 31 | + |
| 32 | +function makeMapDirWithProto(): string { |
| 33 | + const mapsDir = path.join(tmpDir, "maps"); |
| 34 | + const protoDir = path.join(tmpDir, "proto"); |
| 35 | + fs.mkdirSync(mapsDir); |
| 36 | + fs.mkdirSync(path.join(protoDir, "items"), { recursive: true }); |
| 37 | + fs.mkdirSync(path.join(protoDir, "scenery"), { recursive: true }); |
| 38 | + fs.copyFileSync(path.join(FIXTURES, "items", "00000031.pro"), path.join(protoDir, "items", "00000031.pro")); |
| 39 | + fs.copyFileSync(path.join(FIXTURES, "scenery", "00000008.pro"), path.join(protoDir, "scenery", "00000008.pro")); |
| 40 | + return path.join(mapsDir, "fake.map"); |
| 41 | +} |
| 42 | + |
| 43 | +describe("buildFileDerivedParseOptions", () => { |
| 44 | + it("returns empty options for a non-.map file path", () => { |
| 45 | + const opts = buildFileDerivedParseOptions("/some/where/file.pro"); |
| 46 | + expect(opts.pidResolver).toBeUndefined(); |
| 47 | + expect(opts.diagnostics).toBeUndefined(); |
| 48 | + }); |
| 49 | + |
| 50 | + it("returns empty options for a .map file with no sibling proto/ dir", () => { |
| 51 | + const mapsDir = path.join(tmpDir, "maps"); |
| 52 | + fs.mkdirSync(mapsDir); |
| 53 | + const opts = buildFileDerivedParseOptions(path.join(mapsDir, "any.map")); |
| 54 | + expect(opts.pidResolver).toBeUndefined(); |
| 55 | + expect(opts.diagnostics).toBeUndefined(); |
| 56 | + }); |
| 57 | + |
| 58 | + it("returns a pidResolver covering items from a sibling proto/items/", () => { |
| 59 | + const mapPath = makeMapDirWithProto(); |
| 60 | + const opts = buildFileDerivedParseOptions(mapPath); |
| 61 | + expect(opts.pidResolver).toBeDefined(); |
| 62 | + // 00000031.pro is a vanilla ammo proto (subType 4 / Ammo). |
| 63 | + expect(opts.pidResolver!(31)).toBe(4); |
| 64 | + }); |
| 65 | + |
| 66 | + it("returns a pidResolver covering scenery from a sibling proto/scenery/", () => { |
| 67 | + const mapPath = makeMapDirWithProto(); |
| 68 | + const opts = buildFileDerivedParseOptions(mapPath); |
| 69 | + // 00000008.pro is a vanilla door proto (subType 0 / Door); pid = (2<<24)|8. |
| 70 | + expect(opts.pidResolver!(0x02000008)).toBe(0); |
| 71 | + }); |
| 72 | + |
| 73 | + it("layers proto/ overrides on top of the bundled vanilla defaults", () => { |
| 74 | + const mapPath = makeMapDirWithProto(); |
| 75 | + const opts = buildFileDerivedParseOptions(mapPath); |
| 76 | + // pid 161 is a vanilla weapon in the bundled table → subType 3 (Weapon). |
| 77 | + // It is NOT in the temp proto dir, so this exercises fallback. |
| 78 | + expect(opts.pidResolver!(161)).toBe(3); |
| 79 | + }); |
| 80 | + |
| 81 | + it("reports diagnostics so callers can log scan stats", () => { |
| 82 | + const mapPath = makeMapDirWithProto(); |
| 83 | + const opts = buildFileDerivedParseOptions(mapPath); |
| 84 | + expect(opts.diagnostics).toBeDefined(); |
| 85 | + expect(opts.diagnostics!.protoDir).toBe(path.join(tmpDir, "proto")); |
| 86 | + expect(opts.diagnostics!.stats.filesScanned).toBe(2); |
| 87 | + expect(opts.diagnostics!.stats.subtypesResolved).toBe(2); |
| 88 | + expect(opts.diagnostics!.stats.errors).toEqual([]); |
| 89 | + }); |
| 90 | + |
| 91 | + it("returns empty options when sibling proto/ exists but has zero matching files", () => { |
| 92 | + const mapsDir = path.join(tmpDir, "maps"); |
| 93 | + const protoDir = path.join(tmpDir, "proto"); |
| 94 | + fs.mkdirSync(mapsDir); |
| 95 | + fs.mkdirSync(path.join(protoDir, "items"), { recursive: true }); |
| 96 | + // Empty proto/items/ — filesScanned will be 0, no resolver attached. |
| 97 | + const opts = buildFileDerivedParseOptions(path.join(mapsDir, "any.map")); |
| 98 | + expect(opts.pidResolver).toBeUndefined(); |
| 99 | + expect(opts.diagnostics).toBeUndefined(); |
| 100 | + }); |
| 101 | +}); |
0 commit comments