@@ -4,7 +4,7 @@ import { describe, it } from "node:test"
44import { Linter } from "eslint"
55import typescriptEslint from "typescript-eslint"
66
7- import { apiProviderHygieneConfig } from "./api-provider-hygiene.js"
7+ import { apiProviderHygieneConfig , apiProviderTestSelectors } from "./api-provider-hygiene.js"
88
99const linter = new Linter ( { configType : "flat" } )
1010const config = [ { files : [ "**/*.ts" ] , languageOptions : { parser : typescriptEslint . parser } } , apiProviderHygieneConfig ]
@@ -88,3 +88,89 @@ describe("api provider hygiene: sensitive values in logs", () => {
8888 } )
8989 }
9090} )
91+
92+ describe ( "api provider hygiene: request timeout forwarding" , ( ) => {
93+ for ( const code of [
94+ "create(body, { timeout: options.timeoutMs })" ,
95+ "create(body, { timeout: options?.timeoutMs })" ,
96+ "create(body, { signal, timeout: timeoutMs })" ,
97+ "requestOptions.timeout = options.timeoutMs" ,
98+ "requestOptions.timeout = timeoutMs" ,
99+ ] ) {
100+ it ( `flags: ${ code } ` , ( ) => {
101+ assert . equal ( lint ( code ) . length , 1 )
102+ } )
103+ }
104+
105+ for ( const code of [
106+ "new OpenAI({ timeout: this.timeoutMs })" ,
107+ "requestOptions.timeout = toSdkTimeoutMs(timeoutMs)" ,
108+ "create(body, { timeout: toSdkTimeoutMs(options.timeoutMs) })" ,
109+ "if (hasUsableTimeout(timeoutMs)) requestOptions.timeout = toSdkTimeoutMs(timeoutMs)" ,
110+ "const timeoutMs = options?.timeoutMs" ,
111+ "mergeAbortSignalAndTimeout(signal, options?.timeoutMs)" ,
112+ ] ) {
113+ it ( `allows: ${ code } ` , ( ) => {
114+ assert . deepEqual ( lint ( code ) , [ ] )
115+ } )
116+ }
117+ } )
118+
119+ describe ( "api provider hygiene: timeout helpers" , ( ) => {
120+ for ( const code of [
121+ "const MAX = 2147483647" ,
122+ "Math.min(Math.ceil(timeoutMs), 2_147_483_647)" ,
123+ "function hasUsableTimeout(t?: number) { return true }" ,
124+ "const toSdkTimeoutMs = (t: number) => t" ,
125+ "function bridgeExternalAbortSignal() {}" ,
126+ ] ) {
127+ it ( `flags: ${ code } ` , ( ) => {
128+ assert . equal ( lint ( code ) . length , 1 )
129+ } )
130+ }
131+
132+ it ( "allows importing the timeout helpers" , ( ) => {
133+ assert . deepEqual (
134+ lint ( 'import { hasUsableTimeout, toSdkTimeoutMs } from "./utils/abort-signal"\ntoSdkTimeoutMs(1)' ) ,
135+ [ ] ,
136+ )
137+ } )
138+
139+ it ( "allows the literal in the helper module" , ( ) => {
140+ assert . deepEqual ( lint ( "export const MAX_TIMEOUT_MS = 2_147_483_647" , "api/providers/utils/abort-signal.ts" ) , [ ] )
141+ } )
142+ } )
143+
144+ describe ( "api provider hygiene: spec loops" , ( ) => {
145+ const testLinter = new Linter ( { configType : "flat" } )
146+ const testConfig = [
147+ { files : [ "**/*.ts" ] , languageOptions : { parser : typescriptEslint . parser } } ,
148+ {
149+ files : [ "api/**/__tests__/**/*.spec.ts" ] ,
150+ rules : { "no-restricted-syntax" : [ "error" , ...apiProviderTestSelectors ] } ,
151+ } ,
152+ ]
153+ const lintSpec = ( code ) =>
154+ testLinter
155+ . verify ( code , testConfig , { filename : "api/providers/__tests__/example.spec.ts" } )
156+ . map ( ( m ) => m . message )
157+
158+ for ( const code of [
159+ "it('x', () => { for (const v of [0, -5]) { expect(f(v)).toBe(1) } })" ,
160+ "it('x', async () => { for (const v of [0, -5]) { await run(v); expect(f(v)).toBe(1) } })" ,
161+ ] ) {
162+ it ( `flags a loop of expects over a literal array: ${ code } ` , ( ) => {
163+ assert . equal ( lintSpec ( code ) . length , 1 )
164+ } )
165+ }
166+
167+ for ( const code of [
168+ "it.each([0, -5])('x %s', (v) => { expect(f(v)).toBe(1) })" ,
169+ "it('x', () => { for (const v of [0, -5]) { run(v) } })" ,
170+ "it('x', () => { for (const v of cases) { expect(f(v)).toBe(1) } })" ,
171+ ] ) {
172+ it ( `allows: ${ code } ` , ( ) => {
173+ assert . deepEqual ( lintSpec ( code ) , [ ] )
174+ } )
175+ }
176+ } )
0 commit comments