Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 52 additions & 0 deletions src/services/api/openaiShim.architecture.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import { existsSync, readFileSync, readdirSync } from 'node:fs'
import { join } from 'node:path'
import { fileURLToPath } from 'node:url'
import { describe, expect, test } from 'bun:test'

const facadePath = fileURLToPath(new URL('./openaiShim.ts', import.meta.url))
const moduleDirectory = fileURLToPath(new URL('./openaiShim/', import.meta.url))
Comment thread
coderabbitai[bot] marked this conversation as resolved.

const extractionDeltas = [
['streamControl.ts', 169],
['providerCompatibility.ts', 115],
['ollamaAdapter.ts', 387],
['messageConversion.ts', 474],
['rawToolCallParsing.ts', 291],
['xmlToolCallParsing.ts', 356],
['streamConversion.ts', 1_072],
['clientDispatch.ts', 182],
['requestPlanner.ts', 304],
['requestExecutor.ts', 704],
['transport.ts', 361],
Comment thread
coderabbitai[bot] marked this conversation as resolved.
['responseAdapters.ts', 189],
['requestPreparation.ts', 247],
['codexDispatch.ts', 109],
] as const

const upstreamExtractionCount = 11

describe('openaiShim facade architecture', () => {
test('does not regain logic removed by the independent extractions', () => {
for (const [moduleName] of extractionDeltas.slice(0, upstreamExtractionCount)) {
expect(existsSync(join(moduleDirectory, moduleName))).toBe(true)
}
const activeReduction = extractionDeltas
.filter(([moduleName]) => existsSync(join(moduleDirectory, moduleName)))
.reduce(
(total, [, reduction]) => total + reduction,
0,
)
const facadeLines = readFileSync(facadePath, 'utf8').trimEnd().split('\n').length
expect(facadeLines).toBeLessThanOrEqual(5_636 - activeReduction)
})

test('keeps every extracted production module paired with its own test', () => {
const files = readdirSync(moduleDirectory)
const productionModules = files.filter(file =>
file.endsWith('.ts') && !file.endsWith('.test.ts'),
)
for (const moduleName of productionModules) {
expect(files).toContain(moduleName.replace(/\.ts$/, '.test.ts'))
}
})
})
25 changes: 2 additions & 23 deletions src/services/api/openaiShim.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -376,7 +376,6 @@ function importFreshOpenAIShim(

type StreamIdleTestApi = {
StreamIdleTimeoutError: new (timeoutMs: number) => Error
getApiTimeoutMs: () => number
getStreamIdleTimeoutMs: () => number
readWithIdleTimeout: (
reader: ReadableStreamDefaultReader<Uint8Array>,
Expand All @@ -389,7 +388,6 @@ async function getStreamIdleTestApi(cacheKey: string): Promise<StreamIdleTestApi
const mod = await importFreshOpenAIShim(cacheKey)
const testApi = mod.__test as unknown as Partial<StreamIdleTestApi>
expect(typeof testApi.StreamIdleTimeoutError).toBe('function')
expect(typeof testApi.getApiTimeoutMs).toBe('function')
expect(typeof testApi.getStreamIdleTimeoutMs).toBe('function')
expect(typeof testApi.readWithIdleTimeout).toBe('function')
return testApi as StreamIdleTestApi
Expand Down Expand Up @@ -1374,27 +1372,6 @@ test('stream idle timeout env parser parses and bounds overrides', async () => {
})
// openaiShim test extraction seam 024 end

test('API timeout env parser accepts safe positive integers and falls back otherwise', async () => {
const testApi = await getStreamIdleTestApi('api-timeout-env-parser')

delete process.env.API_TIMEOUT_MS
expect(testApi.getApiTimeoutMs()).toBe(600_000)

process.env.API_TIMEOUT_MS = '50'
expect(testApi.getApiTimeoutMs()).toBe(50)

process.env.API_TIMEOUT_MS = ' 50 '
expect(testApi.getApiTimeoutMs()).toBe(50)

process.env.API_TIMEOUT_MS = '3000000000'
expect(testApi.getApiTimeoutMs()).toBe(2_147_483_647)

for (const invalid of ['abc', '-5', '', '0', '1.5', '9007199254740993']) {
process.env.API_TIMEOUT_MS = invalid
expect(testApi.getApiTimeoutMs()).toBe(600_000)
}
})

// openaiShim test extraction seam 025 start: Anthropic-compatible passthrough stream rejects with idle timeout when it stalls
test('Anthropic-compatible passthrough stream rejects with idle timeout when it stalls', async () => {
process.env.CLAUDE_STREAM_IDLE_TIMEOUT_MS = '25'
Expand Down Expand Up @@ -4855,6 +4832,8 @@ test('manual signal fallback removes caller forwarding after the body settles',
} finally {
if (originalAbortSignalAny) {
Object.defineProperty(AbortSignal, 'any', originalAbortSignalAny)
} else {
delete (AbortSignal as { any?: unknown }).any
}
}
})
Expand Down
Loading
Loading