/** * Access callbacks of a project (`cookieScanner.access`, the doctor's `access`), owner's decision Q4, 1.18.0: * still allowed on any truthy answer, but an answer that is not a boolean is reported once per option, so a * project sees it before the next minor version allows only `true`. Every call site goes through one function. */ import { readFileSync } from 'node:fs' import { beforeEach, describe, expect, test, vi } from 'vitest' import { accessAllowed, resetAccessWarnings } from '../src/modules/shared/accessAnswer.js' beforeEach(() => resetAccessWarnings()) describe('accessAllowed', () => { test('true and false are read as they are, without a word', () => { const warn = vi.fn() expect(accessAllowed('cookieScanner.access', true, { warn })).toBe(true) expect(accessAllowed('cookieScanner.access', false, { warn })).toBe(false) expect(warn).not.toHaveBeenCalled() }) test.each([ [{ id: 1 }, true, 'an object'], ['no', true, 'a string'], [1, true, 'a number'], [null, false, 'null'], [undefined, false, 'a undefined'], ])('%j is read as before (%s) and reported once', (answer, allowed, kind) => { const warn = vi.fn() expect(accessAllowed('doctor.access', answer, { warn })).toBe(allowed) expect(accessAllowed('doctor.access', answer, { warn })).toBe(allowed) expect(warn).toHaveBeenCalledTimes(1) expect(warn.mock.calls[0][0]).toContain(`doctor.access returned ${kind}`) expect(warn.mock.calls[0][0]).toContain('only `true` allows') }) test('each option is reported on its own', () => { const warn = vi.fn() accessAllowed('doctor.access', 'x', { warn }) accessAllowed('cookieScanner.access', 'x', { warn }) expect(warn).toHaveBeenCalledTimes(2) }) }) test('every call of a project access callback goes through accessAllowed', () => { const sites = ['src/modules/consent/scanner/endpoint.ts', 'src/modules/consent/scanner/approval.ts', 'src/modules/consent/scanner/crawl/endpoints.ts', 'src/modules/doctor/status.ts'] for (const file of sites) { const text = readFileSync(file, 'utf8') const calls = text.match(/await options\.access\(\{ req \}\)/g) ?? [] expect(calls.length, file).toBeGreaterThan(0) expect(text.match(/accessAllowed\('[\w.]+', await options\.access\(\{ req \}\)\)/g)?.length, file).toBe(calls.length) } })