315 lines
11 KiB
TypeScript
315 lines
11 KiB
TypeScript
import { describe, expect, it, vi } from "vitest";
|
|
import { createSearchClient, type MeiliFactory } from "./client.js";
|
|
import { createInMemoryMeili } from "./testing.js";
|
|
|
|
describe("createSearchClient — guards", () => {
|
|
it("rejects an empty index name", () => {
|
|
const { factory } = createInMemoryMeili();
|
|
const client = createSearchClient({ url: "http://memory", meiliFactory: factory });
|
|
expect(() => client.index("")).toThrow(/non-empty string/);
|
|
});
|
|
|
|
it("constructs against the real Meilisearch SDK when no factory is injected", () => {
|
|
// Exercises the default require()-based factory. Construction is offline
|
|
// (no network request is made until a method is actually called).
|
|
const client = createSearchClient({ url: "http://127.0.0.1:7700" });
|
|
expect(typeof client.index).toBe("function");
|
|
expect(typeof client.health).toBe("function");
|
|
});
|
|
|
|
it("reports unavailable when health returns a malformed payload", async () => {
|
|
const factory: MeiliFactory = () => ({
|
|
index() {
|
|
return {
|
|
async updateSettings() {},
|
|
async search() {
|
|
return { hits: [] };
|
|
},
|
|
async addDocuments() {},
|
|
async deleteDocuments() {},
|
|
};
|
|
},
|
|
async health() {
|
|
return null as unknown as { status: string };
|
|
},
|
|
});
|
|
const client = createSearchClient({ url: "http://memory", meiliFactory: factory });
|
|
await expect(client.health()).resolves.toEqual({ status: "unavailable" });
|
|
});
|
|
});
|
|
|
|
describe("IndexHandle.ensure — createIndex error handling", () => {
|
|
it("rethrows a non 'already exists' error from createIndex", async () => {
|
|
const { factory } = createInMemoryMeili({ throwOnIndex: true });
|
|
const client = createSearchClient({ url: "http://memory", meiliFactory: factory });
|
|
await expect(
|
|
client.index("x").ensure({ primaryKey: "id", settings: {} })
|
|
).rejects.toThrow(/meili offline/);
|
|
});
|
|
|
|
it("swallows an HTTP 409 conflict and still applies settings", async () => {
|
|
const updateSettings = vi.fn(async () => {});
|
|
const factory: MeiliFactory = () => ({
|
|
async createIndex() {
|
|
throw Object.assign(new Error("conflict"), { httpStatus: 409 });
|
|
},
|
|
index() {
|
|
return {
|
|
updateSettings,
|
|
async search() {
|
|
return { hits: [] };
|
|
},
|
|
async addDocuments() {},
|
|
async deleteDocuments() {},
|
|
};
|
|
},
|
|
async health() {
|
|
return { status: "available" };
|
|
},
|
|
});
|
|
const client = createSearchClient({ url: "http://memory", meiliFactory: factory });
|
|
await expect(
|
|
client.index("x").ensure({ primaryKey: "id", settings: { searchableAttributes: ["t"] } })
|
|
).resolves.toBeUndefined();
|
|
expect(updateSettings).toHaveBeenCalledOnce();
|
|
});
|
|
});
|
|
|
|
describe("IndexHandle.ensure — settings mapping", () => {
|
|
it("maps every settings field including synonyms onto the Meilisearch input", async () => {
|
|
let received: Record<string, unknown> | undefined;
|
|
// No createIndex member → exercises the branch where ensure skips
|
|
// index creation entirely.
|
|
const factory: MeiliFactory = () => ({
|
|
index() {
|
|
return {
|
|
async updateSettings(input: Record<string, unknown>) {
|
|
received = input;
|
|
},
|
|
async search() {
|
|
return { hits: [] };
|
|
},
|
|
async addDocuments() {},
|
|
async deleteDocuments() {},
|
|
};
|
|
},
|
|
async health() {
|
|
return { status: "available" };
|
|
},
|
|
});
|
|
const client = createSearchClient({ url: "http://memory", meiliFactory: factory });
|
|
await client.index("x").ensure({
|
|
primaryKey: "id",
|
|
settings: {
|
|
searchableAttributes: ["title"],
|
|
filterableAttributes: ["tenantId"],
|
|
sortableAttributes: ["createdAt"],
|
|
rankingRules: ["words"],
|
|
stopWords: ["the"],
|
|
synonyms: { car: ["auto", "vehicle"] },
|
|
},
|
|
});
|
|
expect(received).toEqual({
|
|
searchableAttributes: ["title"],
|
|
filterableAttributes: ["tenantId"],
|
|
sortableAttributes: ["createdAt"],
|
|
rankingRules: ["words"],
|
|
stopWords: ["the"],
|
|
synonyms: { car: ["auto", "vehicle"] },
|
|
});
|
|
});
|
|
});
|
|
|
|
describe("IndexHandle.search — result normalisation & option mapping", () => {
|
|
it("fills missing totals, omits absent limit/offset, and forwards every option", async () => {
|
|
let receivedOptions: Record<string, unknown> | undefined;
|
|
const factory: MeiliFactory = () => ({
|
|
index() {
|
|
return {
|
|
async updateSettings() {},
|
|
async search(_query: string, options: Record<string, unknown>) {
|
|
receivedOptions = options;
|
|
// Intentionally omit estimatedTotalHits / processingTimeMs /
|
|
// limit / offset to drive the default-and-omit branches.
|
|
return { hits: [{ id: "1", tenantId: "t1" }] };
|
|
},
|
|
async addDocuments() {},
|
|
async deleteDocuments() {},
|
|
};
|
|
},
|
|
async health() {
|
|
return { status: "available" };
|
|
},
|
|
});
|
|
const client = createSearchClient({ url: "http://memory", meiliFactory: factory });
|
|
const result = await client.index("x").search("hello", {
|
|
filter: ['tenantId = "t1"', "isPublished = true"],
|
|
sort: ["createdAt:desc"],
|
|
limit: 5,
|
|
offset: 2,
|
|
attributesToHighlight: ["title"],
|
|
attributesToRetrieve: ["id", "title"],
|
|
});
|
|
|
|
expect(result.estimatedTotalHits).toBe(1); // estimatedTotalHits ?? hits.length
|
|
expect(result.processingTimeMs).toBe(0); // processingTimeMs ?? 0
|
|
expect(result.limit).toBeUndefined();
|
|
expect(result.offset).toBeUndefined();
|
|
expect(result.hits).toEqual([{ document: { id: "1", tenantId: "t1" } }]);
|
|
|
|
expect(receivedOptions).toEqual({
|
|
filter: ['tenantId = "t1"', "isPublished = true"],
|
|
sort: ["createdAt:desc"],
|
|
limit: 5,
|
|
offset: 2,
|
|
attributesToHighlight: ["title"],
|
|
attributesToRetrieve: ["id", "title"],
|
|
});
|
|
});
|
|
});
|
|
|
|
describe("IndexHandle — empty batches are no-ops", () => {
|
|
it("does not touch the backend on an empty upsert or delete", async () => {
|
|
const addDocuments = vi.fn(async () => {});
|
|
const deleteDocuments = vi.fn(async () => {});
|
|
const factory: MeiliFactory = () => ({
|
|
index() {
|
|
return {
|
|
async updateSettings() {},
|
|
async search() {
|
|
return { hits: [] };
|
|
},
|
|
addDocuments,
|
|
deleteDocuments,
|
|
};
|
|
},
|
|
async health() {
|
|
return { status: "available" };
|
|
},
|
|
});
|
|
const client = createSearchClient({ url: "http://memory", meiliFactory: factory });
|
|
const handle = client.index("x");
|
|
await handle.upsert([]);
|
|
await handle.delete([]);
|
|
expect(addDocuments).not.toHaveBeenCalled();
|
|
expect(deleteDocuments).not.toHaveBeenCalled();
|
|
});
|
|
});
|
|
|
|
describe("IndexHandle.deleteByFilter", () => {
|
|
it("deletes documents matching a filter via deleteDocuments({ filter })", async () => {
|
|
const { factory, state } = createInMemoryMeili();
|
|
const client = createSearchClient({ url: "http://memory", meiliFactory: factory });
|
|
const handle = client.index<{ id: string; tenantId: string }>("kb");
|
|
await handle.ensure({ primaryKey: "id", settings: { filterableAttributes: ["tenantId"] } });
|
|
await handle.upsert([
|
|
{ id: "a", tenantId: "t1" },
|
|
{ id: "b", tenantId: "t2" },
|
|
]);
|
|
|
|
await handle.deleteByFilter('tenantId = "t1"');
|
|
|
|
const docs = state.indexes.get("kb")?.documents;
|
|
expect(docs?.has("a")).toBe(false);
|
|
expect(docs?.has("b")).toBe(true);
|
|
});
|
|
|
|
it("falls back to deleteDocumentsFilter when filtered deleteDocuments is unsupported", async () => {
|
|
const deleteDocumentsFilter = vi.fn(async () => {});
|
|
const factory: MeiliFactory = () => ({
|
|
index() {
|
|
return {
|
|
async updateSettings() {},
|
|
async search() {
|
|
return { hits: [] };
|
|
},
|
|
async addDocuments() {},
|
|
async deleteDocuments(input: readonly (string | number)[] | { filter: string }) {
|
|
if (!Array.isArray(input)) {
|
|
throw new Error("filter delete is unsupported on this build");
|
|
}
|
|
},
|
|
deleteDocumentsFilter,
|
|
};
|
|
},
|
|
async health() {
|
|
return { status: "available" };
|
|
},
|
|
});
|
|
const client = createSearchClient({ url: "http://memory", meiliFactory: factory });
|
|
await client.index("kb").deleteByFilter('tenantId = "t1"');
|
|
expect(deleteDocumentsFilter).toHaveBeenCalledWith({ filter: 'tenantId = "t1"' });
|
|
});
|
|
|
|
it("throws when no filter-delete capability exists at all", async () => {
|
|
const factory: MeiliFactory = () => ({
|
|
index() {
|
|
return {
|
|
async updateSettings() {},
|
|
async search() {
|
|
return { hits: [] };
|
|
},
|
|
async addDocuments() {},
|
|
async deleteDocuments(input: readonly (string | number)[] | { filter: string }) {
|
|
if (!Array.isArray(input)) {
|
|
throw new Error("filter delete is unknown on this build");
|
|
}
|
|
},
|
|
};
|
|
},
|
|
async health() {
|
|
return { status: "available" };
|
|
},
|
|
});
|
|
const client = createSearchClient({ url: "http://memory", meiliFactory: factory });
|
|
await expect(client.index("kb").deleteByFilter('tenantId = "t1"')).rejects.toThrow(
|
|
/does not support deleteByFilter/
|
|
);
|
|
});
|
|
|
|
it("rethrows an error from deleteDocuments that is not a 'filter unsupported' signal", async () => {
|
|
const factory: MeiliFactory = () => ({
|
|
index() {
|
|
return {
|
|
async updateSettings() {},
|
|
async search() {
|
|
return { hits: [] };
|
|
},
|
|
async addDocuments() {},
|
|
async deleteDocuments() {
|
|
throw new Error("backend exploded");
|
|
},
|
|
};
|
|
},
|
|
async health() {
|
|
return { status: "available" };
|
|
},
|
|
});
|
|
const client = createSearchClient({ url: "http://memory", meiliFactory: factory });
|
|
await expect(client.index("kb").deleteByFilter('tenantId = "t1"')).rejects.toThrow(
|
|
/backend exploded/
|
|
);
|
|
});
|
|
});
|
|
|
|
describe("in-memory fixture behaviour relied on by worker integration tests", () => {
|
|
it("auto-creates a missing index on first search", async () => {
|
|
const { factory, state } = createInMemoryMeili();
|
|
const client = createSearchClient({ url: "http://memory", meiliFactory: factory });
|
|
const res = await client.index("lazy").search("", { limit: 10 });
|
|
expect(res.hits).toEqual([]);
|
|
expect(state.indexes.has("lazy")).toBe(true);
|
|
});
|
|
|
|
it("matches documents by terms inside array-valued fields", async () => {
|
|
const { factory } = createInMemoryMeili();
|
|
const client = createSearchClient({ url: "http://memory", meiliFactory: factory });
|
|
const handle = client.index<{ id: string; tenantId: string; tags: string[] }>("kb");
|
|
await handle.ensure({ primaryKey: "id", settings: { filterableAttributes: ["tenantId"] } });
|
|
await handle.upsert([{ id: "1", tenantId: "t1", tags: ["Onboarding", "Setup"] }]);
|
|
|
|
const res = await handle.search("setup", { filter: 'tenantId = "t1"', limit: 10 });
|
|
expect(res.hits.map((hit) => hit.document.id)).toEqual(["1"]);
|
|
});
|
|
});
|