Repository Public
1/**2 * =============================================================================3 * SP BOUCHER | SOFTWARE & RESEARCH4 * ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING5 * =============================================================================6 *7 * Project : Note SPBoucher AI8 * File : tests/e2e/global-teardown.ts9 * Description : after the whole run — attachments uploaded by the suites10 * whose page was deleted by a spec's clean-up stay behind as11 * orphans (`page_id` null), with their text in the search12 * index: a later run then finds them « by meaning ». They13 * are removed here, limited to the orphans created since14 * this run started (other clients of the scratch instance15 * keep theirs).16 * Module : tests17 * Repository : https://git.spboucher.ai/note-spboucher-ai18 * Project Website : https://note.spboucher.ai19 * Created : 2026-11-1920 * Last Modified : 2026-11-1921 *22 * Author : Simon-Pierre Boucher23 * Position : Professor of Finance24 * Institution : Université du Québec en Outaouais (UQO)25 * Organization : SP Boucher26 * Website : https://spboucher.ai27 * Git : https://git.spboucher.ai28 *29 * Preferred Contact: contact@spboucher.ai30 * Development : dev@spboucher.ai31 * Support : support@spboucher.ai32 * Security/Admin : admin@spboucher.ai33 *34 * Copyright (c) 202635 * Simon-Pierre Boucher36 * All rights reserved where applicable. This file is governed by the37 * applicable project license, copyright notices and relevant law.38 * Existing third-party rights and notices remain unaffected.39 *40 * License : None specified (no license file in this repository)41 *42 * Do not embed credentials, private keys, access tokens,43 * certificates or confidential personal information.44 *45 * =============================================================================46 * END OF SP BOUCHER SOURCE HEADER47 * =============================================================================48 */4950import postgres from "postgres";51import "../helpers/testdb";52export default async function globalTeardown() {53 const sql = postgres(process.env.DATABASE_URL!, { max: 1, onnotice: () => {} }); // its own connection: the one of the helpers was closed by the global set-up54 const since = new Date(Number(process.env.E2E_STARTED ?? Date.now()));55 try {56 const ids = (await sql`select id from attachments where page_id is null and created_at >= ${since}`).map((r) => r.id as string);57 if (ids.length) { await sql`delete from chunks where source_type = 'attachment' and source_id = any(${ids}::uuid[])`; await sql`delete from index_state where source_type = 'attachment' and source_id = any(${ids}::uuid[])`; await sql`delete from attachments where id = any(${ids}::uuid[])`; await sql`delete from sync_tombstones where id = any(${ids}::uuid[])`; }58 // index entries of pages removed by a spec's clean-up (deleted in SQL, so no un-index job ran)59 await sql`delete from chunks c where c.source_type = 'page' and c.created_at >= ${since} and not exists (select 1 from pages p where p.id = c.source_id)`;60 // index entries whose attachment no longer exists (deleted by a spec's clean-up without its chunks)61 await sql`delete from chunks c where c.source_type = 'attachment' and c.created_at >= ${since} and not exists (select 1 from attachments a where a.id = c.source_id)`;62 } finally { await sql.end(); }63}64