// What the tests of FILIUS' own examples share (examplesJava.test.ts, the // acceptance of F2; classicsJava.test.ts, the acceptance of F3): an example // read with this package's reader, its nodes by the names Java gives them, // and each card Java recorded by its MAC address. import { readFileSync } from 'node:fs'; import { dirname, join } from 'node:path'; import { fileURLToPath } from 'node:url'; import { stableMacAddress, type Id } from '@mdg-filius/core'; import { openProjectArchive, type ImportedProject } from '../src/index.js'; import type { CaptureCard } from '../../core/test/javaRun.js'; const here = dirname(fileURLToPath(import.meta.url)); const examples = join(here, '../../../corpus/java-examples'); /** The recording of a story (examples/.java.json). */ export function exampleRecording(story: string): T { return JSON.parse(readFileSync(join(here, 'examples', `${story}.java.json`), 'utf8')) as T; } /** An example as this package reads it, its ids numbered as the file's entries come. */ export function openExample(file: string): ImportedProject { let counter = 0; return openProjectArchive(new Uint8Array(readFileSync(join(examples, file))), () => `00000000-0000-0000-0000-${(++counter).toString(16).padStart(12, '0')}`); } /** Java's names as ScenarioNetwork.fromFile gives them: a repeated name gets #2, #3, … in the file's order. */ export function namesInJavaOrder(project: ImportedProject): Map { const names = new Map(); for (const node of project.graph.nodes) { let name = node.displayName; for (let count = 2; names.has(name); count += 1) name = `${node.displayName}#${count}`; names.set(name, node.id); } return names; } /** * Each card Java recorded, by its node and index (`name/index`). A card the * file has no MAC address for gets one Java rolls at each load, and the * model one derived from its port: Java's stands for the engine's in the * texts (withEngineMacs). */ export function exampleCards( project: ImportedProject, names: ReadonlyMap, javaCards: Record, ): { readonly cards: CaptureCard[]; readonly withEngineMacs: (text: string) => string } { const engineMacByJavaMac = new Map(); const cards: CaptureCard[] = Object.entries(javaCards).map(([mac, card]) => { const [nodeName, index] = card.split('/') as [string, string]; const node = project.graph.nodes.find((each) => each.id === names.get(nodeName))!; const port = node.ports[Number(index)]!; engineMacByJavaMac.set(mac.toUpperCase(), (port.importedMacAddress ?? stableMacAddress(port.id)).toUpperCase()); return { key: mac, portId: port.id }; }); const withEngineMacs = (text: string) => text.replace(/[0-9A-Fa-f]{2}(:[0-9A-Fa-f]{2}){5}/g, (mac) => engineMacByJavaMac.get(mac.toUpperCase()) ?? mac); return { cards, withEngineMacs }; }