|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +const path = require('path'); |
| 4 | +const os = require('os'); |
| 5 | +const assert = require('assert'); |
| 6 | + |
| 7 | +const OUT = path.resolve(__dirname, '../out'); |
| 8 | + |
| 9 | +const { ClaudeLocalReader } = require(path.join(OUT, 'providers/claudeLocal')); |
| 10 | +const { CodexLocalReader } = require(path.join(OUT, 'providers/codexLocal')); |
| 11 | +const { runEnabledReaders } = require(path.join(OUT, 'providers/readProviders')); |
| 12 | +const { formatRefreshSummary } = require(path.join(OUT, 'core/formatQuota')); |
| 13 | + |
| 14 | +let pass = 0; |
| 15 | +let fail = 0; |
| 16 | + |
| 17 | +async function test(name, fn) { |
| 18 | + try { |
| 19 | + await fn(); |
| 20 | + console.log(` PASS ${name}`); |
| 21 | + pass++; |
| 22 | + } catch (e) { |
| 23 | + console.error(` FAIL ${name}: ${e.message}`); |
| 24 | + fail++; |
| 25 | + } |
| 26 | +} |
| 27 | + |
| 28 | +const ABSENT = path.join(os.tmpdir(), `pf-smoke-absent-${Date.now()}`); |
| 29 | + |
| 30 | +async function main() { |
| 31 | + // --- ClaudeLocalReader --- |
| 32 | + |
| 33 | + await test('ClaudeLocalReader: returns not-found for absent path', async () => { |
| 34 | + const reader = new ClaudeLocalReader(ABSENT); |
| 35 | + const result = await reader.read(); |
| 36 | + assert.strictEqual(result.providerId, 'claude'); |
| 37 | + assert.strictEqual(result.status, 'not-found'); |
| 38 | + }); |
| 39 | + |
| 40 | + await test('ClaudeLocalReader: providerId is claude', async () => { |
| 41 | + const reader = new ClaudeLocalReader(ABSENT); |
| 42 | + assert.strictEqual(reader.providerId, 'claude'); |
| 43 | + }); |
| 44 | + |
| 45 | + // --- CodexLocalReader --- |
| 46 | + |
| 47 | + await test('CodexLocalReader: returns not-found for absent path', async () => { |
| 48 | + const reader = new CodexLocalReader(ABSENT); |
| 49 | + const result = await reader.read(); |
| 50 | + assert.strictEqual(result.providerId, 'codex'); |
| 51 | + assert.strictEqual(result.status, 'not-found'); |
| 52 | + }); |
| 53 | + |
| 54 | + await test('CodexLocalReader: providerId is codex', async () => { |
| 55 | + const reader = new CodexLocalReader(ABSENT); |
| 56 | + assert.strictEqual(reader.providerId, 'codex'); |
| 57 | + }); |
| 58 | + |
| 59 | + // --- runEnabledReaders --- |
| 60 | + |
| 61 | + await test('runEnabledReaders: empty enabledProviders returns empty array', async () => { |
| 62 | + const readers = [new ClaudeLocalReader(ABSENT), new CodexLocalReader(ABSENT)]; |
| 63 | + const results = await runEnabledReaders(readers, []); |
| 64 | + assert.strictEqual(results.length, 0); |
| 65 | + }); |
| 66 | + |
| 67 | + await test('runEnabledReaders: only enabled providers run', async () => { |
| 68 | + const readers = [new ClaudeLocalReader(ABSENT), new CodexLocalReader(ABSENT)]; |
| 69 | + const results = await runEnabledReaders(readers, ['claude']); |
| 70 | + assert.strictEqual(results.length, 1); |
| 71 | + assert.strictEqual(results[0].providerId, 'claude'); |
| 72 | + }); |
| 73 | + |
| 74 | + await test('runEnabledReaders: unknown provider id is skipped', async () => { |
| 75 | + const readers = [new ClaudeLocalReader(ABSENT)]; |
| 76 | + const results = await runEnabledReaders(readers, ['openai']); |
| 77 | + assert.strictEqual(results.length, 0); |
| 78 | + }); |
| 79 | + |
| 80 | + await test('runEnabledReaders: both providers run when both enabled', async () => { |
| 81 | + const readers = [new ClaudeLocalReader(ABSENT), new CodexLocalReader(ABSENT)]; |
| 82 | + const results = await runEnabledReaders(readers, ['claude', 'codex']); |
| 83 | + assert.strictEqual(results.length, 2); |
| 84 | + const ids = results.map(r => r.providerId).sort(); |
| 85 | + assert.deepStrictEqual(ids, ['claude', 'codex']); |
| 86 | + }); |
| 87 | + |
| 88 | + // --- formatRefreshSummary --- |
| 89 | + |
| 90 | + await test('formatRefreshSummary: empty results returns no-providers message', async () => { |
| 91 | + const s = formatRefreshSummary([]); |
| 92 | + assert.ok(s.includes('no providers'), `expected "no providers" in "${s}"`); |
| 93 | + }); |
| 94 | + |
| 95 | + await test('formatRefreshSummary: not-found includes label and "not found"', async () => { |
| 96 | + const s = formatRefreshSummary([{ providerId: 'claude', status: 'not-found' }]); |
| 97 | + assert.ok(s.includes('Claude'), `expected "Claude" in "${s}"`); |
| 98 | + assert.ok(s.includes('not found'), `expected "not found" in "${s}"`); |
| 99 | + }); |
| 100 | + |
| 101 | + await test('formatRefreshSummary: ok includes file count', async () => { |
| 102 | + const s = formatRefreshSummary([{ providerId: 'claude', status: 'ok', filesFound: 5 }]); |
| 103 | + assert.ok(s.includes('Claude'), `expected "Claude" in "${s}"`); |
| 104 | + assert.ok(s.includes('5'), `expected "5" in "${s}"`); |
| 105 | + }); |
| 106 | + |
| 107 | + await test('formatRefreshSummary: ok with 1 file uses singular', async () => { |
| 108 | + const s = formatRefreshSummary([{ providerId: 'claude', status: 'ok', filesFound: 1 }]); |
| 109 | + assert.ok(s.includes('1 session file'), `expected singular "1 session file" in "${s}"`); |
| 110 | + assert.ok(!s.includes('1 session files'), `expected no plural "1 session files" in "${s}"`); |
| 111 | + }); |
| 112 | + |
| 113 | + await test('formatRefreshSummary: no-data includes label and "no session files"', async () => { |
| 114 | + const s = formatRefreshSummary([{ providerId: 'codex', status: 'no-data', filesFound: 0 }]); |
| 115 | + assert.ok(s.includes('Codex'), `expected "Codex" in "${s}"`); |
| 116 | + assert.ok(s.includes('no session files'), `expected "no session files" in "${s}"`); |
| 117 | + }); |
| 118 | + |
| 119 | + await test('formatRefreshSummary: error includes label and "read error"', async () => { |
| 120 | + const s = formatRefreshSummary([{ providerId: 'claude', status: 'error' }]); |
| 121 | + assert.ok(s.includes('Claude'), `expected "Claude" in "${s}"`); |
| 122 | + assert.ok(s.includes('read error'), `expected "read error" in "${s}"`); |
| 123 | + }); |
| 124 | + |
| 125 | + await test('formatRefreshSummary: mixed states joined with " | "', async () => { |
| 126 | + const s = formatRefreshSummary([ |
| 127 | + { providerId: 'claude', status: 'ok', filesFound: 3 }, |
| 128 | + { providerId: 'codex', status: 'not-found' }, |
| 129 | + ]); |
| 130 | + assert.ok(s.includes(' | '), `expected " | " separator in "${s}"`); |
| 131 | + }); |
| 132 | + |
| 133 | + console.log(''); |
| 134 | + console.log(`smoke-providers: ${pass} passed, ${fail} failed`); |
| 135 | + if (fail > 0) { |
| 136 | + process.exit(1); |
| 137 | + } |
| 138 | +} |
| 139 | + |
| 140 | +main().catch(e => { |
| 141 | + console.error('smoke-providers: fatal error:', e); |
| 142 | + process.exit(1); |
| 143 | +}); |
0 commit comments