Branchwell

Jobley / website · jobley.ai / test / helpers.mjs

1import { createHash } from 'node:crypto';
2import { fileURLToPath } from 'node:url';
3import { dirname, join } from 'node:path';
4import { migrate, openD1 } from '../tools/d1-local.mjs';
5import { handle } from '../src/api.js';
6
7const root = join(dirname(fileURLToPath(import.meta.url)), '..');
8// A curator session, as if the curator had signed in with a passkey. People only sign in with passkeys.
9export const ADMIN = 'jl_session_test-curator-session-0123456789abcdef';
10
11export function makeEnv(extra = {}) {
12 const DB = migrate(openD1(), join(root, 'migrations'));
13 DB.raw.prepare(`INSERT INTO sessions (token_hash, role, user_id, created_at, expires_at) VALUES (?, 'curator', 'curator', ?, '9999-12-31T00:00:00.000Z')`)
14 .run(createHash('sha256').update(ADMIN).digest('hex'), new Date().toISOString());
15 return { DB, ALLOW_NO_TURNSTILE: '1', ...extra };
16}
17
18export async function call(env, method, path, { body, token, client, ip = '203.0.113.7', headers = {} } = {}) {
19 const h = { ...headers, 'cf-connecting-ip': ip };
20 if (body !== undefined) h['content-type'] = 'application/json';
21 if (token) h.authorization = `Bearer ${token}`;
22 if (client) h['x-client'] = client;
23 const res = await handle(new Request(`https://jobley.test${path}`, { method, headers: h, body: body === undefined ? undefined : typeof body === 'string' ? body : JSON.stringify(body) }), env);
24 const text = await res.text();
25 let data = null;
26 try { data = JSON.parse(text); } catch { /* not json */ }
27 return { status: res.status, data, headers: res.headers };
28}
29
30export const JOB = {
31 title: 'Support Engineer', company: 'Fernhollow Tools', company_url: 'https://fernhollow.example', location: 'United States',
32 workplace: 'remote', employment_type: 'full_time', category: 'support', pay_min: 210000, pay_max: 240000, pay_period: 'year',
33 description: 'Help the people who use our scheduling tools. You will answer questions, write the help pages and work with the engineers on fixes. Sample text for tests.',
34 apply_url: 'https://fernhollow.example/jobs/support-engineer', contact_name: 'Rowan Vale', contact_email: 'rowan@fernhollow.example',
35};
36export const CANDIDATE = {
37 name: 'Mika Okafor', email: 'Mika@Example.org', location: 'Lisbon', headline: 'Support engineer, six years',
38 looking_for: 'Remote support or success roles at small product teams.', about: 'I like untangling hard tickets.',
39 links: ['https://example.org/mika'], resume_url: 'https://example.org/mika/resume.pdf', consent: true,
40};
41export const MODERATOR = {
42 name: 'Jo Lindqvist', email: 'jo@example.org', timezone: 'Europe/Stockholm', hours_per_week: 3,
43 why: 'I have hired for small teams for years and I know what a misleading post looks like.', experience: 'Ran hiring at two small studios.',
44 links: ['https://example.org/jo'], agree: true,
45};