|
1
|
import { SearchTypes } from "@medusajs/framework/types" |
|
2
|
import { resolveIndexDefinitions } from "../index" |
|
3
|
|
|
4
|
const baseDefinition = ( |
|
5
|
fields: Record<string, SearchTypes.SearchFieldDefinition> |
|
6
|
): SearchTypes.SearchIndexDefinition => ({ |
|
7
|
name: "product", |
|
8
|
entity: "product", |
|
9
|
fields: { |
|
10
|
id: { type: "keyword", filterable: true }, |
|
11
|
...fields, |
|
12
|
}, |
|
13
|
async *seed() {}, |
|
14
|
}) |
|
15
|
|
|
16
|
const resolve = (fields: Record<string, SearchTypes.SearchFieldDefinition>) => |
|
17
|
resolveIndexDefinitions({ |
|
18
|
definitions: [baseDefinition(fields)], |
|
19
|
default_provider: "test-provider", |
|
20
|
}) |
|
21
|
|
|
22
|
describe("resolveIndexDefinitions validation", () => { |
|
23
|
test("accepts a coherent definition", () => { |
|
24
|
const resolved = resolve({ |
|
25
|
title: { type: "text", searchable: true }, |
|
26
|
embedding: { type: "vector", dimensions: 3 }, |
|
27
|
}) |
|
28
|
|
|
29
|
expect(resolved.get("product")?.provider).toEqual("test-provider") |
|
30
|
}) |
|
31
|
|
|
32
|
test("rejects a vector field without dimensions", () => { |
|
33
|
expect(() => resolve({ embedding: { type: "vector" } })).toThrow( |
|
34
|
/must declare its "dimensions"/ |
|
35
|
) |
|
36
|
}) |
|
37
|
|
|
38
|
test("rejects an array of vectors", () => { |
|
39
|
expect(() => |
|
40
|
resolve({ embedding: { type: "vector", dimensions: 3, array: true } }) |
|
41
|
).toThrow(/cannot be an array/) |
|
42
|
}) |
|
43
|
|
|
44
|
test("accepts a vector field that embeds its own text", () => { |
|
45
|
const resolved = resolve({ |
|
46
|
embedding: { |
|
47
|
type: "vector", |
|
48
|
dimensions: 1536, |
|
49
|
embed: true, |
|
50
|
}, |
|
51
|
}) |
|
52
|
|
|
53
|
expect(resolved.get("product")?.fields.embedding.embed).toEqual(true) |
|
54
|
}) |
|
55
|
|
|
56
|
test("rejects embedding a source field path", () => { |
|
57
|
expect(() => |
|
58
|
resolve({ |
|
59
|
embedding: { |
|
60
|
type: "vector", |
|
61
|
dimensions: 3, |
|
62
|
embed: "title" as unknown as true, |
|
63
|
}, |
|
64
|
}) |
|
65
|
).toThrow(/must be true/) |
|
66
|
}) |
|
67
|
|
|
68
|
test("rejects free-text search on non-string fields", () => { |
|
69
|
expect(() => |
|
70
|
resolve({ price: { type: "float", searchable: true } }) |
|
71
|
).toThrow(/cannot be searched as free text/) |
|
72
|
}) |
|
73
|
}) |