import fs from 'node:fs'; import path from 'node:path'; import { describe, expect, it } from 'vitest'; import { ALLOWED_LICENSES, evaluateLicense, findException, readDenyAllowList, } from './check-licenses.mjs'; describe('evaluateLicense', () => { it('allows listed licenses and expressions the allowlist satisfies', () => { expect(evaluateLicense('MIT').status).toBe('allowed'); expect(evaluateLicense('(MIT AND Zlib)').status).toBe('allowed'); expect(evaluateLicense('(MIT OR Apache-2.0) AND Unicode-3.0').status).toBe('allowed'); expect(evaluateLicense('Apache-2.0 WITH LLVM-exception').status).toBe('allowed'); }); it('passes dual-licensed packages on their permissive option and reports the other one', () => { const result = evaluateLicense('(MIT OR GPL-3.0-or-later)'); expect(result.status).toBe('allowed'); expect(result.alternatives).toEqual(['GPL-3.0-or-later']); }); it('denies copyleft licenses, alone or in a conjunction', () => { for (const expression of ['GPL-3.0-only', 'AGPL-3.0-or-later', 'SSPL-1.0', 'MIT AND LGPL-3.0-or-later']) { const result = evaluateLicense(expression); expect(result.status, expression).toBe('denied'); expect(result.copyleft, expression).toBe(true); } expect(evaluateLicense('GPL-2.0-only WITH Classpath-exception-2.0').status).toBe('denied'); }); it('flags missing and non-SPDX licenses', () => { expect(evaluateLicense(null).status).toBe('missing'); expect(evaluateLicense(' ').status).toBe('missing'); for (const expression of ['SEE LICENSE IN LICENSE', 'MIT/X11', 'Apache 2.0', '(MIT', 'MIT OR']) { expect(evaluateLicense(expression).status, expression).toBe('invalid'); } expect(evaluateLicense('BSD').status).toBe('denied'); }); }); describe('findException', () => { it('matches by name pattern and the exact declared license', () => { expect(findException({ name: '@img/sharp-libvips-linux-x64', license: 'LGPL-3.0-or-later' })).toBeDefined(); expect(findException({ name: '@img/sharp-libvips-linux-x64', license: 'GPL-3.0-only' })).toBeUndefined(); expect(findException({ name: 'async', license: null })).toBeDefined(); expect(findException({ name: 'async', license: 'GPL-3.0-only' })).toBeUndefined(); }); }); describe('deny.toml', () => { it('uses the same allowlist as the npm check', () => { const denyToml = fs.readFileSync(path.join(__dirname, '..', 'app', 'src-tauri', 'deny.toml'), 'utf8'); expect([...readDenyAllowList(denyToml)].sort()).toEqual([...ALLOWED_LICENSES].sort()); }); });