|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +let makeModelBodyTransform; |
| 4 | + |
| 5 | +describe('Copilot Responses auto routing', () => { |
| 6 | + const originalEnv = { ...process.env }; |
| 7 | + |
| 8 | + beforeEach(() => { |
| 9 | + delete process.env.AWF_ALLOWED_MODELS; |
| 10 | + delete process.env.AWF_DISALLOWED_MODELS; |
| 11 | + jest.isolateModules(() => { |
| 12 | + ({ makeModelBodyTransform } = require('./model-config')); |
| 13 | + }); |
| 14 | + }); |
| 15 | + |
| 16 | + afterEach(() => { |
| 17 | + process.env = { ...originalEnv }; |
| 18 | + }); |
| 19 | + |
| 20 | + const models = { |
| 21 | + copilot: ['gpt-5.3-codex', 'gpt-5.4-codex', 'gpt-5.5-chat'], |
| 22 | + }; |
| 23 | + const metadata = [ |
| 24 | + { id: 'gpt-5.3-codex', supportedEndpoints: ['/responses'] }, |
| 25 | + { id: 'gpt-5.4-codex', supportedEndpoints: ['/responses', '/chat/completions'] }, |
| 26 | + { id: 'gpt-5.5-chat', supportedEndpoints: ['/chat/completions'] }, |
| 27 | + ]; |
| 28 | + |
| 29 | + function createTransform(overrides = {}) { |
| 30 | + return makeModelBodyTransform( |
| 31 | + 'copilot', |
| 32 | + overrides.models || models, |
| 33 | + overrides.refresh || jest.fn(), |
| 34 | + () => new Set(['copilot']), |
| 35 | + overrides.getRuntimeModels || (() => metadata), |
| 36 | + overrides.isNativeCopilot ?? true, |
| 37 | + ); |
| 38 | + } |
| 39 | + |
| 40 | + it('routes Responses auto to the highest-version inventory model compatible with Codex', async () => { |
| 41 | + const transform = createTransform(); |
| 42 | + const req = { method: 'POST', url: '/v1/responses?stream=true' }; |
| 43 | + const body = Buffer.from(JSON.stringify({ |
| 44 | + model: 'copilot/auto', |
| 45 | + input: 'Hello', |
| 46 | + tools: [{ type: 'custom' }], |
| 47 | + })); |
| 48 | + |
| 49 | + const result = await transform(body, req); |
| 50 | + |
| 51 | + expect(JSON.parse(result.toString('utf8'))).toEqual({ |
| 52 | + model: 'gpt-5.4-codex', |
| 53 | + input: 'Hello', |
| 54 | + tools: [{ type: 'custom' }], |
| 55 | + }); |
| 56 | + expect(req.awfModelCandidates).toEqual(['gpt-5.4-codex', 'gpt-5.3-codex']); |
| 57 | + }); |
| 58 | + |
| 59 | + it('skips a disabled higher-version Codex model', async () => { |
| 60 | + const transform = createTransform({ |
| 61 | + getRuntimeModels: () => metadata.map(record => ({ |
| 62 | + ...record, |
| 63 | + modelPickerEnabled: record.id !== 'gpt-5.4-codex', |
| 64 | + })), |
| 65 | + }); |
| 66 | + const req = { method: 'POST', url: '/responses' }; |
| 67 | + |
| 68 | + const result = await transform(Buffer.from('{"model":"auto"}'), req); |
| 69 | + |
| 70 | + expect(JSON.parse(result.toString('utf8')).model).toBe('gpt-5.3-codex'); |
| 71 | + expect(req.awfModelCandidates).toEqual(['gpt-5.3-codex']); |
| 72 | + }); |
| 73 | + |
| 74 | + it.each([ |
| 75 | + ['AWF_ALLOWED_MODELS', ['copilot/gpt-5.3-codex']], |
| 76 | + ['AWF_DISALLOWED_MODELS', ['copilot/gpt-5.4-codex']], |
| 77 | + ])('routes auto using concrete-model policy from %s', async (key, patterns) => { |
| 78 | + process.env[key] = JSON.stringify(patterns); |
| 79 | + jest.isolateModules(() => { |
| 80 | + ({ makeModelBodyTransform } = require('./model-config')); |
| 81 | + }); |
| 82 | + const refresh = jest.fn(); |
| 83 | + const transform = createTransform({ refresh }); |
| 84 | + const req = { method: 'POST', url: '/responses' }; |
| 85 | + |
| 86 | + const result = await transform(Buffer.from('{"model":"copilot/auto"}'), req); |
| 87 | + |
| 88 | + expect(JSON.parse(result.toString('utf8')).model).toBe('gpt-5.3-codex'); |
| 89 | + expect(req.awfModelCandidates).toEqual(['gpt-5.3-codex']); |
| 90 | + expect(refresh).not.toHaveBeenCalled(); |
| 91 | + }); |
| 92 | + |
| 93 | + it.each([ |
| 94 | + ['AWF_ALLOWED_MODELS', ['gpt-5.5-chat']], |
| 95 | + ['AWF_DISALLOWED_MODELS', ['*codex*']], |
| 96 | + ])('fails closed after one refresh when %s excludes all compatible models', async (key, patterns) => { |
| 97 | + process.env[key] = JSON.stringify(patterns); |
| 98 | + jest.isolateModules(() => { |
| 99 | + ({ makeModelBodyTransform } = require('./model-config')); |
| 100 | + }); |
| 101 | + const refresh = jest.fn(); |
| 102 | + const transform = createTransform({ refresh }); |
| 103 | + |
| 104 | + await expect(transform( |
| 105 | + Buffer.from('{"model":"auto"}'), |
| 106 | + { method: 'POST', url: '/responses' }, |
| 107 | + )).rejects.toMatchObject({ |
| 108 | + statusCode: 503, |
| 109 | + code: 'copilot_auto_responses_model_unavailable', |
| 110 | + }); |
| 111 | + expect(refresh).toHaveBeenCalledTimes(1); |
| 112 | + expect(refresh).toHaveBeenCalledWith('copilot'); |
| 113 | + }); |
| 114 | + |
| 115 | + it('fails closed when all compatible models are disabled', async () => { |
| 116 | + const refresh = jest.fn(); |
| 117 | + const transform = createTransform({ |
| 118 | + getRuntimeModels: () => metadata.map(record => ({ ...record, modelPickerEnabled: false })), |
| 119 | + refresh, |
| 120 | + }); |
| 121 | + |
| 122 | + await expect(transform( |
| 123 | + Buffer.from('{"model":"auto"}'), |
| 124 | + { method: 'POST', url: '/responses' }, |
| 125 | + )).rejects.toMatchObject({ |
| 126 | + statusCode: 503, |
| 127 | + code: 'copilot_auto_responses_model_unavailable', |
| 128 | + }); |
| 129 | + expect(refresh).toHaveBeenCalledTimes(1); |
| 130 | + }); |
| 131 | + |
| 132 | + it('leaves Chat Completions auto unchanged', async () => { |
| 133 | + const transform = createTransform(); |
| 134 | + const body = Buffer.from(JSON.stringify({ model: 'auto', messages: [] })); |
| 135 | + |
| 136 | + await expect(transform(body, { method: 'POST', url: '/chat/completions' })).resolves.toBeNull(); |
| 137 | + }); |
| 138 | + |
| 139 | + it('refreshes the Copilot inventory before reporting no compatible model', async () => { |
| 140 | + const refreshedModels = { copilot: ['gpt-4o'] }; |
| 141 | + const refresh = jest.fn(async () => { |
| 142 | + refreshedModels.copilot = ['gpt-5.3-codex']; |
| 143 | + }); |
| 144 | + const transform = createTransform({ |
| 145 | + models: refreshedModels, |
| 146 | + getRuntimeModels: () => metadata, |
| 147 | + refresh, |
| 148 | + }); |
| 149 | + const req = { method: 'POST', url: '/responses' }; |
| 150 | + const result = await transform(Buffer.from('{"model":"auto"}'), req); |
| 151 | + |
| 152 | + expect(refresh).toHaveBeenCalledWith('copilot'); |
| 153 | + expect(JSON.parse(result.toString('utf8')).model).toBe('gpt-5.3-codex'); |
| 154 | + }); |
| 155 | + |
| 156 | + it('fails explicitly when the inventory has no Codex-compatible Responses model', async () => { |
| 157 | + const transform = createTransform({ |
| 158 | + models: { copilot: ['gpt-5.5-chat'] }, |
| 159 | + refresh: jest.fn(), |
| 160 | + }); |
| 161 | + |
| 162 | + await expect(transform( |
| 163 | + Buffer.from('{"model":"auto"}'), |
| 164 | + { method: 'POST', url: '/responses' }, |
| 165 | + )).rejects.toMatchObject({ |
| 166 | + statusCode: 503, |
| 167 | + code: 'copilot_auto_responses_model_unavailable', |
| 168 | + }); |
| 169 | + }); |
| 170 | + |
| 171 | + it('does not route auto for custom Copilot targets', async () => { |
| 172 | + const transform = createTransform({ isNativeCopilot: false }); |
| 173 | + |
| 174 | + const result = transform |
| 175 | + ? await transform(Buffer.from('{"model":"auto"}'), { method: 'POST', url: '/responses' }) |
| 176 | + : null; |
| 177 | + expect(result).toBeNull(); |
| 178 | + }); |
| 179 | +}); |
0 commit comments