feat(api): seed reference activities and enable CORS for the worker SPA

This commit is contained in:
Bas van Rossem
2026-06-17 15:54:52 +02:00
parent 85184d3287
commit 35f9aa5574
6 changed files with 122 additions and 2 deletions

View File

@@ -0,0 +1,29 @@
import { describe, it, expect } from 'vitest';
import { createApp } from '../src/app';
const ORIGIN = 'http://localhost:5173';
describe('cors', () => {
it('answers a CORS preflight for the SPA origin', async () => {
const app = createApp();
const res = await app.request('/api/activities', {
method: 'OPTIONS',
headers: {
Origin: ORIGIN,
'Access-Control-Request-Method': 'GET',
},
});
expect(res.headers.get('access-control-allow-origin')).toBe(ORIGIN);
const allowMethods = res.headers.get('access-control-allow-methods') ?? '';
expect(allowMethods).toContain('GET');
});
it('exposes set-auth-token to the SPA origin', async () => {
const app = createApp();
const res = await app.request('/api/activities', {
headers: { Origin: ORIGIN },
});
const expose = (res.headers.get('access-control-expose-headers') ?? '').toLowerCase();
expect(expose).toContain('set-auth-token');
});
});

View File

@@ -0,0 +1,34 @@
import { describe, it, expect } from 'vitest';
import { inArray, eq } from 'drizzle-orm';
import { seed } from '../src/db/seed';
import { db } from '../src/db/client';
import { activities } from '../src/db/schema';
const SEED_NAMES = ['Leerrand', 'Frezen', 'Slijpen', 'Bekleden', 'Afwerken', 'Printen'];
describe('seed', () => {
it('seeds the reference activities idempotently', async () => {
await seed();
const first = await db
.select()
.from(activities)
.where(inArray(activities.name, SEED_NAMES));
const countFirst = first.length;
await seed();
const second = await db
.select()
.from(activities)
.where(inArray(activities.name, SEED_NAMES));
expect(second.length).toBe(countFirst);
expect(countFirst).toBe(SEED_NAMES.length);
const printen = await db
.select()
.from(activities)
.where(eq(activities.name, 'Printen'));
expect(printen).toHaveLength(1);
expect(printen[0]?.insoleTypes).toEqual(['3D']);
});
});