|
| 1 | +import assert from 'assert'; |
| 2 | + |
| 3 | +import { createProvidersMethods } from '../../web-ui/modules/app.methods.providers.mjs'; |
| 4 | +import { createClaudeConfigMethods } from '../../web-ui/modules/app.methods.claude-config.mjs'; |
| 5 | +import { createStartupClaudeMethods } from '../../web-ui/modules/app.methods.startup-claude.mjs'; |
| 6 | +import { |
| 7 | + nextClaudeConfigName, |
| 8 | + nextCodexProviderName, |
| 9 | + nextNumericProviderName |
| 10 | +} from '../../web-ui/modules/provider-default-names.mjs'; |
| 11 | + |
| 12 | +test('nextNumericProviderName returns the first free positive numeric name', () => { |
| 13 | + assert.strictEqual(nextNumericProviderName(['1', '3', 'foo', '02', 'local']), '4'); |
| 14 | + assert.strictEqual(nextNumericProviderName([{ name: '1' }, { name: '2' }, { name: 'custom' }]), '3'); |
| 15 | + assert.strictEqual(nextNumericProviderName(['foo', 'local']), '1'); |
| 16 | +}); |
| 17 | + |
| 18 | +test('Codex add-provider modal defaults to an auto-increment numeric provider name', () => { |
| 19 | + const methods = createProvidersMethods({ api: async () => ({ success: true }) }); |
| 20 | + const context = { |
| 21 | + providersList: [ |
| 22 | + { name: 'local' }, |
| 23 | + { name: '1' }, |
| 24 | + { name: '2' }, |
| 25 | + { name: 'custom' } |
| 26 | + ], |
| 27 | + newProvider: { name: 'stale', url: 'https://old.example.test', key: 'sk-old', model: 'old' }, |
| 28 | + showAddProviderKey: true, |
| 29 | + showAddModal: false |
| 30 | + }; |
| 31 | + |
| 32 | + methods.openAddProviderModal.call(context); |
| 33 | + |
| 34 | + assert.strictEqual(context.showAddModal, true); |
| 35 | + assert.strictEqual(context.showAddProviderKey, false); |
| 36 | + assert.deepStrictEqual(context.newProvider, { |
| 37 | + name: '3', |
| 38 | + url: '', |
| 39 | + key: '', |
| 40 | + model: '', |
| 41 | + useTransform: false |
| 42 | + }); |
| 43 | + |
| 44 | + context.providersList.push({ name: '3' }); |
| 45 | + methods.closeAddModal.call(context); |
| 46 | + assert.strictEqual(context.showAddModal, false); |
| 47 | + assert.strictEqual(context.newProvider.name, '4'); |
| 48 | +}); |
| 49 | + |
| 50 | +test('Claude add-config modal defaults to an auto-increment numeric config name', () => { |
| 51 | + const startupMethods = createStartupClaudeMethods({ api: async () => ({}) }); |
| 52 | + const claudeMethods = createClaudeConfigMethods({ api: async () => ({}) }); |
| 53 | + const context = { |
| 54 | + claudeConfigs: { |
| 55 | + '智谱GLM': {}, |
| 56 | + '1': {}, |
| 57 | + '3': {}, |
| 58 | + 'custom': {} |
| 59 | + }, |
| 60 | + newClaudeConfig: { name: 'stale', apiKey: 'sk-old', baseUrl: 'https://old.example.test', model: 'old' }, |
| 61 | + showAddClaudeConfigKey: true, |
| 62 | + showClaudeConfigModal: false |
| 63 | + }; |
| 64 | + |
| 65 | + startupMethods.openClaudeConfigModal.call(context); |
| 66 | + |
| 67 | + assert.strictEqual(context.showClaudeConfigModal, true); |
| 68 | + assert.strictEqual(context.showAddClaudeConfigKey, false); |
| 69 | + assert.deepStrictEqual(context.newClaudeConfig, { |
| 70 | + name: '2', |
| 71 | + apiKey: '', |
| 72 | + externalCredentialType: '', |
| 73 | + baseUrl: '', |
| 74 | + model: '', |
| 75 | + targetApi: 'responses' |
| 76 | + }); |
| 77 | + |
| 78 | + context.claudeConfigs['2'] = {}; |
| 79 | + claudeMethods.closeClaudeConfigModal.call(context); |
| 80 | + assert.strictEqual(context.showClaudeConfigModal, false); |
| 81 | + assert.strictEqual(context.newClaudeConfig.name, '4'); |
| 82 | +}); |
| 83 | + |
| 84 | +test('clone provider/config modals keep manual naming instead of auto-numbering', () => { |
| 85 | + const codexMethods = createProvidersMethods({ api: async () => ({ success: true }) }); |
| 86 | + const claudeMethods = createClaudeConfigMethods({ api: async () => ({}) }); |
| 87 | + |
| 88 | + const codexContext = { |
| 89 | + newProvider: {}, |
| 90 | + showAddProviderKey: true, |
| 91 | + showAddModal: false |
| 92 | + }; |
| 93 | + codexMethods.openCloneProviderModal.call(codexContext, { |
| 94 | + name: '1', |
| 95 | + url: 'https://codex.example.test/v1', |
| 96 | + upstreamUrl: '', |
| 97 | + codexmate_bridge: '' |
| 98 | + }); |
| 99 | + assert.strictEqual(codexContext.showAddModal, true); |
| 100 | + assert.strictEqual(codexContext.newProvider.name, ''); |
| 101 | + assert.strictEqual(codexContext.newProvider.url, 'https://codex.example.test/v1'); |
| 102 | + |
| 103 | + const claudeContext = { |
| 104 | + claudeConfigs: { '1': {}, '2': {} }, |
| 105 | + newClaudeConfig: {}, |
| 106 | + showAddClaudeConfigKey: true, |
| 107 | + showClaudeConfigModal: false |
| 108 | + }; |
| 109 | + claudeMethods.openCloneClaudeConfigModal.call(claudeContext, '1', { |
| 110 | + apiKey: 'sk-source', |
| 111 | + baseUrl: 'https://claude.example.test', |
| 112 | + model: 'claude-source', |
| 113 | + targetApi: 'responses' |
| 114 | + }); |
| 115 | + assert.strictEqual(claudeContext.showClaudeConfigModal, true); |
| 116 | + assert.strictEqual(claudeContext.newClaudeConfig.name, ''); |
| 117 | + assert.strictEqual(claudeContext.newClaudeConfig.baseUrl, 'https://claude.example.test'); |
| 118 | +}); |
| 119 | + |
| 120 | +test('provider default-name helpers read Codex provider lists and Claude config maps', () => { |
| 121 | + assert.strictEqual(nextCodexProviderName([{ name: '1' }, { name: '2' }]), '3'); |
| 122 | + assert.strictEqual(nextClaudeConfigName({ '1': {}, '2': {}, named: {} }), '3'); |
| 123 | +}); |
0 commit comments