1
0
Fork 0
polaris-task-force/tests/seed-baseline.ts
Z8MB1E 8135c9850b feat(test): run tests against a dedicated truncated database
Bootstrap a per-run test database (ensure, push schema, truncate, seed baselines, repair sequences), wire vitest/playwright globals, and gate schema push behind DB_PUSH. Serial test files share the one database.

Ultraworked with [Sisyphus](https://github.com/code-yeongyu/oh-my-openagent)

Co-authored-by: Sisyphus <clio-agent@sisyphuslabs.ai>
2026-09-22 17:58:17 -04:00

56 lines
1.5 KiB
TypeScript

import { getPayload } from "payload";
import config from "@payload-config";
/**
* Baseline data every test run can assume: a Game Rules global with a main
* currency resource. Runs after truncation, so it recreates the baseline on
* the empty database each run (idempotent by construction). Banking and
* economy code paths throw without a main currency.
*/
const CURRENCY_CODE_NAME = "res_test_credits";
const payload = await getPayload({ config });
const existingCurrency = await payload.find({
collection: "resources",
where: { codeName: { equals: CURRENCY_CODE_NAME } },
limit: 1,
overrideAccess: true,
});
let currencyId = existingCurrency.docs[0]?.id as number | undefined;
if (!currencyId) {
const created = await payload.create({
collection: "resources",
data: {
name: "Test Credits",
codeName: CURRENCY_CODE_NAME,
unitOfMeasure: "dollar",
approvalStatus: "in_progress",
type: "abstract",
baseValue: 1,
rarity: "common",
massPerUnit: 0,
gridWidth: 1,
gridHeight: 1,
tradeable: false,
},
draft: false,
overrideAccess: true,
});
currencyId = created.id as number;
}
const rules = await payload.findGlobal({ slug: "game-rules" });
await payload.updateGlobal({
slug: "game-rules",
data: {
...(rules as unknown as Record<string, unknown>),
mainCurrency: currencyId,
},
overrideAccess: true,
});
payload.logger.info(`[test-db] baseline ready: main currency resource ${currencyId}`);
process.exit(0);