mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-10-20 05:56:53 +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.
95 lines
3.6 KiB
TypeScript
95 lines
3.6 KiB
TypeScript
/**
|
|
* Calendar-feed visibility e2e — proves the staged-booking gate holds on the
|
|
* one path that needs no session at all: the public /api/feed/trip/:token.ics.
|
|
*
|
|
* tests/e2e/feeds.e2e.test.ts cannot cover this. It mocks buildTripCalendar and
|
|
* never creates a reservations table, so it owns the calendar parts and the SQL
|
|
* filter is invisible to it. This file runs the REAL CalendarService against a
|
|
* real migrated SQLite instead, and asserts on the bytes a calendar client
|
|
* would receive.
|
|
*/
|
|
import { describe, it, expect, beforeAll, afterAll, vi } from 'vitest';
|
|
import request from 'supertest';
|
|
import type { Server } from 'http';
|
|
import { Test } from '@nestjs/testing';
|
|
|
|
vi.mock('../../src/db/database', async () => {
|
|
const { createSnapshotTestDb, buildDbMock } = await import('../helpers/db-mock');
|
|
return buildDbMock(createSnapshotTestDb());
|
|
});
|
|
|
|
import { db } from '../../src/db/database';
|
|
import { FeedsModule } from '../../src/nest/feeds/feeds.module';
|
|
import { TrekExceptionFilter } from '../../src/nest/common/trek-exception.filter';
|
|
import { TestUnitOfWorkModule } from '../helpers/test-uow';
|
|
import { createTestMikroOrmModule, createTestOrm, type TestOrm } from '../helpers/test-orm';
|
|
import { makeUser } from '../helpers/factories/users';
|
|
import { makeTrip, readTripDays } from '../helpers/factories/trips';
|
|
import { makeReservation } from '../helpers/factories/reservations';
|
|
|
|
describe('Calendar feed visibility e2e (real CalendarService over temp SQLite)', () => {
|
|
let server: Server;
|
|
let app: Awaited<ReturnType<typeof build>>;
|
|
let feedToken: string;
|
|
let orm: TestOrm;
|
|
|
|
async function build() {
|
|
const moduleRef = await Test.createTestingModule({ imports: [await TestUnitOfWorkModule.forRoot(db), await createTestMikroOrmModule(db), FeedsModule] }).compile();
|
|
const nest = moduleRef.createNestApplication();
|
|
nest.useGlobalFilters(new TrekExceptionFilter());
|
|
await nest.init();
|
|
return nest;
|
|
}
|
|
|
|
beforeAll(async () => {
|
|
// Seeded through MikroORM (tests/helpers/factories), not raw SQL.
|
|
orm = await createTestOrm(db);
|
|
const { user } = await makeUser(orm, { username: 'e2e-user', email: 'e2e@example.test' });
|
|
feedToken = 'feed-token-visibility';
|
|
const trip = await makeTrip(orm, user.id, {
|
|
title: 'Kyoto',
|
|
start_date: '2026-09-01',
|
|
end_date: '2026-09-05',
|
|
feed_token: feedToken,
|
|
});
|
|
const [firstDay] = await readTripDays(orm, trip.id);
|
|
await makeReservation(orm, trip.id, {
|
|
day: firstDay.id,
|
|
title: 'Parked Flight',
|
|
type: 'flight',
|
|
status: 'confirmed',
|
|
reservation_time: '2026-09-01T08:00',
|
|
confirmation_number: 'SECRET1',
|
|
ingest_state: 'staged',
|
|
});
|
|
await makeReservation(orm, trip.id, {
|
|
day: firstDay.id,
|
|
title: 'Booked Flight',
|
|
type: 'flight',
|
|
status: 'confirmed',
|
|
reservation_time: '2026-09-01T12:00',
|
|
confirmation_number: 'OPEN1',
|
|
});
|
|
|
|
app = await build();
|
|
server = app.getHttpServer();
|
|
});
|
|
|
|
afterAll(async () => {
|
|
await app.close();
|
|
await orm.close();
|
|
});
|
|
|
|
it('the public feed serves the live booking and neither the staged one nor its confirmation number', async () => {
|
|
const res = await request(server).get(`/api/feed/trip/${feedToken}.ics`);
|
|
|
|
expect(res.status).toBe(200);
|
|
expect(res.headers['content-type']).toContain('text/calendar');
|
|
expect(res.text).toContain('SUMMARY:Booked Flight');
|
|
expect(res.text).not.toContain('Parked Flight');
|
|
// The number rides in the DESCRIPTION, so search the whole document.
|
|
expect(res.text).not.toContain('SECRET1');
|
|
expect(res.text).toContain('OPEN1');
|
|
});
|
|
});
|