|
| 1 | +/** |
| 2 | + * test/setup.js — Vitest global setup |
| 3 | + * |
| 4 | + * Auto-activates mock-github and mock-gsd-agent before each test, and |
| 5 | + * deactivates them after. Both mocks are conditionally required — the |
| 6 | + * setup works correctly even when lib/mock-github.cjs or |
| 7 | + * lib/mock-gsd-agent.cjs are not yet present (e.g., when PRs #258 and |
| 8 | + * #259 are not yet merged to main). |
| 9 | + * |
| 10 | + * To use mocks in a vitest test file: |
| 11 | + * |
| 12 | + * import { mockGitHub, mockGsdAgent } from './setup.js'; |
| 13 | + * |
| 14 | + * test('my test', () => { |
| 15 | + * // mocks are already active (activated in beforeEach) |
| 16 | + * mockGitHub.setResponse('gh issue view', '{"number":999}'); |
| 17 | + * // ... |
| 18 | + * }); |
| 19 | + * |
| 20 | + * To use a scenario: |
| 21 | + * |
| 22 | + * import { mockGitHub } from './setup.js'; |
| 23 | + * |
| 24 | + * beforeEach(() => { |
| 25 | + * // Override the global beforeEach activation with a scenario |
| 26 | + * mockGitHub.deactivate(); |
| 27 | + * mockGitHub.activate('pr-error'); |
| 28 | + * }); |
| 29 | + */ |
| 30 | + |
| 31 | +import { beforeEach, afterEach } from 'vitest'; |
| 32 | +import { createRequire } from 'module'; |
| 33 | +import { fileURLToPath } from 'url'; |
| 34 | +import path from 'path'; |
| 35 | + |
| 36 | +const require = createRequire(import.meta.url); |
| 37 | +const __dirname = path.dirname(fileURLToPath(import.meta.url)); |
| 38 | +const repoRoot = path.resolve(__dirname, '..'); |
| 39 | + |
| 40 | +// --------------------------------------------------------------------------- |
| 41 | +// Conditional mock loading |
| 42 | +// --------------------------------------------------------------------------- |
| 43 | + |
| 44 | +// Conditionally load mock-github — gracefully skip if not present |
| 45 | +// (lib/mock-github.cjs lands via PR #258) |
| 46 | +let mockGitHub = null; |
| 47 | +try { |
| 48 | + mockGitHub = require(path.join(repoRoot, 'lib', 'mock-github.cjs')); |
| 49 | +} catch (_e) { |
| 50 | + // mock-github.cjs not available — tests run without GitHub API interception |
| 51 | +} |
| 52 | + |
| 53 | +// Conditionally load mock-gsd-agent — gracefully skip if not present |
| 54 | +// (lib/mock-gsd-agent.cjs lands via PR #259) |
| 55 | +let mockGsdAgent = null; |
| 56 | +try { |
| 57 | + mockGsdAgent = require(path.join(repoRoot, 'lib', 'mock-gsd-agent.cjs')); |
| 58 | +} catch (_e) { |
| 59 | + // mock-gsd-agent.cjs not available — tests run without agent spawn interception |
| 60 | +} |
| 61 | + |
| 62 | +// --------------------------------------------------------------------------- |
| 63 | +// Auto-activate hooks |
| 64 | +// --------------------------------------------------------------------------- |
| 65 | + |
| 66 | +beforeEach(() => { |
| 67 | + if (mockGitHub && typeof mockGitHub.activate === 'function') { |
| 68 | + mockGitHub.activate(); |
| 69 | + } |
| 70 | + if (mockGsdAgent && typeof mockGsdAgent.activate === 'function') { |
| 71 | + mockGsdAgent.activate(); |
| 72 | + } |
| 73 | +}); |
| 74 | + |
| 75 | +afterEach(() => { |
| 76 | + if (mockGitHub && typeof mockGitHub.deactivate === 'function') { |
| 77 | + mockGitHub.deactivate(); |
| 78 | + } |
| 79 | + if (mockGsdAgent && typeof mockGsdAgent.deactivate === 'function') { |
| 80 | + mockGsdAgent.deactivate(); |
| 81 | + } |
| 82 | +}); |
| 83 | + |
| 84 | +// --------------------------------------------------------------------------- |
| 85 | +// Exports — available for test files that need direct mock access |
| 86 | +// --------------------------------------------------------------------------- |
| 87 | + |
| 88 | +export { mockGitHub, mockGsdAgent }; |
0 commit comments