-
Notifications
You must be signed in to change notification settings - Fork 101
Refactor client tests, deprecate addProtocolIfNotPresent, fix abort bug, adjust documentation
#1926
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
flevi29
wants to merge
14
commits into
meilisearch:main
Choose a base branch
from
flevi29:improve-and-fix-http-requests
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+418
−970
Open
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
5e97cf8
Progress
flevi29 4fea08f
Merge branch 'main' into improve-and-fix-http-requests
flevi29 e3c20a3
Misc
flevi29 31c7eae
Merge branch 'main' into improve-and-fix-http-requests
flevi29 ad8db0a
Properly restore mocks
flevi29 7a02450
Add error tests
flevi29 837aafb
Misc comments
flevi29 1cf023a
Refactor error tests, move them to meilisearch tests
flevi29 ace1430
Rename test
flevi29 dd4b147
Misc
flevi29 1c1dfbc
Improve error tests
flevi29 519a27f
Merge branch 'main' into improve-and-fix-http-requests
flevi29 e75b4d2
Fix typo
flevi29 da66f97
Merge branch 'main' into improve-and-fix-http-requests
flevi29 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| import { test } from "vitest"; | ||
| import { assert, getClient } from "./utils/meilisearch-test-utils.js"; | ||
|
|
||
| const ms = await getClient("Master"); | ||
|
|
||
| test(`${ms.health.name} method`, async () => { | ||
| const health = await ms.health(); | ||
| assert.strictEqual(Object.keys(health).length, 1); | ||
| assert.strictEqual(health.status, "available"); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,255 @@ | ||
| import { | ||
| afterAll, | ||
| afterEach, | ||
| beforeAll, | ||
| describe, | ||
| test, | ||
| vi, | ||
| type MockInstance, | ||
| } from "vitest"; | ||
| import { | ||
| MeiliSearch, | ||
| MeiliSearchRequestTimeOutError, | ||
| MeiliSearchRequestError, | ||
| MeiliSearchError, | ||
| MeiliSearchApiError, | ||
| type MeiliSearchErrorResponse, | ||
| } from "../src/index.js"; | ||
| import { assert, HOST } from "./utils/meilisearch-test-utils.js"; | ||
|
|
||
| describe("abort", () => { | ||
| let spy: MockInstance<typeof fetch>; | ||
| beforeAll(() => { | ||
| spy = vi.spyOn(globalThis, "fetch").mockImplementation((_input, init) => { | ||
| assert.isDefined(init); | ||
| const signal = init.signal; | ||
| assert.isDefined(signal); | ||
| assert.isNotNull(signal); | ||
|
|
||
| return new Promise((_resolve, reject) => { | ||
| if (signal.aborted) { | ||
| // eslint-disable-next-line @typescript-eslint/prefer-promise-reject-errors | ||
| reject(signal.reason as unknown); | ||
| } | ||
|
|
||
| signal.onabort = function () { | ||
| // eslint-disable-next-line @typescript-eslint/prefer-promise-reject-errors | ||
| reject(signal.reason); | ||
| signal.removeEventListener("abort", this.onabort!); | ||
| }; | ||
| }); | ||
| }); | ||
| }); | ||
|
|
||
| afterAll(() => { | ||
| spy.mockRestore(); | ||
| }); | ||
|
|
||
| test.concurrent("with global timeout", async () => { | ||
| const timeout = 1; | ||
| const ms = new MeiliSearch({ host: HOST, timeout }); | ||
|
|
||
| const error = await assert.rejects(ms.health(), MeiliSearchRequestError); | ||
| assert.instanceOf(error.cause, MeiliSearchRequestTimeOutError); | ||
| assert.strictEqual(error.cause.cause.timeout, timeout); | ||
| }); | ||
|
|
||
| test.concurrent("with signal", async () => { | ||
| const ms = new MeiliSearch({ host: HOST }); | ||
| const reason = Symbol("<reason>"); | ||
|
|
||
| const error = await assert.rejects( | ||
| ms.multiSearch({ queries: [] }, { signal: AbortSignal.abort(reason) }), | ||
| MeiliSearchRequestError, | ||
| ); | ||
| assert.strictEqual(error.cause, reason); | ||
| }); | ||
|
|
||
| test.concurrent("with signal with a timeout", async () => { | ||
| const ms = new MeiliSearch({ host: HOST }); | ||
|
|
||
| const error = await assert.rejects( | ||
| ms.multiSearch({ queries: [] }, { signal: AbortSignal.timeout(5) }), | ||
| MeiliSearchRequestError, | ||
| ); | ||
|
|
||
| assert.strictEqual( | ||
| String(error.cause), | ||
| "TimeoutError: The operation was aborted due to timeout", | ||
| ); | ||
| }); | ||
|
|
||
| test.concurrent.for([ | ||
| [2, 1], | ||
| [1, 2], | ||
| ] as const)( | ||
| "with global timeout of %ims and signal timeout of %ims", | ||
| async ([timeout, signalTimeout]) => { | ||
| const ms = new MeiliSearch({ host: HOST, timeout }); | ||
|
|
||
| const error = await assert.rejects( | ||
| ms.multiSearch( | ||
| { queries: [] }, | ||
| { signal: AbortSignal.timeout(signalTimeout) }, | ||
| ), | ||
| MeiliSearchRequestError, | ||
| ); | ||
|
|
||
| if (timeout > signalTimeout) { | ||
| assert.strictEqual( | ||
| String(error.cause), | ||
| "TimeoutError: The operation was aborted due to timeout", | ||
| ); | ||
| } else { | ||
| assert.instanceOf(error.cause, MeiliSearchRequestTimeOutError); | ||
| assert.strictEqual(error.cause.cause.timeout, timeout); | ||
| } | ||
| }, | ||
| ); | ||
|
|
||
| test.concurrent( | ||
| "with global timeout and immediately aborted signal", | ||
| async () => { | ||
| const ms = new MeiliSearch({ host: HOST, timeout: 1 }); | ||
| const reason = Symbol("<reason>"); | ||
|
|
||
| const error = await assert.rejects( | ||
| ms.multiSearch({ queries: [] }, { signal: AbortSignal.abort(reason) }), | ||
| MeiliSearchRequestError, | ||
| ); | ||
|
|
||
| assert.strictEqual(error.cause, reason); | ||
| }, | ||
| ); | ||
| }); | ||
|
|
||
| test("headers with API key, clientAgents, global headers, and custom headers", async () => { | ||
| using spy = (() => { | ||
| const spy = vi | ||
| .spyOn(globalThis, "fetch") | ||
| .mockImplementation(() => Promise.resolve(new Response())); | ||
|
|
||
| return { | ||
| get value() { | ||
| return spy; | ||
| }, | ||
| [Symbol.dispose]() { | ||
| spy.mockRestore(); | ||
| }, | ||
| }; | ||
| })(); | ||
|
|
||
| const apiKey = "secrète"; | ||
| const clientAgents = ["TEST"]; | ||
| const globalHeaders = { my: "feather", not: "helper", extra: "header" }; | ||
|
|
||
| const ms = new MeiliSearch({ | ||
| host: HOST, | ||
| apiKey, | ||
| clientAgents, | ||
| requestInit: { headers: globalHeaders }, | ||
| }); | ||
|
|
||
| const customHeaders = { my: "header", not: "yours" }; | ||
| await ms.multiSearch({ queries: [] }, { headers: customHeaders }); | ||
|
|
||
| const { calls } = spy.value.mock; | ||
| assert.lengthOf(calls, 1); | ||
|
|
||
| const headers = calls[0][1]?.headers; | ||
| assert.isDefined(headers); | ||
| assert.instanceOf(headers, Headers); | ||
|
|
||
| const xMeilisearchClientKey = "x-meilisearch-client"; | ||
| const xMeilisearchClient = headers.get(xMeilisearchClientKey); | ||
| headers.delete(xMeilisearchClientKey); | ||
|
|
||
| assert.isNotNull(xMeilisearchClient); | ||
| assert.sameMembers( | ||
| xMeilisearchClient.split(" ; ").slice(0, -1), | ||
| clientAgents, | ||
| ); | ||
|
|
||
| const authorizationKey = "authorization"; | ||
| const authorization = headers.get(authorizationKey); | ||
| headers.delete(authorizationKey); | ||
|
|
||
| assert.strictEqual(authorization, `Bearer ${apiKey}`); | ||
|
|
||
| // note how they overwrite each other, top priority being the custom headers | ||
| assert.deepEqual(Object.fromEntries(headers.entries()), { | ||
| "content-type": "application/json", | ||
| ...globalHeaders, | ||
| ...customHeaders, | ||
| }); | ||
| }); | ||
|
|
||
| test.concurrent("custom http client", async () => { | ||
| const httpClient = vi.fn((..._params: Parameters<typeof fetch>) => | ||
| Promise.resolve(new Response()), | ||
| ); | ||
|
|
||
| const ms = new MeiliSearch({ host: HOST, httpClient }); | ||
| await ms.health(); | ||
|
|
||
| assert.lengthOf(httpClient.mock.calls, 1); | ||
| const input = httpClient.mock.calls[0][0]; | ||
|
|
||
| assert.instanceOf(input, URL); | ||
| assert(input.href.startsWith(HOST)); | ||
| }); | ||
|
|
||
| describe("errors", () => { | ||
| let spy: MockInstance<typeof fetch>; | ||
| beforeAll(() => { | ||
| spy = vi.spyOn(globalThis, "fetch"); | ||
| }); | ||
|
|
||
| afterAll(() => { | ||
| spy.mockRestore(); | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| spy.mockReset(); | ||
| }); | ||
|
|
||
| test(`${MeiliSearchError.name}`, () => { | ||
| assert.throws( | ||
| () => new MeiliSearch({ host: "http:// invalid URL" }), | ||
| MeiliSearchError, | ||
| "The provided host is not valid", | ||
| ); | ||
| }); | ||
|
|
||
| test(`${MeiliSearchRequestError.name}`, async () => { | ||
| const simulatedError = new TypeError("simulated network error"); | ||
| spy.mockImplementation(() => Promise.reject(simulatedError)); | ||
|
|
||
| const ms = new MeiliSearch({ host: "https://politi.dk/en/" }); | ||
| const error = await assert.rejects(ms.health(), MeiliSearchRequestError); | ||
| assert.typeOf(error.message, "string"); | ||
| assert.deepEqual(error.cause, simulatedError); | ||
| }); | ||
|
|
||
| test(`${MeiliSearchApiError.name}`, async () => { | ||
| const simulatedCause: MeiliSearchErrorResponse = { | ||
| message: "message", | ||
| code: "code", | ||
| type: "type", | ||
| link: "link", | ||
| }; | ||
| spy.mockImplementation(() => | ||
| Promise.resolve( | ||
| new Response(JSON.stringify(simulatedCause), { status: 400 }), | ||
| ), | ||
| ); | ||
|
|
||
| const ms = new MeiliSearch({ host: "https://polisen.se/en/" }); | ||
| const error = await assert.rejects(ms.health(), MeiliSearchApiError); | ||
| assert.typeOf(error.message, "string"); | ||
| assert.deepEqual(error.cause, simulatedCause); | ||
| assert.instanceOf(error.response, Response); | ||
| }); | ||
|
|
||
| // MeiliSearchTaskTimeOutError is tested by tasks-and-batches tests | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| import { test, afterAll } from "vitest"; | ||
| import { assert, getClient } from "./utils/meilisearch-test-utils.js"; | ||
| import type { Remote } from "../src/index.js"; | ||
|
|
||
| const ms = await getClient("Master"); | ||
|
|
||
| afterAll(async () => { | ||
| await ms.updateNetwork({ | ||
| remotes: { | ||
| // TODO: Better types for Network | ||
| // @ts-expect-error This should be accepted | ||
| soi: null, | ||
| }, | ||
| }); | ||
| }); | ||
|
|
||
| test(`${ms.updateNetwork.name} and ${ms.getNetwork.name} method`, async () => { | ||
| const network = { | ||
| self: "soi", | ||
| remotes: { | ||
| soi: { | ||
| url: "https://france-visas.gouv.fr/", | ||
| searchApiKey: "hemmelighed", | ||
| }, | ||
| }, | ||
| }; | ||
|
|
||
| function validateRemotes(remotes: Record<string, Remote>) { | ||
| for (const [key, val] of Object.entries(remotes)) { | ||
| if (key !== "soi") { | ||
| assert.lengthOf(Object.keys(val), 2); | ||
| assert.typeOf(val.url, "string"); | ||
| assert( | ||
| typeof val.searchApiKey === "string" || val.searchApiKey === null, | ||
| ); | ||
| delete remotes[key]; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| const updateResponse = await ms.updateNetwork(network); | ||
| validateRemotes(updateResponse.remotes); | ||
| assert.deepEqual(updateResponse, network); | ||
|
|
||
| const getResponse = await ms.getNetwork(); | ||
| validateRemotes(getResponse.remotes); | ||
| assert.deepEqual(getResponse, network); | ||
| }); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| import { test } from "vitest"; | ||
| import { assert, getClient } from "./utils/meilisearch-test-utils.js"; | ||
|
|
||
| const ms = await getClient("Master"); | ||
|
|
||
| test(`${ms.getStats.name} method`, async () => { | ||
| const stats = await ms.getStats(); | ||
| assert.strictEqual(Object.keys(stats).length, 4); | ||
| const { databaseSize, usedDatabaseSize, lastUpdate, indexes } = stats; | ||
| assert.typeOf(databaseSize, "number"); | ||
| assert.typeOf(usedDatabaseSize, "number"); | ||
| assert(typeof lastUpdate === "string" || lastUpdate === null); | ||
|
|
||
| for (const indexStats of Object.values(indexes)) { | ||
| assert.lengthOf(Object.keys(indexStats), 7); | ||
| const { | ||
| numberOfDocuments, | ||
| isIndexing, | ||
| fieldDistribution, | ||
| numberOfEmbeddedDocuments, | ||
| numberOfEmbeddings, | ||
| rawDocumentDbSize, | ||
| avgDocumentSize, | ||
| } = indexStats; | ||
|
|
||
| assert.typeOf(numberOfDocuments, "number"); | ||
| assert.typeOf(isIndexing, "boolean"); | ||
| assert.typeOf(numberOfEmbeddedDocuments, "number"); | ||
| assert.typeOf(numberOfEmbeddings, "number"); | ||
| assert.typeOf(rawDocumentDbSize, "number"); | ||
| assert.typeOf(avgDocumentSize, "number"); | ||
|
|
||
| for (const val of Object.values(fieldDistribution)) { | ||
| assert.typeOf(val, "number"); | ||
| } | ||
| } | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| import { test } from "vitest"; | ||
| import { assert, getClient } from "./utils/meilisearch-test-utils.js"; | ||
|
|
||
| const ms = await getClient("Master"); | ||
|
|
||
| test(`${ms.getVersion.name} method`, async () => { | ||
| const version = await ms.getVersion(); | ||
| assert.strictEqual(Object.keys(version).length, 3); | ||
| const { commitDate, commitSha, pkgVersion } = version; | ||
| for (const v of [commitDate, commitSha, pkgVersion]) { | ||
| assert.typeOf(v, "string"); | ||
| } | ||
| }); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.