|
| 1 | +import assert from 'assert'; |
| 2 | +import path from 'path'; |
| 3 | +import { pathToFileURL } from 'url'; |
| 4 | +import { withGlobalOverrides } from './helpers/web-ui-app-options.mjs'; |
| 5 | + |
| 6 | +const startupModule = await import(pathToFileURL(path.join(process.cwd(), 'web-ui', 'modules', 'app.methods.startup-claude.mjs'))); |
| 7 | + |
| 8 | +function createDeferred() { |
| 9 | + let resolve = null; |
| 10 | + let reject = null; |
| 11 | + const promise = new Promise((res, rej) => { |
| 12 | + resolve = res; |
| 13 | + reject = rej; |
| 14 | + }); |
| 15 | + return { promise, resolve, reject }; |
| 16 | +} |
| 17 | + |
| 18 | +test('loadAll enforces config load timeout deadline', async () => { |
| 19 | + let apiCalls = 0; |
| 20 | + const api = async (action) => { |
| 21 | + apiCalls += 1; |
| 22 | + if (action === 'status') { |
| 23 | + return new Promise(() => {}); |
| 24 | + } |
| 25 | + return { error: 'unexpected' }; |
| 26 | + }; |
| 27 | + const methods = startupModule.createStartupClaudeMethods({ api, configLoadTimeoutMs: 6000 }); |
| 28 | + const context = { |
| 29 | + loading: false, |
| 30 | + initError: '', |
| 31 | + editingCodexBudgetField: '', |
| 32 | + providersList: [], |
| 33 | + currentProvider: '', |
| 34 | + currentModel: '', |
| 35 | + normalizePositiveIntegerInput() { |
| 36 | + return { ok: true, text: '1', value: 1 }; |
| 37 | + }, |
| 38 | + maybeShowStarPrompt() {}, |
| 39 | + showMessage() {}, |
| 40 | + loadModelsForProvider() { |
| 41 | + throw new Error('should not run'); |
| 42 | + }, |
| 43 | + loadCodexAuthProfiles() { |
| 44 | + throw new Error('should not run'); |
| 45 | + } |
| 46 | + }; |
| 47 | + |
| 48 | + await withGlobalOverrides({ |
| 49 | + setTimeout(fn) { |
| 50 | + fn(); |
| 51 | + return 1; |
| 52 | + }, |
| 53 | + clearTimeout() {} |
| 54 | + }, async () => { |
| 55 | + const ok = await methods.loadAll.call(context); |
| 56 | + assert.strictEqual(ok, false); |
| 57 | + assert.strictEqual(context.loading, false); |
| 58 | + assert.strictEqual(context.initError, '读取配置超时'); |
| 59 | + assert.strictEqual(apiCalls, 1); |
| 60 | + }); |
| 61 | +}); |
| 62 | + |
| 63 | +test('refreshClaudeSelectionFromSettings queues the latest call during an inflight request', async () => { |
| 64 | + const first = createDeferred(); |
| 65 | + const second = createDeferred(); |
| 66 | + const calls = []; |
| 67 | + const api = async (action) => { |
| 68 | + calls.push(action); |
| 69 | + if (calls.length === 1) return first.promise; |
| 70 | + return second.promise; |
| 71 | + }; |
| 72 | + const methods = startupModule.createStartupClaudeMethods({ api, configLoadTimeoutMs: 6000 }); |
| 73 | + const context = { |
| 74 | + claudeConfigs: {}, |
| 75 | + currentClaudeConfig: '', |
| 76 | + currentClaudeModel: '', |
| 77 | + messages: [], |
| 78 | + showMessage(text, type) { |
| 79 | + this.messages.push({ text: String(text), type: type || '' }); |
| 80 | + }, |
| 81 | + matchClaudeConfigFromSettings() { |
| 82 | + return ''; |
| 83 | + }, |
| 84 | + ensureClaudeConfigFromSettings() { |
| 85 | + return ''; |
| 86 | + }, |
| 87 | + resetClaudeModelsState() {}, |
| 88 | + refreshClaudeModelContext() {} |
| 89 | + }; |
| 90 | + |
| 91 | + const run1 = methods.refreshClaudeSelectionFromSettings.call(context, { silent: true }); |
| 92 | + const run2 = methods.refreshClaudeSelectionFromSettings.call(context, { silent: true }); |
| 93 | + await Promise.resolve(); |
| 94 | + assert.deepStrictEqual(calls, ['get-claude-settings']); |
| 95 | + |
| 96 | + first.resolve({ env: {}, exists: false }); |
| 97 | + await run1; |
| 98 | + await Promise.resolve(); |
| 99 | + assert.deepStrictEqual(calls, ['get-claude-settings', 'get-claude-settings']); |
| 100 | + |
| 101 | + second.resolve({ env: {}, exists: false }); |
| 102 | + await run2; |
| 103 | +}); |
| 104 | + |
0 commit comments