-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsearch-dsl.test.ts
More file actions
267 lines (235 loc) · 9.03 KB
/
Copy pathsearch-dsl.test.ts
File metadata and controls
267 lines (235 loc) · 9.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
import { describe, expect, it } from "bun:test";
import * as fc from "fast-check";
import {
composeQuery,
hasStructured,
parseQuery,
Q_TOTAL_MAX,
type StructuredQuery,
sameQuery,
} from "./search-dsl.ts";
// Broad arbitrary: allows colons, embedded quotes (which composeQuery strips),
// and surrounding whitespace (which composeQuery trims). `\` is kept out
// because the underlying search-parser already escapes it, and `\n` because
// SQLite FTS5 phrase tokenisation drops newlines anyway.
const fieldValueArb = fc.string({ minLength: 0, maxLength: 32 }).filter((s) => !/[\\\n\r]/.test(s));
const structuredArb: fc.Arbitrary<StructuredQuery> = fc.record({
title: fieldValueArb,
company: fieldValueArb,
location: fieldValueArb,
freeText: fieldValueArb,
});
/**
* Mirror the cleanup `composeQuery` performs so the property test asserts
* the right invariant (round-trip yields the *normalised* form).
*/
function normaliseField(v: string): string {
return v.replace(/"/g, "").trim();
}
function normaliseFreeText(v: string): string {
return v.replace(/"/g, "").trim();
}
describe("parseQuery", () => {
it("returns all-empty for empty input", () => {
expect(parseQuery("")).toEqual({ title: "", company: "", location: "", freeText: "" });
});
it("extracts a single bare-word free-text term", () => {
expect(parseQuery("engineer").freeText).toBe("engineer");
});
it("extracts title:value", () => {
expect(parseQuery("title:engineer").title).toBe("engineer");
});
it("extracts company:stripe", () => {
expect(parseQuery("company:stripe").company).toBe("stripe");
});
it("extracts location:remote", () => {
expect(parseQuery("location:remote").location).toBe("remote");
});
it("extracts a quoted multi-word value", () => {
const out = parseQuery('title:"senior engineer"');
expect(out.title).toBe("senior engineer");
});
it("preserves a quoted phrase in free-text", () => {
const out = parseQuery('"senior engineer"');
expect(out.freeText).toBe("senior engineer");
});
it("treats unknown field prefix as a free-text token (literal `xyz:foo`)", () => {
const out = parseQuery("xyz:engineer");
expect(out.freeText).toBe("xyz:engineer");
});
it("handles a mixed query with structured + free-text remainder", () => {
const out = parseQuery("title:engineer staff at company:stripe");
expect(out.title).toBe("engineer");
expect(out.company).toBe("stripe");
expect(out.freeText).toBe("staff at");
expect(out.location).toBe("");
});
it("concatenates duplicate field tokens with a single space", () => {
const out = parseQuery("title:senior title:engineer");
expect(out.title).toBe("senior engineer");
});
});
describe("composeQuery", () => {
it("returns empty string for an empty StructuredQuery", () => {
expect(composeQuery({ title: "", company: "", location: "", freeText: "" })).toBe("");
});
it("always quotes field tokens (round-trip safety)", () => {
expect(composeQuery({ title: "engineer", company: "", location: "", freeText: "" })).toBe(
'title:"engineer"',
);
expect(
composeQuery({ title: "senior engineer", company: "", location: "", freeText: "" }),
).toBe('title:"senior engineer"');
});
it("emits structured tokens before the free-text remainder", () => {
expect(
composeQuery({
title: "engineer",
company: "",
location: "remote",
freeText: "fulltime",
}),
).toBe('title:"engineer" location:"remote" fulltime');
});
it("throws when the composed length exceeds Q_TOTAL_MAX", () => {
const long = "x".repeat(Q_TOTAL_MAX);
expect(() => composeQuery({ title: long, company: "", location: "", freeText: long })).toThrow(
/exceeds 256/,
);
});
});
describe("round-trip (property)", () => {
it("parseQuery yields the normalised form for any StructuredQuery value", () => {
fc.assert(
fc.property(structuredArb, (s) => {
const composed = composeQuery(s);
const reparsed = parseQuery(composed);
expect(reparsed.title).toBe(normaliseField(s.title));
expect(reparsed.company).toBe(normaliseField(s.company));
expect(reparsed.location).toBe(normaliseField(s.location));
expect(reparsed.freeText).toBe(normaliseFreeText(s.freeText));
}),
);
});
it("composeQuery output never exceeds Q_TOTAL_MAX when each field is short", () => {
const short = fc.string({ maxLength: 20 }).filter((s) => !/[\\\n\r]/.test(s));
fc.assert(
fc.property(
fc.record({ title: short, company: short, location: short, freeText: short }),
(s) => {
const composed = composeQuery(s);
expect(composed.length).toBeLessThanOrEqual(Q_TOTAL_MAX);
},
),
);
});
it("freeText that looks like field:value is quoted so the parser does not re-bucket", () => {
const composed = composeQuery({
title: "",
company: "",
location: "",
freeText: "title:hijack",
});
expect(composed).toBe('"title:hijack"');
const reparsed = parseQuery(composed);
expect(reparsed.title).toBe("");
expect(reparsed.freeText).toBe("title:hijack");
});
it("strips embedded double-quotes from field values (lossy by design)", () => {
const composed = composeQuery({
title: 'sen"ior',
company: "",
location: "",
freeText: "",
});
expect(composed).toBe('title:"senior"');
const reparsed = parseQuery(composed);
expect(reparsed.title).toBe("senior");
});
it("trims whitespace inside field values (round-trip yields trimmed value)", () => {
const composed = composeQuery({
title: " engineer ",
company: "",
location: "",
freeText: "",
});
const reparsed = parseQuery(composed);
expect(reparsed.title).toBe("engineer");
});
});
describe("hasStructured", () => {
it("is false when no structured field is set", () => {
expect(hasStructured({ title: "", company: "", location: "", freeText: "freeonly" })).toBe(
false,
);
});
it("is true when any structured field is non-empty", () => {
expect(hasStructured({ title: "x", company: "", location: "", freeText: "" })).toBe(true);
expect(hasStructured({ title: "", company: "x", location: "", freeText: "" })).toBe(true);
expect(hasStructured({ title: "", company: "", location: "x", freeText: "" })).toBe(true);
});
});
describe("sameQuery", () => {
const empty: StructuredQuery = { title: "", company: "", location: "", freeText: "" };
it("is true for identical queries", () => {
expect(sameQuery(empty, empty)).toBe(true);
const q: StructuredQuery = {
title: "engineer",
company: "stripe",
location: "berlin",
freeText: "rust",
};
expect(sameQuery(q, { ...q })).toBe(true);
});
it("is true for canonical-no-op round-trips (the regression this fixes)", () => {
// User types `company:stripe title:engineer` — composer emits
// `title:"engineer" company:"stripe"` (canonical field order +
// quoted values). The composed string differs from the input,
// but the PARSED forms are identical.
const userTyped = "company:stripe title:engineer";
const canonical = composeQuery(parseQuery(userTyped));
expect(canonical).not.toBe(userTyped); // composer reordered + quoted
expect(sameQuery(parseQuery(canonical), parseQuery(userTyped))).toBe(true);
});
it("is true after parse→compose→parse round-trip for any input", () => {
fc.assert(
fc.property(
fc.record({
title: fc.string({ minLength: 0, maxLength: 30 }).map((s) => s.replace(/"/g, "")),
company: fc.string({ minLength: 0, maxLength: 30 }).map((s) => s.replace(/"/g, "")),
location: fc.string({ minLength: 0, maxLength: 30 }).map((s) => s.replace(/"/g, "")),
freeText: fc.string({ minLength: 0, maxLength: 30 }).map((s) => s.replace(/"/g, "")),
}),
(raw) => {
// Filter to keep composed length under cap
const trimmed: StructuredQuery = {
title: raw.title.trim().slice(0, 20),
company: raw.company.trim().slice(0, 20),
location: raw.location.trim().slice(0, 20),
freeText: raw.freeText.trim().slice(0, 20),
};
let composed: string;
try {
composed = composeQuery(trimmed);
} catch {
return true; // skip overflows
}
return sameQuery(parseQuery(composed), trimmed);
},
),
{ numRuns: 100 },
);
});
it("is false when title differs", () => {
expect(sameQuery({ ...empty, title: "a" }, { ...empty, title: "b" })).toBe(false);
});
it("is false when company differs", () => {
expect(sameQuery({ ...empty, company: "a" }, { ...empty, company: "b" })).toBe(false);
});
it("is false when location differs", () => {
expect(sameQuery({ ...empty, location: "a" }, { ...empty, location: "b" })).toBe(false);
});
it("is false when freeText differs", () => {
expect(sameQuery({ ...empty, freeText: "a" }, { ...empty, freeText: "b" })).toBe(false);
});
});