import { execFileSync } from 'node:child_process' import { randomUUID } from 'node:crypto' import { test, expect } from '@playwright/test' import { readStackManifest } from './stack-manifest.mjs' function articleAndJobSnapshot(stack) { const env = { ...process.env, PGHOST: stack.databaseURL.hostname, PGPORT: stack.databaseURL.port || '5432', PGUSER: decodeURIComponent(stack.databaseURL.username), PGPASSWORD: decodeURIComponent(stack.databaseURL.password), PGDATABASE: decodeURIComponent(stack.databaseURL.pathname.slice(1)), PGSSLMODE: stack.databaseURL.searchParams.get('sslmode') || 'disable', } const sql = `SELECT (SELECT count(*)::text || ':' || coalesce(md5(string_agg(md5(row_to_json(a)::text), ',' ORDER BY row_to_json(a)::text)), md5('')) FROM public.articles a) || '|' || (SELECT count(*)::text || ':' || coalesce(md5(string_agg(md5(row_to_json(s)::text), ',' ORDER BY row_to_json(s)::text)), md5('')) FROM public.article_symbols s) || '|' || (SELECT count(*)::text || ':' || coalesce(md5(string_agg(md5(row_to_json(j)::text), ',' ORDER BY row_to_json(j)::text)), md5('')) FROM public.scrape_jobs j);` return execFileSync(stack.psqlPath, ['--no-psqlrc', '--tuples-only', '--no-align', '--set', 'ON_ERROR_STOP=1', '--command', sql], { env, encoding: 'utf8', stdio: ['ignore', 'pipe', 'pipe'], timeout: 10_000, }).trim() } async function login(page, baseURL, email, password) { await page.goto(new URL('/login', baseURL).href) await page.getByLabel('Adresse email').fill(email) await page.getByLabel('Mot de passe').fill(password) await page.getByRole('button', { name: 'Se connecter' }).click() await expect(page).toHaveURL(new URL('/', baseURL).href) } test('admin creates a user who follows a fake company and generates a private summary without article/job writes', async ({ page, request }) => { const stack = readStackManifest() await page.context().route('**/*', async route => { const host = new URL(route.request().url()).hostname if (host !== '127.0.0.1' && host !== '[::1]') return route.abort('blockedbyclient') return route.continue() }) const baseURL = stack.frontendUrl const fixture = stack.fixtures const email = `e2e-${randomUUID()}@example.test` const password = `E2e-${randomUUID()}!` const before = articleAndJobSnapshot(stack) await test.step('admin bootstrap login and account creation', async () => { await login(page, baseURL, stack.bootstrapAdmin.email, stack.bootstrapAdmin.password) await page.goto(new URL('/admin/users', baseURL).href) await page.getByRole('button', { name: 'Créer un compte' }).click() const form = page.getByRole('form', { name: 'Créer un compte' }) await form.getByLabel('Email').fill(email) await form.getByLabel('Mot de passe initial').fill(password) await form.getByRole('button', { name: 'Créer', exact: true }).click() await expect(form.getByRole('status')).toHaveText('Compte créé avec succès.') await page.getByRole('button', { name: 'Déconnexion' }).click() }) let companySearchRequest let companyFollowRequest page.on('request', request => { const url = new URL(request.url()) if (url.pathname === '/api/companies/search') companySearchRequest = url if (url.pathname === '/api/me/assets' && request.method() === 'POST') companyFollowRequest = request.postDataJSON() }) await test.step('new user searches by company name and follows the selected local catalog company', async () => { await login(page, baseURL, email, password) // Seed a pre-existing legacy follow through the compatibility API so the // real Watchlist page can prove it remains present after a company follow. const token = await page.evaluate(() => localStorage.getItem('token')) const legacy = await request.post(new URL('/api/me/assets', stack.apiUrl).href, { headers: { Authorization: `Bearer ${token}` }, data: { symbol: 'LEGACYIDX', name: 'Historic index follow' }, }) expect(legacy.status()).toBe(201) await page.goto(new URL('/assets', baseURL).href) const oldFollows = page.getByRole('region', { name: /autres suivis existants/i }) await expect(oldFollows).toContainText('Historic index follow') const query = page.getByRole('textbox', { name: 'Nom ou nom courant de l’entreprise' }) await query.fill(fixture.companyQuery) await page.getByRole('button', { name: 'Rechercher' }).click() const results = page.getByRole('region', { name: 'Résultats des entreprises' }) await expect(results.getByText(fixture.companyName)).toBeVisible() await expect(results).toContainText('E2E Country') for (const privateValue of [fixture.companySymbol, fixture.companyFIGI, fixture.companyID]) { await expect(results.getByText(privateValue, { exact: false })).toHaveCount(0) } await results.getByRole('button', { name: 'Suivre cette entreprise' }).click() await expect(page.getByRole('region', { name: 'Entreprises suivies' })).toContainText(fixture.companyName) await expect(oldFollows).toContainText('Historic index follow') await expect(companySearchRequest?.pathname).toBe('/api/companies/search') await expect(companySearchRequest?.searchParams.get('q')).toBe(fixture.companyQuery) await expect(companySearchRequest?.searchParams.get('limit')).toBe('20') expect(companyFollowRequest).toEqual({ company_id: fixture.companyID }) }) await test.step('user saves language and ranking preferences', async () => { await page.goto(new URL('/preferences', baseURL).href) await page.getByLabel('Langue des synthèses').selectOption('en') await page.getByLabel('Modèle de synthèse', { exact: true }).selectOption(fixture.summaryModelID) await page.getByLabel('Informations utilisées par Laya pour classer les articles').selectOption('title_content') await page.getByRole('button', { name: 'Enregistrer' }).click() await expect(page.getByRole('status')).toHaveText('Préférences enregistrées.') }) await test.step('fake scraper and Laya produce an aggregate summary', async () => { await page.goto(new URL('/', baseURL).href) await page.getByRole('button', { name: 'Générer une synthèse' }).click() await expect(page.getByText(fixture.summarySentinel, { exact: false })).toBeVisible({ timeout: 90_000 }) await expect(page.getByText('Langue de cette synthèse : English', { exact: true })).toBeVisible() for (const forbidden of [fixture.articleTitle, fixture.articleURL, fixture.articleContent, fixture.candidateID]) { await expect(page.getByText(forbidden, { exact: false })).toHaveCount(0) } }) await test.step('legacy article/job routes stay closed and database rows remain unchanged', async () => { const api = stack.apiUrl expect((await request.get(new URL('/api/articles', api).href)).status()).toBe(404) expect((await request.get(new URL('/api/articles/e2e-forbidden', api).href)).status()).toBe(404) expect((await request.get(new URL('/api/admin/scrape-jobs', api).href)).status()).toBe(404) expect((await request.post(new URL('/api/admin/scrape-jobs/trigger', api).href)).status()).toBe(404) expect(articleAndJobSnapshot(stack)).toBe(before) }) await test.step('fake-provider audit confirms every dependency stayed local', async () => { const response = await request.get(new URL(stack.fakeAuditPath, stack.fakeControlUrl).href) expect(response.ok()).toBeTruthy() const events = await response.json() expect(events.openfigiSearches).toBe(0) expect(events.companySearches).toBeGreaterThan(0) expect(events.companyResolutions).toBeGreaterThan(0) expect(events.scraperCalls).toBeGreaterThan(0) expect(events.layaRankingCalls).toBeGreaterThan(0) expect(events.layaSummaryCalls).toBeGreaterThan(0) const token = await page.evaluate(() => localStorage.getItem('token')) const history = await request.get(new URL('/api/summaries/generations', stack.apiUrl).href, { headers: { Authorization: `Bearer ${token}` }, }) expect(history.status()).toBe(200) const generations = await history.json() expect(generations.data.length).toBe(1) const outcome = generations.data[0].outcome expect(outcome.status).toBe('succeeded') const summaries = await request.get(new URL('/api/summaries', stack.apiUrl).href, { headers: { Authorization: `Bearer ${token}` } }) const saved = await summaries.json() expect(saved.data[0].summary_model_id).toBe(fixture.summaryModelID) expect(saved.data[0].provider_name).toBe('ollama') expect(saved.data[0].model_name).toBe('fake-summary') expect(events.events.filter(event => event.kind === 'layaSummaryCalls').every(event => event.model === 'fake-summary')).toBeTruthy() expect(outcome.total_candidates).toBe(1) expect(outcome.source_stats).toHaveLength(1) const serialized = JSON.stringify(outcome) const candidateIDs = events.events.filter(event => event.kind === 'layaRankingCalls').map(event => event.candidate_id) expect(candidateIDs.length).toBeGreaterThan(0) for (const forbidden of [fixture.articleTitle, fixture.articleURL, fixture.articleContent, ...candidateIDs]) { expect(serialized).not.toContain(forbidden) await expect(page.getByText(forbidden, { exact: false })).toHaveCount(0) } for (const field of ['title', 'url', 'content', 'prompt', 'candidate_id']) { expect(serialized).not.toContain(`"${field}":`) } }) })