verification.test.ts (4327B)
1 import { mkdtemp, rm, writeFile } from 'node:fs/promises'; 2 import { spawnSync } from 'node:child_process'; 3 import path from 'node:path'; 4 import { fileURLToPath } from 'node:url'; 5 import { ESLint } from 'eslint'; 6 import { describe, expect, it } from 'vitest'; 7 8 const root = fileURLToPath(new URL('../../', import.meta.url)); 9 const execute = (args: string[]) => 10 spawnSync(process.execPath, args, { 11 cwd: root, 12 encoding: 'utf8', 13 timeout: 86_400_000 14 }); 15 16 describe('verification rejects invalid inputs', () => { 17 it('rejects a deliberate TypeScript error through the configured Svelte checker', async () => { 18 const directory = await mkdtemp(path.join(root, 'tests', '.type-fixture-')); 19 try { 20 await writeFile( 21 path.join(directory, 'invalid.ts'), 22 'export const value: number = "invalid";\n' 23 ); 24 await writeFile( 25 path.join(directory, 'invalid.svelte'), 26 '<script lang="ts">let value: number = "invalid";</script><p>{value}</p>' 27 ); 28 await writeFile( 29 path.join(directory, 'tsconfig.json'), 30 JSON.stringify({ 31 extends: '../../tsconfig.json', 32 include: ['invalid.ts', 'invalid.svelte'], 33 exclude: [] 34 }) 35 ); 36 const result = execute([ 37 'node_modules/svelte-check/bin/svelte-check', 38 '--tsconfig', 39 path.join(directory, 'tsconfig.json'), 40 '--fail-on-warnings' 41 ]); 42 expect(result.error).toBeUndefined(); 43 expect(result.status).toBe(1); 44 expect(result.stdout).toContain('invalid.svelte'); 45 expect(result.stdout).toContain( 46 "Type 'string' is not assignable to type 'number'" 47 ); 48 } finally { 49 await rm(directory, { recursive: true }); 50 } 51 }, 86_400_000); 52 53 it('rejects deliberate JavaScript and Svelte lint violations', async () => { 54 const eslint = new ESLint({ cwd: root }); 55 const js = await eslint.lintText('const unused = 1;\n', { 56 filePath: 'eslint.config.js' 57 }); 58 expect( 59 js[0].messages.some( 60 (message) => message.ruleId === '@typescript-eslint/no-unused-vars' 61 ) 62 ).toBe(true); 63 const svelte = await eslint.lintText( 64 '<main style="color: red">HarvestCircle</main>\n', 65 { filePath: 'src/routes/+page.svelte' } 66 ); 67 expect( 68 svelte[0].messages.some( 69 (message) => message.ruleId === 'svelte/no-inline-styles' 70 ) 71 ).toBe(true); 72 const typed = await eslint.lintText( 73 '<script lang="ts">Promise.resolve(1);</script><main>HarvestCircle</main>', 74 { filePath: 'src/routes/+layout.svelte' } 75 ); 76 expect( 77 typed[0].messages.some( 78 (message) => 79 message.ruleId === '@typescript-eslint/no-floating-promises' 80 ) 81 ).toBe(true); 82 }); 83 84 it.each([ 85 [ 86 'inline.svelte', 87 '<main style="color: red">unsafe style</main>', 88 'inline styles' 89 ], 90 [ 91 'component.svelte', 92 '<main>unsafe style</main><style>main { color: red; }</style>', 93 'component CSS' 94 ], 95 ['outside.css', 'main { color: red; }', 'handwritten CSS'], 96 [ 97 'native.ts', 98 "import '../../../core/Cargo.toml';", 99 'forbidden production import' 100 ], 101 [ 102 'provider.ts', 103 "import '../../tests/unit/ssr-effects';", 104 'forbidden production import' 105 ], 106 ['nostr.ts', "import 'applesauce-core';", 'Applesauce imports'] 107 ])( 108 'rejects production boundary violation %s', 109 async (name, content, finding) => { 110 const directory = await mkdtemp( 111 path.join(root, 'src', '.boundary-fixture-') 112 ); 113 try { 114 await writeFile(path.join(directory, name), content); 115 const result = execute(['tools/check-source.mjs']); 116 expect(result.error).toBeUndefined(); 117 expect(result.status).toBe(1); 118 expect(result.stderr).toContain(finding); 119 } finally { 120 await rm(directory, { recursive: true }); 121 } 122 } 123 ); 124 125 it('fails a runner selection with zero matching tests', () => { 126 const result = execute([ 127 'node_modules/vitest/vitest.mjs', 128 'run', 129 '--config', 130 'vitest.config.ts', 131 'no-matching-hcp007-test' 132 ]); 133 expect(result.error).toBeUndefined(); 134 expect(result.status).toBe(1); 135 expect(result.stdout + result.stderr).toContain('No test files found'); 136 }); 137 });