import { test, expect, type APIRequestContext } from '../../../../../job-tracker-ui/node_modules/@playwright/test'; const api = 'http://localhost:5302/api'; const password = 'E2ePassword123!'; async function login(request: APIRequestContext, email: string) { expect((await request.post(`${api}/auth/login`, { data: { email, password } })).status()).toBe(200); const me = await (await request.get(`${api}/auth/me`)).json(); const csrf = (await request.storageState()).cookies.find(cookie => cookie.name === 'XSRF-TOKEN')!.value; return { 'X-CSRF-TOKEN': csrf, 'X-Session-Context': me.sessionContext as string }; } test('P9 characterize retained RD21 stale writes separately from CV revision safety', async ({ playwright }, info) => { const admin = await playwright.request.newContext(); const first = await playwright.request.newContext(); const second = await playwright.request.newContext(); try { const adminHeaders = await login(admin, 'e2e@example.test'); const email = `p9-stale-${Date.now()}@example.test`; expect((await admin.post(`${api}/users`, { headers: adminHeaders, data: { email, password, roles: [] } })).status()).toBe(200); const a = await login(first, email); const b = await login(second, email); const submitted = { version: '1', contact: { fullName: 'Synthetic concurrent owner' }, summary: ['Initial summary'] }; expect((await first.put(`${api}/career/profile`, { headers: a, data: { profile: submitted } })).status()).toBe(200); const careerA = (await (await first.get(`${api}/career/profile`)).json()).profile; const careerB = (await (await second.get(`${api}/career/profile`)).json()).profile; careerA.summary = ['First editor committed summary']; careerB.contact.headline = 'Second editor headline'; expect((await first.put(`${api}/career/profile`, { headers: a, data: { profile: careerA } })).status()).toBe(200); const staleCareer = await second.put(`${api}/career/profile`, { headers: b, data: { profile: careerB } }); expect([200, 409]).toContain(staleCareer.status()); const careerAfter = (await (await first.get(`${api}/career/profile`)).json()).profile; const company = await first.post(`${api}/companies`, { headers: a, data: { name: 'Synthetic stale job fixture' } }); expect(company.status()).toBe(201); const created = await first.post(`${api}/jobapplications`, { headers: a, data: { companyId: (await company.json()).id, jobTitle: 'Original job title', status: 'Saved' } }); expect(created.status()).toBe(201); const jobId = (await created.json()).id; const jobA = await (await first.get(`${api}/jobapplications/${jobId}`)).json(); const jobB = await (await second.get(`${api}/jobapplications/${jobId}`)).json(); jobA.jobTitle = 'First editor committed title'; jobB.notes = 'Second editor notes'; expect([200, 204]).toContain((await first.put(`${api}/jobapplications/${jobId}`, { headers: a, data: jobA })).status()); const staleJob = await second.put(`${api}/jobapplications/${jobId}`, { headers: b, data: jobB }); expect([200, 204, 409]).toContain(staleJob.status()); const jobAfter = await (await first.get(`${api}/jobapplications/${jobId}`)).json(); const careerLostFirstEdit = careerAfter.summary[0] !== careerA.summary[0]; const jobLostFirstEdit = jobAfter.jobTitle !== jobA.jobTitle; await info.attach('phase9-rd21-characterization', { body: JSON.stringify({ id: 'RD21', career: { staleStatus: staleCareer.status(), expectedFirstSummary: careerA.summary, actualSummary: careerAfter.summary, lostFirstEdit: careerLostFirstEdit }, job: { staleStatus: staleJob.status(), expectedFirstTitle: jobA.jobTitle, actualTitle: jobAfter.jobTitle, lostFirstEdit: jobLostFirstEdit }, safetyQualification: careerLostFirstEdit || jobLostFirstEdit ? 'FAIL: stale writes can overwrite acknowledged edits' : 'Finite scenario retained prior edits', limitation: 'This passing characterization records observed behavior; it does not accept unsafe stale writes or provide a new revision contract. Creation-intent idempotency remains separate.' }, null, 2), contentType: 'application/json' }); } finally { await second.dispose(); await first.dispose(); await admin.dispose(); } });