experiments in a post-browser web
1#!/usr/bin/env node
2/**
3 * E2E test setup: create test users in system.db before server starts.
4 *
5 * Uses createRequire to import the CJS users module directly so we get
6 * real DB access without running the server.
7 *
8 * Environment variables:
9 * DATA_DIR — server data directory (must exist or will be created)
10 * USER_A_KEY — API key to assign to account-a
11 * USER_B_KEY — API key to assign to account-b
12 *
13 * Outputs JSON to stdout:
14 * { "account-a": { userId, apiKey }, "account-b": { userId, apiKey } }
15 */
16
17import { createRequire } from 'module';
18import { resolve } from 'path';
19
20const require = createRequire(import.meta.url);
21
22// DATA_DIR must be set so users.js writes to the test directory
23const DATA_DIR = process.env.DATA_DIR;
24const USER_A_KEY = process.env.USER_A_KEY;
25const USER_B_KEY = process.env.USER_B_KEY;
26
27if (!DATA_DIR) {
28 console.error('DATA_DIR is required');
29 process.exit(1);
30}
31if (!USER_A_KEY || !USER_B_KEY) {
32 console.error('USER_A_KEY and USER_B_KEY are required');
33 process.exit(1);
34}
35
36// Set DATA_DIR before importing users.js (it reads process.env.DATA_DIR at module level)
37process.env.DATA_DIR = resolve(DATA_DIR);
38
39const users = require('../backend/server/users.js');
40
41try {
42 users.createUserWithKey('account-a', USER_A_KEY);
43 users.createUserWithKey('account-b', USER_B_KEY);
44
45 const result = {
46 'account-a': { userId: 'account-a', apiKey: USER_A_KEY },
47 'account-b': { userId: 'account-b', apiKey: USER_B_KEY },
48 };
49
50 console.log(JSON.stringify(result));
51 users.closeSystemDb();
52} catch (err) {
53 console.error('Failed to create test users:', err.message);
54 users.closeSystemDb();
55 process.exit(1);
56}