// @vitest-environment node import { spawnSync } from 'node:child_process'; import { mkdtempSync, rmSync, symlinkSync } from 'node:fs'; import { tmpdir } from 'node:os'; import path from 'node:path'; import { fileURLToPath } from 'node:url'; import { describe, expect, it } from 'vitest'; import { parseBasename } from './server.js'; describe('parseBasename', () => { it.each([ [undefined, ''], ['', ''], ['/', ''], ['/workflows', '/workflows'], ['/workflows/', '/workflows'], ['/ops/workflows', '/ops/workflows'], ])('%s -> %s', (input, expected) => { expect(parseBasename(input)).toBe(expected); }); it.each([ 'workflows', '//evil.example.com', '/a/../b', '/a b', '/a?b', ])('refuses %s', (input) => { expect(() => parseBasename(input)).toThrow(/basename/); }); }); describe('direct invocation through a symlink', () => { it('starts server.js when invoked through a symlinked directory (as under pnpm)', () => { const dir = mkdtempSync(path.join(tmpdir(), 'workflow-web-symlink-')); const pkgDir = path.dirname(fileURLToPath(import.meta.url)); const linkDir = path.join(dir, 'web-link'); symlinkSync(pkgDir, linkDir, 'junction'); try { // Trigger fast fail-closed config validation so we prove startServer() // actually ran without needing to bind a port. const result = spawnSync( process.execPath, [path.join(linkDir, 'server.js')], { encoding: 'utf8', env: { ...process.env, WORKFLOW_WEB_AUTH: '', WORKFLOW_WEB_AUTH_ISSUER: 'https://issuer.example.com', }, } ); expect(result.status).toBe(1); expect(result.stderr).toMatch(/WORKFLOW_WEB_AUTH_ISSUER/); } finally { rmSync(dir, { recursive: true, force: true }); } }); });