From de56a388eb3c9c17151e206a5243bc5f9a85953c Mon Sep 17 00:00:00 2001 From: Deyaaeldeen Almahallawi Date: Wed, 20 Nov 2024 21:09:00 +0200 Subject: [PATCH] [OpenAI] Lint test (#31854) --- .../openai/test/public/abortsignal.spec.ts | 6 ++-- .../openai/test/public/assistants.spec.ts | 6 ++-- .../openai/test/public/completions.spec.ts | 8 ++--- .../openai/test/public/embeddings.spec.ts | 6 ++-- sdk/openai/openai/test/public/images.spec.ts | 6 ++-- .../test/public/node/assistantsFile.spec.ts | 2 +- .../openai/test/public/node/batches.spec.ts | 16 ++++++---- .../openai/test/public/node/whisper.spec.ts | 8 ++--- sdk/openai/openai/test/public/tts.spec.ts | 6 ++-- .../openai/test/public/utils/asserts.ts | 29 ++++++++++--------- .../openai/test/public/utils/audioTypes.ts | 5 +++- .../openai/test/public/utils/createClient.ts | 8 ++--- .../test/public/utils/images-browser.mts | 2 +- sdk/openai/openai/test/public/utils/images.ts | 2 +- .../openai/test/public/utils/logging.ts | 2 +- sdk/openai/openai/test/public/utils/setup.ts | 6 ++-- sdk/openai/openai/test/public/utils/utils.ts | 8 ++--- sdk/openai/openai/test/public/vision.spec.ts | 6 ++-- 18 files changed, 71 insertions(+), 61 deletions(-) diff --git a/sdk/openai/openai/test/public/abortsignal.spec.ts b/sdk/openai/openai/test/public/abortsignal.spec.ts index 5cfc3e1d6f7..986f5cecb3a 100644 --- a/sdk/openai/openai/test/public/abortsignal.spec.ts +++ b/sdk/openai/openai/test/public/abortsignal.spec.ts @@ -3,9 +3,9 @@ import { assert, describe, beforeEach, it } from "vitest"; import { matrix } from "@azure-tools/test-utils-vitest"; -import OpenAI, { AzureOpenAI } from "openai"; +import type { OpenAI, AzureOpenAI } from "openai"; import { createClient } from "./utils/createClient.js"; -import { APIMatrix, APIVersion } from "./utils/utils.js"; +import { APIMatrix, type APIVersion } from "./utils/utils.js"; describe("AbortSignal", () => { let client: AzureOpenAI | OpenAI; @@ -31,7 +31,6 @@ describe("AbortSignal", () => { ]; const deploymentName = "gpt-35-turbo"; - let currentMessage = ""; try { const events = client.beta.chat.completions.stream({ model: deploymentName, @@ -46,7 +45,6 @@ describe("AbortSignal", () => { assert.isDefined(event); events.abort(); } - assert.isDefined(currentMessage); assert.fail("Expected to abort streaming"); } catch (error: any) { assert.isTrue(error.message.includes("aborted")); diff --git a/sdk/openai/openai/test/public/assistants.spec.ts b/sdk/openai/openai/test/public/assistants.spec.ts index 6aa90ab9fd9..28080599772 100644 --- a/sdk/openai/openai/test/public/assistants.spec.ts +++ b/sdk/openai/openai/test/public/assistants.spec.ts @@ -5,8 +5,8 @@ import { matrix } from "@azure-tools/test-utils-vitest"; import { assert, describe, beforeEach, it } from "vitest"; import { assertAssistantEquality } from "./utils/asserts.js"; import { createClient } from "./utils/createClient.js"; -import OpenAI, { AzureOpenAI } from "openai"; -import { APIVersion, isRateLimitRun, Metadata } from "./utils/utils.js"; +import type { OpenAI, AzureOpenAI } from "openai"; +import { APIVersion, isRateLimitRun, type Metadata } from "./utils/utils.js"; describe("OpenAIAssistants", () => { matrix([[APIVersion.Preview]] as const, async function (apiVersion: APIVersion) { @@ -217,7 +217,7 @@ describe("OpenAIAssistants", () => { } const instructions = "Please address the user as Jane Doe. The user has a premium account."; - let run = await client.beta.threads.runs.createAndPoll(thread.id, { + const run = await client.beta.threads.runs.createAndPoll(thread.id, { assistant_id: assistant.id, instructions, }); diff --git a/sdk/openai/openai/test/public/completions.spec.ts b/sdk/openai/openai/test/public/completions.spec.ts index 03d1d7965ef..32d001ea55a 100644 --- a/sdk/openai/openai/test/public/completions.spec.ts +++ b/sdk/openai/openai/test/public/completions.spec.ts @@ -4,8 +4,8 @@ import { matrix } from "@azure-tools/test-utils-vitest"; import { assert, describe, beforeEach, it, beforeAll } from "vitest"; import { createClient } from "./utils/createClient.js"; -import { APIMatrix, APIVersion, DeploymentInfo } from "./utils/utils.js"; -import OpenAI, { AzureOpenAI } from "openai"; +import { APIMatrix, type APIVersion, type DeploymentInfo } from "./utils/utils.js"; +import type { OpenAI, AzureOpenAI } from "openai"; import { assertChatCompletions, assertChatCompletionsList, @@ -20,7 +20,7 @@ import { updateWithSucceeded, withDeployments, } from "./utils/utils.js"; -import { ChatCompletionMessageParam } from "openai/resources/chat/completions.mjs"; +import { type ChatCompletionMessageParam } from "openai/resources/chat/completions.mjs"; import { completionsModelsToSkip, functionCallModelsToSkip } from "./utils/models.js"; import "../../src/types/index.js"; @@ -364,7 +364,7 @@ describe("Completions", function () { if (!content) assert.fail("Undefined content"); try { JSON.parse(content); - } catch (e) { + } catch { assert.fail(`Invalid JSON: ${content}`); } }, diff --git a/sdk/openai/openai/test/public/embeddings.spec.ts b/sdk/openai/openai/test/public/embeddings.spec.ts index fdbdb3df4ef..12e2f335af0 100644 --- a/sdk/openai/openai/test/public/embeddings.spec.ts +++ b/sdk/openai/openai/test/public/embeddings.spec.ts @@ -6,12 +6,12 @@ import { describe, it, beforeAll } from "vitest"; import { createClient } from "./utils/createClient.js"; import { APIMatrix, - APIVersion, - DeploymentInfo, + type APIVersion, + type DeploymentInfo, getDeployments, withDeployments, } from "./utils/utils.js"; -import OpenAI, { AzureOpenAI } from "openai"; +import type { OpenAI, AzureOpenAI } from "openai"; import { assertEmbeddings } from "./utils/asserts.js"; describe("Embeddings", function () { diff --git a/sdk/openai/openai/test/public/images.spec.ts b/sdk/openai/openai/test/public/images.spec.ts index 2e724967bb2..4e42c562ead 100644 --- a/sdk/openai/openai/test/public/images.spec.ts +++ b/sdk/openai/openai/test/public/images.spec.ts @@ -5,15 +5,15 @@ import { matrix } from "@azure-tools/test-utils-vitest"; import { createClient } from "./utils/createClient.js"; import { APIMatrix, - APIVersion, - DeploymentInfo, + type APIVersion, + type DeploymentInfo, getDeployments, getSucceeded, updateWithSucceeded, withDeployments, } from "./utils/utils.js"; import { assertImagesWithJSON, assertImagesWithURLs } from "./utils/asserts.js"; -import OpenAI, { AzureOpenAI } from "openai"; +import type { OpenAI, AzureOpenAI } from "openai"; import { describe, it, beforeAll } from "vitest"; describe("Images", function () { diff --git a/sdk/openai/openai/test/public/node/assistantsFile.spec.ts b/sdk/openai/openai/test/public/node/assistantsFile.spec.ts index c8aa500b7f6..4fd6582783b 100644 --- a/sdk/openai/openai/test/public/node/assistantsFile.spec.ts +++ b/sdk/openai/openai/test/public/node/assistantsFile.spec.ts @@ -3,7 +3,7 @@ import { matrix } from "@azure-tools/test-utils-vitest"; import { assert, describe, beforeEach, it } from "vitest"; -import OpenAI, { AzureOpenAI, toFile } from "openai"; +import { type OpenAI, type AzureOpenAI, toFile } from "openai"; import { createClient } from "../utils/createClient.js"; import { APIVersion } from "../utils/utils.js"; diff --git a/sdk/openai/openai/test/public/node/batches.spec.ts b/sdk/openai/openai/test/public/node/batches.spec.ts index 715bcb16114..ed80c2c9697 100644 --- a/sdk/openai/openai/test/public/node/batches.spec.ts +++ b/sdk/openai/openai/test/public/node/batches.spec.ts @@ -1,13 +1,19 @@ // Copyright (c) Microsoft Corporation. -// Licensed under the MIT license. +// Licensed under the MIT License. import { matrix } from "@azure-tools/test-utils-vitest"; import { describe, beforeEach, it } from "vitest"; -import OpenAI, { AzureOpenAI, toFile } from "openai"; +import { type OpenAI, type AzureOpenAI, toFile } from "openai"; import { createClient } from "../utils/createClient.js"; -import { APIVersion, DeploymentInfo, getDeployments, withDeployments } from "../utils/utils.js"; +import { + APIVersion, + type DeploymentInfo, + getDeployments, + withDeployments, +} from "../utils/utils.js"; import { assertBatch, assertNonEmptyArray } from "../utils/asserts.js"; import { delay } from "@azure-tools/test-recorder"; +import type { FileObject } from "openai/resources/index"; describe("Batches", () => { matrix([[APIVersion.Preview]] as const, async function (apiVersion: APIVersion) { @@ -21,9 +27,9 @@ describe("Batches", () => { }); describe("all CRUD APIs", function () { - async function createBatchFile(deploymentName: string) { + async function createBatchFile(deploymentName: string): Promise { const inputObject = `{ "custom_id": "request-1", "method": "POST", "url": "/v1/chat/completions", "body": { "model": "${deploymentName}", "messages": [{ "role": "system", "content": "You are a helpful assistant." }, { "role": "user", "content": "What is 2+2?" }] } }`; - let file = await client.files.create({ + const file = await client.files.create({ file: await toFile(Buffer.from(inputObject), "batch.jsonl"), purpose: "batch", }); diff --git a/sdk/openai/openai/test/public/node/whisper.spec.ts b/sdk/openai/openai/test/public/node/whisper.spec.ts index ad36ffe8647..902e6c6d1ba 100644 --- a/sdk/openai/openai/test/public/node/whisper.spec.ts +++ b/sdk/openai/openai/test/public/node/whisper.spec.ts @@ -5,17 +5,17 @@ import { createReadStream } from "fs"; import { matrix } from "@azure-tools/test-utils-vitest"; import { describe, it, beforeAll } from "vitest"; import { createClient } from "../utils/createClient.js"; -import OpenAI, { AzureOpenAI } from "openai"; +import type { OpenAI, AzureOpenAI } from "openai"; import { APIMatrix, - APIVersion, - DeploymentInfo, + type APIVersion, + type DeploymentInfo, getDeployments, maxRetriesOption, withDeployments, } from "../utils/utils.js"; import { assertAudioResult } from "../utils/asserts.js"; -import { AudioResultFormat } from "../utils/audioTypes.js"; +import type { AudioResultFormat } from "../utils/audioTypes.js"; describe("OpenAI", function () { matrix([APIMatrix] as const, async function (apiVersion: APIVersion) { diff --git a/sdk/openai/openai/test/public/tts.spec.ts b/sdk/openai/openai/test/public/tts.spec.ts index e692aea9989..fcf78142b1e 100644 --- a/sdk/openai/openai/test/public/tts.spec.ts +++ b/sdk/openai/openai/test/public/tts.spec.ts @@ -1,11 +1,11 @@ // Copyright (c) Microsoft Corporation. -// Licensed under the MIT license. +// Licensed under the MIT License. import { matrix } from "@azure-tools/test-utils-vitest"; import { describe, beforeEach, it, assert, beforeAll } from "vitest"; import { createClient } from "./utils/createClient.js"; -import { APIVersion, DeploymentInfo, getDeployments, withDeployments } from "./utils/utils.js"; -import OpenAI, { AzureOpenAI } from "openai"; +import { APIVersion, type DeploymentInfo, getDeployments, withDeployments } from "./utils/utils.js"; +import type { OpenAI, AzureOpenAI } from "openai"; import { ttsModelsToSkip } from "./utils/models.js"; describe("OpenAI", function () { diff --git a/sdk/openai/openai/test/public/utils/asserts.ts b/sdk/openai/openai/test/public/utils/asserts.ts index 3612f02f6c4..8d9debc5aa9 100644 --- a/sdk/openai/openai/test/public/utils/asserts.ts +++ b/sdk/openai/openai/test/public/utils/asserts.ts @@ -1,7 +1,10 @@ +// Copyright (c) Microsoft Corporation. +// Licensed under the MIT License. + import { assert } from "vitest"; -import { get, Metadata } from "./utils.js"; +import { get, type Metadata } from "./utils.js"; import { getImageDimensionsFromResponse, getImageDimensionsFromString } from "./images.js"; -import { +import type { AzureChatExtensionDataSourceResponseCitationOutput, AzureChatExtensionsMessageContextOutput, ContentFilterCitedDetectionResultOutput, @@ -11,8 +14,8 @@ import { ContentFilterResultsForChoiceOutput, ContentFilterResultsForPromptOutput, } from "../../../src/types/index.js"; -import { Assistant, AssistantCreateParams } from "openai/resources/beta/assistants.mjs"; -import { +import type { Assistant, AssistantCreateParams } from "openai/resources/beta/assistants.mjs"; +import type { Batch, BatchError, BatchRequestCounts, @@ -25,13 +28,13 @@ import { CreateEmbeddingResponse, ImagesResponse, } from "openai/resources/index"; -import { ErrorModel } from "@azure-rest/core-client"; -import { +import type { ErrorModel } from "@azure-rest/core-client"; +import type { ChatCompletion, ChatCompletionMessageToolCall, } from "openai/resources/chat/completions.mjs"; -import { Transcription } from "openai/resources/audio/transcriptions.mjs"; -import { AudioSegment, AudioResultVerboseJson, AudioResultFormat } from "./audioTypes.js"; +import type { Transcription } from "openai/resources/audio/transcriptions.mjs"; +import type { AudioSegment, AudioResultVerboseJson, AudioResultFormat } from "./audioTypes.js"; export function assertAudioResult(responseFormat: AudioResultFormat, result: Transcription): void { switch (responseFormat) { @@ -83,7 +86,7 @@ export function assertChatCompletions( function assertChatCompletionsNoUsage( completions: ChatCompletion, - { allowEmptyChoices, allowEmptyId, ...opts }: ChatCompletionTestOptions, + { allowEmptyChoices, ...opts }: ChatCompletionTestOptions, ): void { if (!allowEmptyChoices || completions.choices.length > 0) { assertNonEmptyArray(completions.choices, (choice) => assertChoice(choice, opts)); @@ -93,7 +96,7 @@ function assertChatCompletionsNoUsage( function assertChatCompletionsChunkNoUsage( completions: ChatCompletionChunk, - { allowEmptyChoices, allowEmptyId, ...opts }: ChatCompletionTestOptions, + { allowEmptyChoices, ...opts }: ChatCompletionTestOptions, ): void { if (!allowEmptyChoices || completions.choices.length > 0) { assertNonEmptyArray(completions.choices, (choice) => assertChoice(choice, opts)); @@ -220,8 +223,8 @@ function assertChoice( if (stream) { const delta = (choice as ChatCompletionChunk.Choice).delta; // TODO: Relevant issue https://github.com/openai/openai-python/issues/1677 - ifDefined(delta, (delta) => { - assertMessage(delta, options); + ifDefined(delta, (d) => { + assertMessage(d, options); }); assert.isFalse("message" in choice); } else { @@ -384,7 +387,7 @@ export function assertImagesWithJSON(image: ImagesResponse, height: number, widt export function assertEmbeddings( embeddings: CreateEmbeddingResponse, options?: EmbeddingTestOptions, -) { +): void { assert.isNotNull(embeddings.data); assert.equal(embeddings.data.length > 0, true); assert.isNotNull(embeddings.data[0].embedding); diff --git a/sdk/openai/openai/test/public/utils/audioTypes.ts b/sdk/openai/openai/test/public/utils/audioTypes.ts index f260f9baf35..282df7ac807 100644 --- a/sdk/openai/openai/test/public/utils/audioTypes.ts +++ b/sdk/openai/openai/test/public/utils/audioTypes.ts @@ -1,4 +1,7 @@ -import { Transcription } from "openai/resources/audio/transcriptions.mjs"; +// Copyright (c) Microsoft Corporation. +// Licensed under the MIT License. + +import type { Transcription } from "openai/resources/audio/transcriptions.mjs"; export type AudioResultFormat = "json" | "verbose_json" | "text" | "srt" | "vtt"; diff --git a/sdk/openai/openai/test/public/utils/createClient.ts b/sdk/openai/openai/test/public/utils/createClient.ts index d2ea0f9f156..c58d661058a 100644 --- a/sdk/openai/openai/test/public/utils/createClient.ts +++ b/sdk/openai/openai/test/public/utils/createClient.ts @@ -3,11 +3,11 @@ import { Recorder, - RecorderStartOptions, - TestInfo, + type RecorderStartOptions, + type TestInfo, assertEnvironmentVariable, } from "@azure-tools/test-recorder"; -import OpenAI, { AzureClientOptions, AzureOpenAI } from "openai"; +import OpenAI, { type AzureClientOptions, AzureOpenAI } from "openai"; import { getBearerTokenProvider } from "@azure/identity"; import { EnvironmentVariableNames, @@ -15,7 +15,7 @@ import { EnvironmentVariableNamesForVision, EnvironmentVariableNamesForAudio, } from "./envVars.js"; -import { APIVersion, DeploymentType } from "./utils.js"; +import { APIVersion, type DeploymentType } from "./utils.js"; import { createTestCredential } from "@azure-tools/test-credential"; const scope = "https://cognitiveservices.azure.com/.default"; diff --git a/sdk/openai/openai/test/public/utils/images-browser.mts b/sdk/openai/openai/test/public/utils/images-browser.mts index 83419a43e31..7dfab216654 100644 --- a/sdk/openai/openai/test/public/utils/images-browser.mts +++ b/sdk/openai/openai/test/public/utils/images-browser.mts @@ -1,7 +1,7 @@ // Copyright (c) Microsoft Corporation. // Licensed under the MIT License. -import { PipelineResponse } from "@azure/core-rest-pipeline"; +import type { PipelineResponse } from "@azure/core-rest-pipeline"; interface Dimensions { width: number; diff --git a/sdk/openai/openai/test/public/utils/images.ts b/sdk/openai/openai/test/public/utils/images.ts index d4701640a74..cac78b802cc 100644 --- a/sdk/openai/openai/test/public/utils/images.ts +++ b/sdk/openai/openai/test/public/utils/images.ts @@ -1,7 +1,7 @@ // Copyright (c) Microsoft Corporation. // Licensed under the MIT License. -import { PipelineResponse } from "@azure/core-rest-pipeline"; +import type { PipelineResponse } from "@azure/core-rest-pipeline"; export interface Dimensions { width: number; diff --git a/sdk/openai/openai/test/public/utils/logging.ts b/sdk/openai/openai/test/public/utils/logging.ts index c0ecfd802aa..0cb370fa126 100644 --- a/sdk/openai/openai/test/public/utils/logging.ts +++ b/sdk/openai/openai/test/public/utils/logging.ts @@ -2,7 +2,7 @@ // Licensed under the MIT License. import { afterAll, beforeAll, inject } from "vitest"; -import { AzureLogLevel, setLogLevel } from "@azure/logger"; +import { type AzureLogLevel, setLogLevel } from "@azure/logger"; const logLevel = inject("AZURE_LOG_LEVEL") as AzureLogLevel; const localStorage: { debug?: string } = {}; diff --git a/sdk/openai/openai/test/public/utils/setup.ts b/sdk/openai/openai/test/public/utils/setup.ts index 0f7b45e2294..481d3d05596 100644 --- a/sdk/openai/openai/test/public/utils/setup.ts +++ b/sdk/openai/openai/test/public/utils/setup.ts @@ -6,10 +6,10 @@ enum EnvVarKeys { AZURE_LOG_LEVEL = "AZURE_LOG_LEVEL", } declare module "vitest" { - type MyEnvVarKeys = { + type MyEnvVarKey = { [K in keyof typeof EnvVarKeys]: string; }; - export interface ProvidedContext extends MyEnvVarKeys {} + export interface ProvidedContext extends MyEnvVarKey {} } const defaultLogLevel = "info"; @@ -28,7 +28,7 @@ function assertEnvironmentVariable(key: string): string { return value; } -export default async function ({ provide }: GlobalSetupContext) { +export default async function ({ provide }: GlobalSetupContext): Promise<() => void> { for (const key of Object.values(EnvVarKeys)) { provide(key, assertEnvironmentVariable(key)); } diff --git a/sdk/openai/openai/test/public/utils/utils.ts b/sdk/openai/openai/test/public/utils/utils.ts index 45037b782c1..13191546ea2 100644 --- a/sdk/openai/openai/test/public/utils/utils.ts +++ b/sdk/openai/openai/test/public/utils/utils.ts @@ -3,8 +3,8 @@ import { assert } from "vitest"; import { - PipelineRequest, - PipelineResponse, + type PipelineRequest, + type PipelineResponse, RestError, createDefaultHttpClient, createEmptyPipeline, @@ -21,9 +21,9 @@ import { EnvironmentVariableNamesForVision, EnvironmentVariableNamesForAudio, } from "./envVars.js"; -import { Run } from "openai/resources/beta/threads/runs/runs.mjs"; +import type { Run } from "openai/resources/beta/threads/runs/runs.mjs"; import { createClientLogger } from "@azure/logger"; -import { AzureChatExtensionConfiguration } from "../../../src/types/models.js"; +import type { AzureChatExtensionConfiguration } from "../../../src/types/models.js"; const logger = createClientLogger("openai"); diff --git a/sdk/openai/openai/test/public/vision.spec.ts b/sdk/openai/openai/test/public/vision.spec.ts index d9924d488f1..ec94ea5b553 100644 --- a/sdk/openai/openai/test/public/vision.spec.ts +++ b/sdk/openai/openai/test/public/vision.spec.ts @@ -7,12 +7,12 @@ import { createClient } from "./utils/createClient.js"; import { assertChatCompletions } from "./utils/asserts.js"; import { APIMatrix, - APIVersion, - DeploymentInfo, + type APIVersion, + type DeploymentInfo, getDeployments, withDeployments, } from "./utils/utils.js"; -import OpenAI, { AzureOpenAI } from "openai"; +import type { OpenAI, AzureOpenAI } from "openai"; import { logger } from "@azure/identity"; import { RestError } from "@azure/core-rest-pipeline"; import { visionModelsToSkip } from "./utils/models.js";