|
| 1 | +import { Buffer } from 'node:buffer' |
| 2 | +import { describe, expect, test, vi } from 'vitest' |
| 3 | +import { attw, type AttwOptions } from './attw.ts' |
| 4 | +import type { ResolvedConfig } from '../../config/types.ts' |
| 5 | +import type { |
| 6 | + CheckResult, |
| 7 | + Problem, |
| 8 | + ResolutionKind, |
| 9 | +} from '@arethetypeswrong/core' |
| 10 | + |
| 11 | +const resolutionKinds: ResolutionKind[] = [ |
| 12 | + 'node10', |
| 13 | + 'node16-cjs', |
| 14 | + 'node16-esm', |
| 15 | + 'bundler', |
| 16 | +] |
| 17 | + |
| 18 | +function createCheckResult( |
| 19 | + problems: [Problem, ResolutionKind[]][], |
| 20 | +): CheckResult { |
| 21 | + return { |
| 22 | + packageName: 'pkg', |
| 23 | + packageVersion: '1.0.0', |
| 24 | + buildTools: {}, |
| 25 | + types: { kind: 'included' }, |
| 26 | + programInfo: {} as any, |
| 27 | + problems: problems.map(([problem]) => problem), |
| 28 | + entrypoints: { |
| 29 | + '.': { |
| 30 | + subpath: '.', |
| 31 | + hasTypes: true, |
| 32 | + isWildcard: false, |
| 33 | + resolutions: Object.fromEntries( |
| 34 | + resolutionKinds.map((resolutionKind) => [ |
| 35 | + resolutionKind, |
| 36 | + { |
| 37 | + name: '.', |
| 38 | + resolutionKind, |
| 39 | + visibleProblems: problems.flatMap(([, kinds], index) => |
| 40 | + kinds.includes(resolutionKind) ? [index] : [], |
| 41 | + ), |
| 42 | + }, |
| 43 | + ]), |
| 44 | + ) as any, |
| 45 | + }, |
| 46 | + }, |
| 47 | + } |
| 48 | +} |
| 49 | + |
| 50 | +async function runAttw( |
| 51 | + checkResult: CheckResult, |
| 52 | + attwOptions: Omit<AttwOptions, 'module'>, |
| 53 | +) { |
| 54 | + const logger = { warn: vi.fn(), error: vi.fn(), success: vi.fn() } |
| 55 | + const options = { |
| 56 | + pkg: {}, |
| 57 | + nameLabel: undefined, |
| 58 | + logger, |
| 59 | + attw: { |
| 60 | + level: 'error', |
| 61 | + ...attwOptions, |
| 62 | + module: { |
| 63 | + createPackageFromTarballData: () => ({}), |
| 64 | + checkPackage: () => Promise.resolve(checkResult), |
| 65 | + }, |
| 66 | + }, |
| 67 | + } as unknown as ResolvedConfig |
| 68 | + await attw(options, Buffer.from('') as Buffer<ArrayBuffer>) |
| 69 | + return logger |
| 70 | +} |
| 71 | + |
| 72 | +const internalResolutionError: Problem = { |
| 73 | + kind: 'InternalResolutionError', |
| 74 | + resolutionOption: 'node10', |
| 75 | + fileName: '/node_modules/pkg/client.d.ts', |
| 76 | + pos: 0, |
| 77 | + end: 0, |
| 78 | + moduleSpecifier: 'pkg', |
| 79 | + resolutionMode: undefined, |
| 80 | + trace: [], |
| 81 | +} |
| 82 | + |
| 83 | +const cjsResolvesToEsm: Problem = { |
| 84 | + kind: 'CJSResolvesToESM', |
| 85 | + entrypoint: '.', |
| 86 | + resolutionKind: 'node16-cjs', |
| 87 | +} |
| 88 | + |
| 89 | +describe('attw profile', () => { |
| 90 | + test('ignores file-based problems only visible from ignored resolutions', async () => { |
| 91 | + const checkResult = createCheckResult([ |
| 92 | + [internalResolutionError, ['node10']], |
| 93 | + [cjsResolvesToEsm, ['node16-cjs']], |
| 94 | + ]) |
| 95 | + |
| 96 | + const logger = await runAttw(checkResult, { profile: 'esm-only' }) |
| 97 | + expect(logger.error).not.toHaveBeenCalled() |
| 98 | + expect(logger.success).toHaveBeenCalled() |
| 99 | + }) |
| 100 | + |
| 101 | + test('keeps file-based problems visible from required resolutions', async () => { |
| 102 | + const checkResult = createCheckResult([ |
| 103 | + [internalResolutionError, ['node10', 'bundler']], |
| 104 | + ]) |
| 105 | + |
| 106 | + const logger = await runAttw(checkResult, { profile: 'esm-only' }) |
| 107 | + expect(logger.error).toHaveBeenCalledOnce() |
| 108 | + expect(logger.error.mock.calls[0].join(' ')).toContain( |
| 109 | + 'Internal resolution error', |
| 110 | + ) |
| 111 | + }) |
| 112 | + |
| 113 | + test('strict reports every problem', async () => { |
| 114 | + const checkResult = createCheckResult([ |
| 115 | + [internalResolutionError, ['node10']], |
| 116 | + [cjsResolvesToEsm, ['node16-cjs']], |
| 117 | + ]) |
| 118 | + |
| 119 | + const logger = await runAttw(checkResult, { profile: 'strict' }) |
| 120 | + const message = logger.error.mock.calls[0].join(' ') |
| 121 | + expect(message).toContain('Internal resolution error') |
| 122 | + expect(message).toContain('CJS resolves to ESM') |
| 123 | + }) |
| 124 | + |
| 125 | + test('node16 only ignores node10', async () => { |
| 126 | + const checkResult = createCheckResult([ |
| 127 | + [internalResolutionError, ['node10']], |
| 128 | + [cjsResolvesToEsm, ['node16-cjs']], |
| 129 | + ]) |
| 130 | + |
| 131 | + const logger = await runAttw(checkResult, { profile: 'node16' }) |
| 132 | + const message = logger.error.mock.calls[0].join(' ') |
| 133 | + expect(message).not.toContain('Internal resolution error') |
| 134 | + expect(message).toContain('CJS resolves to ESM') |
| 135 | + }) |
| 136 | + |
| 137 | + test('ignoreRules still applies', async () => { |
| 138 | + const checkResult = createCheckResult([[cjsResolvesToEsm, ['node16-cjs']]]) |
| 139 | + |
| 140 | + const logger = await runAttw(checkResult, { |
| 141 | + profile: 'strict', |
| 142 | + ignoreRules: ['cjs-resolves-to-esm'], |
| 143 | + }) |
| 144 | + expect(logger.error).not.toHaveBeenCalled() |
| 145 | + }) |
| 146 | +}) |
0 commit comments