srs-api/src/testing/helpers.ts
Aswin Biju 6851d66b38 feat: SRS backend: orders intake, results pipeline, user management
Fastify + TypeScript + SQLite backend for a single Kerala lottery agency.
2026-11-03 10:09:46 +05:30

43 lines
1.5 KiB
TypeScript

import type { Db } from '../db';
import { openDb } from '../db';
import { runMigrations } from '../db/migrate';
/** A fresh in-memory database with every migration applied. */
export function makeDb(): Db {
const db = openDb(':memory:');
runMigrations(db);
return db;
}
export interface Fixture {
shops: { a: number; b: number; c: number };
admin: { id: number; phone: string };
admin2: { id: number; phone: string };
shopUser: { id: number; phone: string };
disabledUser: { id: number; phone: string };
}
export function seedFixture(db: Db): Fixture {
const shop = (code: string, name: string) =>
Number(db.prepare('INSERT INTO shops (name, code) VALUES (?, ?)').run(name, code).lastInsertRowid);
const shops = { a: shop('A', 'Shop A'), b: shop('B', 'Shop B'), c: shop('C', 'Shop C') };
const user = (username: string, phone: string, isAdmin: number, active: number) => ({
id: Number(
db
.prepare('INSERT INTO users (username, password_hash, phone, is_admin, active) VALUES (?, ?, ?, ?, ?)')
.run(username, 'x', phone, isAdmin, active).lastInsertRowid,
),
phone,
});
const admin = user('admin', '919000000001', 1, 1);
const admin2 = user('admin2', '919000000004', 1, 1);
const shopUser = user('shop-a', '919000000002', 0, 1);
const disabledUser = user('gone', '919000000003', 0, 0);
db.prepare('INSERT INTO user_shops (user_id, shop_id) VALUES (?, ?)').run(shopUser.id, shops.a);
return { shops, admin, admin2, shopUser, disabledUser };
}