| 1 |
import { readFile } from "fs/promises"; |
| 2 |
import { join } from "path"; |
| 3 |
|
| 4 |
import { ledgerDataValidator } from "../src/api/validators"; |
| 5 |
import { parseJSON } from "../src/lib/json"; |
| 6 |
import { ledgerData } from "../src/stores"; |
| 7 |
|
| 8 |
/** Load the Python test snapshot output with the given name and parse as JSON. */ |
| 9 |
export async function loadJSONSnapshot( |
| 10 |
name: `${string}.json`, |
| 11 |
): Promise<unknown> { |
| 12 |
const path = join(__dirname, "..", "..", "tests", "__snapshots__", name); |
| 13 |
return parseJSON(await readFile(path, "utf8")).unwrap(); |
| 14 |
} |
| 15 |
|
| 16 |
let loaded = false; |
| 17 |
|
| 18 |
/** Load the ledgerData (of the long-example file. */ |
| 19 |
export async function initialiseLedgerData(): Promise<void> { |
| 20 |
if (loaded) { |
| 21 |
return; |
| 22 |
} |
| 23 |
try { |
| 24 |
const data = await loadJSONSnapshot( |
| 25 |
"test_internal_api-test_get_ledger_data.json", |
| 26 |
); |
| 27 |
const res = ledgerDataValidator(data).unwrap(); |
| 28 |
ledgerData.set(res); |
| 29 |
loaded = true; |
| 30 |
} catch (error: unknown) { |
| 31 |
console.error(error); |
| 32 |
} |
| 33 |
} |