/** * Gate for candidate availability - the three-strikes no-show rule. * * The rule: submitted to three DIFFERENT companies, never attended an * interview, and the account is paused until the person says they are still * looking. Everything below is exercised against the live API and asserted in * the database. * * What it proves, in order of what would hurt most if wrong: * - two submissions do NOT suspend (a false pause costs a real candidate) * - three DIFFERENT companies do * - three rounds at ONE company do not (that is one employer, not three) * - attending an interview immunises * - an operator cannot lift it; only the candidate can * - nothing is deleted or withdrawn by a suspension * * BACKEND_LOG=... node scripts/e2e/p6b-availability.mjs */ import { api, sql, sqlOne, section, check, info, summary, createVerifiedUser, uniqueEmail, makeAdmin, } from './lib.mjs'; const JD = `Backend engineer for a payments team. You will own the ledger, the reconciliation jobs and the money-movement APIs. Strong SQL and Node. Remote.`; let admin; async function makeCampaign(company, title) { const res = await api('/admin/campaigns', { method: 'POST', token: admin.accessToken, body: { title, company, jobDescription: JD }, }); if (res.status !== 201) throw new Error('campaign create failed: ' + JSON.stringify(res.body)); await api(`/admin/campaigns/${res.body.id}`, { method: 'PATCH', token: admin.accessToken, body: { status: 'collecting' }, }); return res.body; } /** Put a user on a campaign and advance them to submitted. */ async function submitTo(campaign, user) { await api(`/campaigns/public/${campaign.slug}/claim`, { method: 'POST', token: user.accessToken, body: {}, }); const candidateId = sqlOne( `select id from campaign_candidates where "campaignId" = '${campaign.id}' and "userId" = '${user.userId}'`, ); const res = await api(`/admin/campaigns/${campaign.id}/submit`, { method: 'POST', token: admin.accessToken, body: { candidateIds: [candidateId] }, }); return { candidateId, res }; } const availabilityOf = (userId) => sqlOne(`select availability from "user" where id = '${userId}'`); async function main() { section('setup'); const adminEmail = uniqueEmail('avail-admin'); admin = await createVerifiedUser({ name: 'Availability Admin', email: adminEmail }); makeAdmin(adminEmail); check('admin ready', Boolean(admin.userId)); const a = await makeCampaign('Northwind Labs', 'Backend Engineer'); const b = await makeCampaign('Klaxon', 'Backend Engineer'); const c = await makeCampaign('Ardent Systems', 'Backend Engineer'); check('three campaigns at three companies', Boolean(a.id && b.id && c.id)); section('a new candidate starts as looking'); const ghost = await createVerifiedUser({ name: 'Ghost Candidate', email: uniqueEmail('ghost') }); check('availability defaults to looking', availabilityOf(ghost.userId) === 'looking'); section('two submissions do not suspend'); await submitTo(a, ghost); check('still looking after one', availabilityOf(ghost.userId) === 'looking'); const second = await submitTo(b, ghost); check('still looking after two', availabilityOf(ghost.userId) === 'looking', availabilityOf(ghost.userId)); check('and the API reports nobody was suspended', second.res.body?.suspended === 0, JSON.stringify(second.res.body)); section('the third different company suspends'); const third = await submitTo(c, ghost); check('suspended', availabilityOf(ghost.userId) === 'suspended', availabilityOf(ghost.userId)); check('the API says so', third.res.body?.suspended === 1, JSON.stringify(third.res.body)); const reason = sqlOne(`select "suspensionReason" from "user" where id = '${ghost.userId}'`); check('with a reason the candidate can read', Boolean(reason) && reason.includes('3 companies'), reason); check( 'and a timestamp', sqlOne(`select "availabilityUpdatedAt" is not null from "user" where id = '${ghost.userId}'`) === 't', ); section('a suspension withdraws nothing'); check( 'their applications are all still there', Number(sqlOne(`select count(*) from campaign_candidates where "userId" = '${ghost.userId}'`)) === 3, ); check( 'all still marked submitted', Number( sqlOne( `select count(*) from campaign_candidates where "userId" = '${ghost.userId}' and status = 'submitted'`, ), ) === 3, ); const stillWorks = await api('/auth/me', { token: ghost.accessToken }); check('and they can still sign in and use the product', stillWorks.status === 200); section('only the candidate can lift it'); const asSelf = await api('/me/availability', { token: ghost.accessToken }); check('they can see their own status', asSelf.status === 200, 'got ' + asSelf.status); check('which explains itself', asSelf.body?.availability === 'suspended'); check('and shows the history behind it', asSelf.body?.submittedToCompanies === 3, JSON.stringify(asSelf.body)); check('and the threshold', asSelf.body?.suspendsAfter === 3); // There is deliberately no admin route for this. Its absence is the feature: // an operator clearing it would be answering "are you still looking?" on // somebody else's behalf. const noAdminRoute = await api(`/admin/users/${ghost.userId}/availability`, { method: 'PATCH', token: admin.accessToken, body: { availability: 'looking' }, }); check( 'no admin route exists to clear it', noAdminRoute.status === 404, 'got ' + noAdminRoute.status, ); const lifted = await api('/me/availability', { method: 'PATCH', token: ghost.accessToken, body: { availability: 'looking' }, }); check('the candidate can lift it themselves', lifted.status === 200, 'got ' + lifted.status); check('back to looking', availabilityOf(ghost.userId) === 'looking'); check( 'and the reason is cleared', sqlOne(`select "suspensionReason" is null from "user" where id = '${ghost.userId}'`) === 't', ); section('three rounds at ONE company is not three companies'); const solo = await createVerifiedUser({ name: 'Single Employer', email: uniqueEmail('solo') }); const r1 = await makeCampaign('Loam', 'Backend Engineer'); const r2 = await makeCampaign('Loam', 'Backend Engineer II'); const r3 = await makeCampaign('Loam', 'Backend Engineer III'); await submitTo(r1, solo); await submitTo(r2, solo); await submitTo(r3, solo); check( 'still looking - one employer hiring three times is not three no-shows', availabilityOf(solo.userId) === 'looking', availabilityOf(solo.userId), ); section('attending an interview immunises'); const attender = await createVerifiedUser({ name: 'Shows Up', email: uniqueEmail('attends') }); const { candidateId } = await submitTo(a, attender); // Mark this one as actually interviewed, the way the interview flow does. sql( `update campaign_candidates set "completedAt" = now(), status = 'submitted' where id = '${candidateId}'`, ); await submitTo(b, attender); await submitTo(c, attender); check( 'not suspended - they turned up', availabilityOf(attender.userId) === 'looking', availabilityOf(attender.userId), ); section('suspension does not re-fire on an already-suspended account'); const repeat = await createVerifiedUser({ name: 'Repeat Ghost', email: uniqueEmail('repeat-ghost') }); await submitTo(a, repeat); await submitTo(b, repeat); await submitTo(c, repeat); check('suspended once', availabilityOf(repeat.userId) === 'suspended'); const d = await makeCampaign('Verity Health', 'Backend Engineer'); const fourth = await submitTo(d, repeat); check( 'a fourth submission reports no NEW suspension', fourth.res.body?.suspended === 0, JSON.stringify(fourth.res.body), ); info('accounts suspended by this run: ' + sqlOne("select count(*) from \"user\" where availability = 'suspended'")); summary('Availability and the three-strikes rule'); } main().catch((e) => { console.error(e); process.exitCode = 1; });