|
| 1 | +import { beforeEach, describe, expect, it } from "vitest"; |
| 2 | +import { ListLatestEntriesUseCase } from "~/features/contentEntry/ListEntries/index.js"; |
| 3 | +import { CreateEntryUseCase } from "~/features/contentEntry/CreateEntry/index.js"; |
| 4 | +import { GetModelUseCase } from "~/features/contentModel/GetModel/index.js"; |
| 5 | +import { createAuthorWithSearchableJsonContextHandler } from "~tests/__helpers/handler/authorWithSearchableJson/context.js"; |
| 6 | +import type { IAuthorWithSearchableJsonCmsEntryValues } from "~tests/__helpers/models/authorWithSearchableJson.js"; |
| 7 | +import { AUTHOR_WITH_SEARCHABLE_JSON_MODEL_ID } from "~tests/__helpers/models/authorWithSearchableJson.js"; |
| 8 | +import type { CmsContext, CmsModel } from "~/types/index.js"; |
| 9 | + |
| 10 | +interface ValueTypeCase { |
| 11 | + type: string; |
| 12 | + summer: Record<string, any>; |
| 13 | + winter: Record<string, any>; |
| 14 | +} |
| 15 | + |
| 16 | +/** |
| 17 | + * Values stored in the searchable-json field are dynamically mapped by OpenSearch. |
| 18 | + * Non-text values (dates, numbers, booleans) get non-text mappings, and wildcard queries |
| 19 | + * are not allowed on those. Full-text search must not break because of it. |
| 20 | + * |
| 21 | + * Each case contains only one non-text value type, so each type is verified on its own. |
| 22 | + */ |
| 23 | +const valueTypeCases: ValueTypeCase[] = [ |
| 24 | + { |
| 25 | + type: "date", |
| 26 | + summer: { |
| 27 | + activeTo: "2026-08-31T23:59:59.000Z", |
| 28 | + address: { validUntil: "2027-01-01T00:00:00.000Z" } |
| 29 | + }, |
| 30 | + winter: { |
| 31 | + activeTo: "2027-02-28T23:59:59.000Z", |
| 32 | + address: { validUntil: "2027-06-01T00:00:00.000Z" } |
| 33 | + } |
| 34 | + }, |
| 35 | + { |
| 36 | + type: "integer", |
| 37 | + summer: { |
| 38 | + priority: 1, |
| 39 | + address: { zip: 10000 } |
| 40 | + }, |
| 41 | + winter: { |
| 42 | + priority: 2, |
| 43 | + address: { zip: 20000 } |
| 44 | + } |
| 45 | + }, |
| 46 | + { |
| 47 | + type: "float", |
| 48 | + summer: { |
| 49 | + discount: 0.15, |
| 50 | + address: { latitude: 45.815 } |
| 51 | + }, |
| 52 | + winter: { |
| 53 | + discount: 0.25, |
| 54 | + address: { latitude: 46.305 } |
| 55 | + } |
| 56 | + }, |
| 57 | + { |
| 58 | + type: "boolean", |
| 59 | + summer: { |
| 60 | + active: true, |
| 61 | + address: { verified: false } |
| 62 | + }, |
| 63 | + winter: { |
| 64 | + active: false, |
| 65 | + address: { verified: true } |
| 66 | + } |
| 67 | + }, |
| 68 | + { |
| 69 | + type: "array", |
| 70 | + summer: { |
| 71 | + dates: ["2026-06-01T00:00:00.000Z", "2026-07-01T00:00:00.000Z"], |
| 72 | + integers: [1, 2], |
| 73 | + floats: [1.5, 2.5], |
| 74 | + flags: [true, false], |
| 75 | + periods: [{ from: "2026-06-01T00:00:00.000Z", count: 1, open: true }] |
| 76 | + }, |
| 77 | + winter: { |
| 78 | + dates: ["2026-12-01T00:00:00.000Z", "2027-01-01T00:00:00.000Z"], |
| 79 | + integers: [3, 4], |
| 80 | + floats: [3.5, 4.5], |
| 81 | + flags: [false, true], |
| 82 | + periods: [{ from: "2026-12-01T00:00:00.000Z", count: 2, open: false }] |
| 83 | + } |
| 84 | + }, |
| 85 | + { |
| 86 | + type: "mixed", |
| 87 | + summer: { |
| 88 | + activeTo: "2026-08-31T23:59:59.000Z", |
| 89 | + priority: 1, |
| 90 | + discount: 0.15, |
| 91 | + active: true, |
| 92 | + tags: ["beach", "sun"], |
| 93 | + periods: [{ from: "2026-06-01T00:00:00.000Z", count: 1, open: true }], |
| 94 | + address: { |
| 95 | + validUntil: "2027-01-01T00:00:00.000Z", |
| 96 | + zip: 10000, |
| 97 | + latitude: 45.815, |
| 98 | + verified: false, |
| 99 | + geo: { updatedOn: "2026-01-01T00:00:00.000Z", precision: 3, exact: true } |
| 100 | + } |
| 101 | + }, |
| 102 | + winter: { |
| 103 | + activeTo: "2027-02-28T23:59:59.000Z", |
| 104 | + priority: 2, |
| 105 | + discount: 0.25, |
| 106 | + active: false, |
| 107 | + tags: ["snow", "ski"], |
| 108 | + periods: [{ from: "2026-12-01T00:00:00.000Z", count: 2, open: false }], |
| 109 | + address: { |
| 110 | + validUntil: "2027-06-01T00:00:00.000Z", |
| 111 | + zip: 20000, |
| 112 | + latitude: 46.305, |
| 113 | + verified: true, |
| 114 | + geo: { updatedOn: "2026-02-01T00:00:00.000Z", precision: 5, exact: false } |
| 115 | + } |
| 116 | + } |
| 117 | + } |
| 118 | +]; |
| 119 | + |
| 120 | +describe.each(valueTypeCases)( |
| 121 | + "searchable-json field - full text search with $type values", |
| 122 | + ({ summer, winter }) => { |
| 123 | + let context: CmsContext; |
| 124 | + let model: CmsModel; |
| 125 | + |
| 126 | + beforeEach(async () => { |
| 127 | + const contextHandler = createAuthorWithSearchableJsonContextHandler(); |
| 128 | + context = await contextHandler.handler(); |
| 129 | + |
| 130 | + model = ( |
| 131 | + await context.container |
| 132 | + .resolve(GetModelUseCase) |
| 133 | + .execute(AUTHOR_WITH_SEARCHABLE_JSON_MODEL_ID) |
| 134 | + ).value; |
| 135 | + |
| 136 | + const createEntry = context.container.resolve(CreateEntryUseCase); |
| 137 | + |
| 138 | + await createEntry.execute<IAuthorWithSearchableJsonCmsEntryValues>(model, { |
| 139 | + values: { |
| 140 | + name: "John Doe", |
| 141 | + info: { |
| 142 | + ...summer, |
| 143 | + title: "Summer campaign", |
| 144 | + address: { |
| 145 | + ...summer.address, |
| 146 | + city: "Anytown" |
| 147 | + } |
| 148 | + } |
| 149 | + } |
| 150 | + }); |
| 151 | + await createEntry.execute<IAuthorWithSearchableJsonCmsEntryValues>(model, { |
| 152 | + values: { |
| 153 | + name: "Jacob Doe", |
| 154 | + info: { |
| 155 | + ...winter, |
| 156 | + title: "Winter campaign", |
| 157 | + address: { |
| 158 | + ...winter.address, |
| 159 | + city: "Othertown" |
| 160 | + } |
| 161 | + } |
| 162 | + } |
| 163 | + }); |
| 164 | + }); |
| 165 | + |
| 166 | + const search = async (term: string) => { |
| 167 | + const result = await context.container |
| 168 | + .resolve(ListLatestEntriesUseCase) |
| 169 | + .execute<IAuthorWithSearchableJsonCmsEntryValues>(model, { |
| 170 | + search: term |
| 171 | + }); |
| 172 | + if (result.isFail()) { |
| 173 | + throw new Error(result.error.message); |
| 174 | + } |
| 175 | + return result.value.entries.map(entry => entry.values.name).sort(); |
| 176 | + }; |
| 177 | + |
| 178 | + it("should find entries by a text value in the searchable-json field", async () => { |
| 179 | + expect(await search("Summer")).toEqual(["John Doe"]); |
| 180 | + expect(await search("campaign")).toEqual(["Jacob Doe", "John Doe"]); |
| 181 | + }); |
| 182 | + |
| 183 | + it("should find entries by a nested text value in the searchable-json field", async () => { |
| 184 | + expect(await search("Othertown")).toEqual(["Jacob Doe"]); |
| 185 | + }); |
| 186 | + |
| 187 | + it("should find entries by the title field", async () => { |
| 188 | + expect(await search("Jacob")).toEqual(["Jacob Doe"]); |
| 189 | + }); |
| 190 | + |
| 191 | + it("should find nothing when term does not match", async () => { |
| 192 | + expect(await search("Autumn")).toEqual([]); |
| 193 | + }); |
| 194 | + } |
| 195 | +); |
0 commit comments