mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-26 06:37:33 +00:00
1bba65634c
The server suite seeds and reads its database through some 5,500 raw better-sqlite3 statements on the sync handle. They restate the schema by hand, skip the repositories the app goes through and run on SQLite only, so nothing in the suite could ever exercise the dialect layer on another engine. tests/helpers/factories/ adds typed factories per domain (users, trips and days, places, assignments, bookings, budget, packing, to-dos, files, journeys, tours, collections, notifications, settings, plugins, tokens, atlas, vacay, photos, collab) and generic readers (findRow, findRows, countRows, updateRows, deleteRows) that go through MikroORM in a request context of their own, against the app's ORM or a createTestOrm handle. They refuse a write to a read-only foreign-key twin, which MikroORM would otherwise drop without a word. The README there is the conversion guide. The categories integration suite and the tags and calendar-feed e2e suites are converted in full as worked examples; the tags suite also moves from hand-rolled tables to the migrated snapshot. lint:test-sql (scripts/test-sql-ratchet.mjs) holds the raw statements left to a per-file baseline: growth and stale entries fail, files whose subject is raw SQL are exempt with a reason, and a single statement can be allowed with a test-sql-allow comment. CI runs it next to lint:boundaries.
75 lines
2.8 KiB
TypeScript
75 lines
2.8 KiB
TypeScript
import { createHash, randomBytes } from 'node:crypto';
|
|
import type { EntityData, EntityDTO } from '@mikro-orm/core';
|
|
import { InviteTokens } from '../../../src/db/entities/InviteTokens.entity';
|
|
import { McpTokens } from '../../../src/db/entities/McpTokens.entity';
|
|
import { OauthClients } from '../../../src/db/entities/OauthClients.entity';
|
|
import { nextSeq, type FactoryOrm } from './context';
|
|
import { createRow, findRow, insertRow } from './rows';
|
|
|
|
const sha256 = (value: string): string => createHash('sha256').update(value).digest('hex');
|
|
|
|
/**
|
|
* An MCP (or API) token for the user. Only its SHA-256 is stored, as the
|
|
* tokens service stores it; the raw token comes back so the test can send it.
|
|
*/
|
|
export async function makeMcpToken(
|
|
orm: FactoryOrm,
|
|
userId: number,
|
|
overrides: EntityData<McpTokens> & { rawToken?: string } = {},
|
|
): Promise<{ token: EntityDTO<McpTokens>; rawToken: string }> {
|
|
const { rawToken: given, ...columns } = overrides;
|
|
const rawToken = given ?? `trek_test_${nextSeq('mcp-token')}_${randomBytes(8).toString('hex')}`;
|
|
const token = await createRow(orm, McpTokens, {
|
|
user: userId,
|
|
name: 'Test Token',
|
|
token_hash: sha256(rawToken),
|
|
token_prefix: rawToken.slice(0, 12),
|
|
...columns,
|
|
});
|
|
return { token, rawToken };
|
|
}
|
|
|
|
/**
|
|
* An OAuth client registered by the user (or by nobody, for `null`),
|
|
* confidential unless told otherwise. Only the secret's SHA-256 is stored;
|
|
* the raw secret comes back.
|
|
*/
|
|
export async function makeOauthClient(
|
|
orm: FactoryOrm,
|
|
userId: number | null,
|
|
overrides: Omit<EntityData<OauthClients>, 'id'> & { id?: string; secret?: string } = {},
|
|
): Promise<{ client: EntityDTO<OauthClients>; secret: string }> {
|
|
const { secret: given, id: givenId, ...columns } = overrides;
|
|
const n = nextSeq('oauth-client');
|
|
const secret = given ?? `test-secret-${n}-${randomBytes(8).toString('hex')}`;
|
|
const id = givenId ?? `test-oauth-client-${n}`;
|
|
await insertRow(orm, OauthClients, {
|
|
id,
|
|
user: userId,
|
|
name: `Test Client ${n}`,
|
|
client_id: `client-${n}`,
|
|
client_secret_hash: sha256(secret),
|
|
redirect_uris: JSON.stringify(['https://client.example.test/callback']),
|
|
allowed_scopes: JSON.stringify(['trips:read']),
|
|
...columns,
|
|
});
|
|
const client = await findRow(orm, OauthClients, { id });
|
|
if (!client) throw new Error(`makeOauthClient: no client ${id} after its insert`);
|
|
return { client, secret };
|
|
}
|
|
|
|
/** A one-use registration invite created by `createdBy`. */
|
|
export function makeInviteToken(
|
|
orm: FactoryOrm,
|
|
createdBy: number,
|
|
overrides: EntityData<InviteTokens> = {},
|
|
): Promise<EntityDTO<InviteTokens>> {
|
|
return createRow(orm, InviteTokens, {
|
|
token: `test-invite-${nextSeq('invite-token')}`,
|
|
max_uses: 1,
|
|
used_count: 0,
|
|
createdByRef: createdBy,
|
|
...overrides,
|
|
});
|
|
}
|