diff --git a/common/src/ads/sponsored-consent.test.ts b/common/src/ads/sponsored-consent.test.ts index 3adeeb6c35..cc6b2a3b39 100644 --- a/common/src/ads/sponsored-consent.test.ts +++ b/common/src/ads/sponsored-consent.test.ts @@ -1,5 +1,5 @@ import { describe, expect, test } from 'bun:test' -import { readFileSync } from 'node:fs' +import { existsSync, readFileSync } from 'node:fs' import path from 'node:path' import { @@ -52,31 +52,49 @@ describe('every surface asks the same question', () => { const root = path.join(__dirname, '..', '..', '..') const read = (rel: string) => readFileSync(path.join(root, rel), 'utf8') - test.each([ - ['freebuff-desktop/electron/mcp-consent-bridge.cjs'], - ['freebuff-desktop/electron/consent-window.html'], - ])('%s carries the same sentence, verbatim', (rel) => { - expect(read(rel)).toContain(SPONSORED_CONSENT_SENTENCE.trim()) - }) + /** + * Whether this tree carries the file at `rel`. + * + * This repository is an export of the private source tree, and the export + * ships neither the web app nor `freebuff-desktop/`, which is not one of the + * in-scope paths in CONTRIBUTING.md. A case that reads one of those paths is + * asserting against a file that was never exported, so it dies with ENOENT + * here instead of skipping. Skipping on presence keeps the drift check running + * in the private tree, where the sources do exist, and reactivates it here by + * itself if the export ever starts including them. + */ + const isInThisTree = (rel: string) => existsSync(path.join(root, rel)) - // The IN-PLACE question (#3989) is asked by Desktop's bridge and by the CLI's - // dock from this constant; the two must be the same words. - test('the bridge carries the same in-place sentence, verbatim', () => { - expect(read('freebuff-desktop/electron/mcp-consent-bridge.cjs')).toContain( - SPONSORED_CONSENT_IN_PLACE_SENTENCE.trim(), + const BRIDGE = 'freebuff-desktop/electron/mcp-consent-bridge.cjs' + const WINDOW = 'freebuff-desktop/electron/consent-window.html' + + for (const rel of [BRIDGE, WINDOW]) { + test.skipIf(!isInThisTree(rel))( + `${rel} carries the same sentence, verbatim`, + () => { + expect(read(rel)).toContain(SPONSORED_CONSENT_SENTENCE.trim()) + }, ) - }) + } - test.each([ - ['freebuff-desktop/electron/mcp-consent-bridge.cjs'], - ['freebuff-desktop/electron/consent-window.html'], - ])( - '%s carries the same Windows no-sandbox sentence, verbatim (COD-642)', - (rel) => { - expect(read(rel)).toContain(SPONSORED_CONSENT_WINDOWS_FLOOR_SENTENCE) + // The IN-PLACE question (#3989) is asked by Desktop's bridge and by the CLI's + // dock from this constant; the two must be the same words. + test.skipIf(!isInThisTree(BRIDGE))( + 'the bridge carries the same in-place sentence, verbatim', + () => { + expect(read(BRIDGE)).toContain(SPONSORED_CONSENT_IN_PLACE_SENTENCE.trim()) }, ) + for (const rel of [BRIDGE, WINDOW]) { + test.skipIf(!isInThisTree(rel))( + `${rel} carries the same Windows no-sandbox sentence, verbatim (COD-642)`, + () => { + expect(read(rel)).toContain(SPONSORED_CONSENT_WINDOWS_FLOOR_SENTENCE) + }, + ) + } + test('the Windows sentence says the three things, in plain words', () => { expect(SPONSORED_CONSENT_WINDOWS_FLOOR_SENTENCE).toContain('no sandbox') expect(SPONSORED_CONSENT_WINDOWS_FLOOR_SENTENCE).toContain( @@ -87,21 +105,24 @@ describe('every surface asks the same question', () => { ) }) - test('the desktop window says the same thing when it cannot name who is asking', () => { - expect(read('freebuff-desktop/electron/consent-window.html')).toContain( - SPONSORED_CONSENT_NO_NAME, - ) - }) + test.skipIf(!isInThisTree(WINDOW))( + 'the desktop window says the same thing when it cannot name who is asking', + () => { + expect(read(WINDOW)).toContain(SPONSORED_CONSENT_NO_NAME) + }, + ) - test.each([ - ['freebuff-desktop/electron/mcp-consent-bridge.cjs'], - ['freebuff-desktop/electron/consent-window.html'], - ])('%s caps the name at the same length', (rel) => { + for (const rel of [BRIDGE, WINDOW]) { // Both of them, because the bridge clamps before it sends AND the page clamps what it is // given: the page is the last thing between an advertiser's name and a human's eyes, and one // U+202E there reverses our own sentence. - expect(read(rel)).toContain( - `MAX_NAME_CHARS = ${SPONSORED_CONSENT_MAX_NAME_CHARS}`, + test.skipIf(!isInThisTree(rel))( + `${rel} caps the name at the same length`, + () => { + expect(read(rel)).toContain( + `MAX_NAME_CHARS = ${SPONSORED_CONSENT_MAX_NAME_CHARS}`, + ) + }, ) - }) + } })