Reorganize repo structure and standards references
This commit is contained in:
1 parent
3cb93a81be
commit
be2054274d
5885 files changed
+2588
-195
No files matched your search
@@ -0,0 +1,138 @@
|
||||
/**
|
||||
* In-memory fake of the Meilisearch SDK surface area exercised by
|
||||
* `createSearchClient`. Sufficient for unit and worker-integration tests
|
||||
* without spinning up a container.
|
||||
*/
|
||||
|
||||
import type { MeiliFactory } from "./client.js";
|
||||
|
||||
interface FakeIndex {
|
||||
documents: Map<string | number, Record<string, unknown>>;
|
||||
settings: Record<string, unknown>;
|
||||
primaryKey: string;
|
||||
}
|
||||
|
||||
export interface InMemoryMeiliOptions {
|
||||
readonly healthStatus?: "available" | "unavailable";
|
||||
readonly throwOnHealthCheck?: boolean;
|
||||
readonly throwOnSearch?: boolean;
|
||||
readonly throwOnIndex?: boolean;
|
||||
}
|
||||
|
||||
export interface InMemoryMeiliState {
|
||||
readonly indexes: ReadonlyMap<string, FakeIndex>;
|
||||
options: InMemoryMeiliOptions;
|
||||
}
|
||||
|
||||
export function createInMemoryMeili(options: InMemoryMeiliOptions = {}): {
|
||||
factory: MeiliFactory;
|
||||
state: InMemoryMeiliState;
|
||||
setOption<K extends keyof InMemoryMeiliOptions>(key: K, value: InMemoryMeiliOptions[K]): void;
|
||||
} {
|
||||
const indexes = new Map<string, FakeIndex>();
|
||||
const mutable: { options: InMemoryMeiliOptions } = { options: { ...options } };
|
||||
|
||||
const factory: MeiliFactory = () => ({
|
||||
async createIndex(uid, opts) {
|
||||
if (mutable.options.throwOnIndex) throw new Error("meili offline");
|
||||
if (indexes.has(uid)) {
|
||||
const error = new Error("index_already_exists") as Error & { code: string };
|
||||
error.code = "index_already_exists";
|
||||
throw error;
|
||||
}
|
||||
indexes.set(uid, { documents: new Map(), settings: {}, primaryKey: opts.primaryKey });
|
||||
},
|
||||
index(uid) {
|
||||
const ensureIndex = (): FakeIndex => {
|
||||
let idx = indexes.get(uid);
|
||||
if (!idx) {
|
||||
idx = { documents: new Map(), settings: {}, primaryKey: "id" };
|
||||
indexes.set(uid, idx);
|
||||
}
|
||||
return idx;
|
||||
};
|
||||
return {
|
||||
async updateSettings(input) {
|
||||
ensureIndex().settings = { ...ensureIndex().settings, ...input };
|
||||
},
|
||||
async search(query, opts) {
|
||||
if (mutable.options.throwOnSearch) {
|
||||
const err = new Error("meilisearch search failed") as Error & { code: string };
|
||||
err.code = "ECONNREFUSED";
|
||||
throw err;
|
||||
}
|
||||
const idx = ensureIndex();
|
||||
const lowered = query.toLowerCase();
|
||||
const filter = typeof opts.filter === "string" ? opts.filter : Array.isArray(opts.filter) ? opts.filter.join(" AND ") : "";
|
||||
const tenantId = extractTenantFromFilter(filter);
|
||||
const limit = typeof opts.limit === "number" ? opts.limit : 20;
|
||||
const offset = typeof opts.offset === "number" ? opts.offset : 0;
|
||||
const hits: Record<string, unknown>[] = [];
|
||||
for (const doc of idx.documents.values()) {
|
||||
if (tenantId !== undefined && doc.tenantId !== tenantId) continue;
|
||||
if (lowered.length > 0 && !documentMatches(doc, lowered)) continue;
|
||||
hits.push(doc);
|
||||
}
|
||||
return {
|
||||
hits: hits.slice(offset, offset + limit) as unknown as readonly (Record<string, unknown> & { id: string | number })[],
|
||||
estimatedTotalHits: hits.length,
|
||||
processingTimeMs: 0,
|
||||
limit,
|
||||
offset,
|
||||
};
|
||||
},
|
||||
async addDocuments(documents) {
|
||||
const idx = ensureIndex();
|
||||
for (const doc of documents) {
|
||||
const id = doc[idx.primaryKey];
|
||||
if (typeof id === "string" || typeof id === "number") {
|
||||
idx.documents.set(id, doc);
|
||||
}
|
||||
}
|
||||
},
|
||||
async deleteDocuments(input: readonly (string | number)[] | { filter: string }) {
|
||||
const idx = ensureIndex();
|
||||
if (Array.isArray(input)) {
|
||||
for (const id of input) idx.documents.delete(id);
|
||||
return;
|
||||
}
|
||||
const filter = (input as { filter: string }).filter;
|
||||
const tenantId = extractTenantFromFilter(filter);
|
||||
if (tenantId === undefined) return;
|
||||
for (const [id, doc] of idx.documents) {
|
||||
if (doc.tenantId === tenantId) idx.documents.delete(id);
|
||||
}
|
||||
},
|
||||
};
|
||||
},
|
||||
async health() {
|
||||
if (mutable.options.throwOnHealthCheck) throw new Error("meilisearch unhealthy");
|
||||
return { status: mutable.options.healthStatus ?? "available" };
|
||||
},
|
||||
});
|
||||
|
||||
return {
|
||||
factory,
|
||||
state: { indexes, get options() { return mutable.options; } },
|
||||
setOption(key, value) {
|
||||
mutable.options = { ...mutable.options, [key]: value };
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
function documentMatches(doc: Record<string, unknown>, lowered: string): boolean {
|
||||
for (const value of Object.values(doc)) {
|
||||
if (typeof value === "string" && value.toLowerCase().includes(lowered)) return true;
|
||||
if (Array.isArray(value)) {
|
||||
for (const item of value) {
|
||||
if (typeof item === "string" && item.toLowerCase().includes(lowered)) return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
function extractTenantFromFilter(filter: string): string | undefined {
|
||||
const match = /tenantId\s*=\s*"([^"]+)"/u.exec(filter);
|
||||
return match ? match[1] : undefined;
|
||||
}
|
||||
Reference in new issue
Block a user