Spaces:
Running
Running
File size: 7,934 Bytes
125276b | 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 | import { describe, expect, it } from "vitest";
import { normalizeRelativeDates } from "./relativeDates";
/** 3 November 2026, midday. Every expectation below is relative to this. */
const AS_OF = new Date(2026, 10, 3, 12);
/** 15 December 2026, the fixture for month and year rollover. */
const AS_OF_DECEMBER = new Date(2026, 11, 15, 12);
describe("normalizeRelativeDates", () => {
describe("appended anchors", () => {
it("appends the resolved year to a query with a Chinese year word", () => {
expect(
normalizeRelativeDates("明年的wrestlemania在哪里举办", AS_OF),
).toEqual({
searchQuery: "明年的wrestlemania在哪里举办 2027年",
replacements: [{ phrase: "明年", anchor: "2027年" }],
});
});
it("appends an English month name for next month", () => {
expect(
normalizeRelativeDates("new series on disney plus next month", AS_OF),
).toEqual({
searchQuery: "new series on disney plus next month December 2026",
replacements: [{ phrase: "next month", anchor: "December 2026" }],
});
});
it("leaves a query with no date word untouched", () => {
expect(
normalizeRelativeDates("the result of most recent ufc event", AS_OF),
).toEqual({
searchQuery: "the result of most recent ufc event",
replacements: [],
});
});
it("treats recent-style words as no-ops", () => {
for (const query of [
"最近 ufc",
"最新的比赛结果",
"近期赛事",
"latest ufc event",
"recent ufc event",
]) {
expect(normalizeRelativeDates(query, AS_OF)).toEqual({
searchQuery: query,
replacements: [],
});
}
});
it("matches English triggers case-insensitively", () => {
expect(normalizeRelativeDates("Next Year", AS_OF)).toEqual({
searchQuery: "Next Year 2027",
replacements: [{ phrase: "Next Year", anchor: "2027" }],
});
});
it("appends both a Chinese and an English anchor in one query", () => {
expect(normalizeRelativeDates("明年 and next month", AS_OF)).toEqual({
searchQuery: "明年 and next month 2027年 December 2026",
replacements: [
{ phrase: "明年", anchor: "2027年" },
{ phrase: "next month", anchor: "December 2026" },
],
});
});
it("rolls a December month word into January of the next year", () => {
expect(normalizeRelativeDates("下个月", AS_OF_DECEMBER)).toEqual({
searchQuery: "下个月 2027年1月",
replacements: [{ phrase: "下个月", anchor: "2027年1月" }],
});
});
it("keeps at most two anchors, in the order they appear", () => {
expect(
normalizeRelativeDates("明年的活动 今年的活动 去年的活动", AS_OF),
).toEqual({
searchQuery: "明年的活动 今年的活动 去年的活动 2027年 2026年",
replacements: [
{ phrase: "明年", anchor: "2027年" },
{ phrase: "今年", anchor: "2026年" },
],
});
});
it("does not repeat a year the query already spells out", () => {
const result = normalizeRelativeDates(
"2027年的wrestlemania 明年在哪",
AS_OF,
);
expect(result.replacements).toEqual([]);
expect(result.searchQuery).toBe("2027年的wrestlemania 明年在哪");
expect(result.searchQuery.match(/2027年/g)).toHaveLength(1);
});
it("collapses two phrases that resolve to the same anchor", () => {
expect(normalizeRelativeDates("这个月 和 本月 都下雨", AS_OF)).toEqual({
searchQuery: "这个月 和 本月 都下雨 2026年11月",
replacements: [{ phrase: "这个月", anchor: "2026年11月" }],
});
});
it("appends a trailing anchor without doubling the separator", () => {
expect(normalizeRelativeDates("下个月几号 ", AS_OF).searchQuery).toBe(
"下个月几号 2026年12月",
);
});
it("leaves an empty query alone", () => {
for (const query of ["", " "]) {
expect(normalizeRelativeDates(query, AS_OF)).toEqual({
searchQuery: query,
replacements: [],
});
}
});
});
describe("blacklist", () => {
it("skips a word that belongs to a longer title", () => {
for (const query of [
"明年今日 电影",
"去年冬天",
"去年夏天",
"人类第一次登上月球是哪一年",
"last month of pregnancy symptoms",
"last year of college",
]) {
expect(normalizeRelativeDates(query, AS_OF)).toEqual({
searchQuery: query,
replacements: [],
});
}
});
it("skips the possessive headline form", () => {
expect(normalizeRelativeDates("next year's champion", AS_OF)).toEqual({
searchQuery: "next year's champion",
replacements: [],
});
});
it("leaves day words alone", () => {
for (const query of [
"明天几点",
"昨天的比赛",
"今日头条",
"高铁票提前天数",
"what happened yesterday",
"today show hosts",
"the day after tomorrow cast",
]) {
expect(normalizeRelativeDates(query, AS_OF)).toEqual({
searchQuery: query,
replacements: [],
});
}
});
});
describe("anchor table", () => {
it("resolves the Chinese year words", () => {
const anchors = (query: string) =>
normalizeRelativeDates(query, AS_OF).replacements.map(
(replacement) => replacement.anchor,
);
expect(anchors("去年")).toEqual(["2025年"]);
});
it("resolves the Chinese month words", () => {
const anchors = (query: string) =>
normalizeRelativeDates(query, AS_OF).replacements.map(
(replacement) => replacement.anchor,
);
expect(anchors("上个月")).toEqual(["2026年10月"]);
expect(anchors("上月")).toEqual(["2026年10月"]);
expect(anchors("本月")).toEqual(["2026年11月"]);
expect(anchors("这个月")).toEqual(["2026年11月"]);
});
it("resolves the English year words", () => {
expect(normalizeRelativeDates("this year", AS_OF).replacements).toEqual([
{ phrase: "this year", anchor: "2026" },
]);
expect(normalizeRelativeDates("last year", AS_OF).replacements).toEqual([
{ phrase: "last year", anchor: "2025" },
]);
});
it("rolls last month across a year boundary", () => {
expect(
normalizeRelativeDates("last month", new Date(2026, 0, 15, 12))
.replacements,
).toEqual([{ phrase: "last month", anchor: "December 2025" }]);
});
});
describe("English word boundaries", () => {
it("does not match a trigger inside a longer word", () => {
for (const query of [
"this yearly report",
"next monthly plan",
"last years",
]) {
expect(normalizeRelativeDates(query, AS_OF)).toEqual({
searchQuery: query,
replacements: [],
});
}
});
});
it("drops an anchor that would push the query past the limit", () => {
const long = `${"x".repeat(1990)} next year`;
expect(
normalizeRelativeDates(long, new Date(2026, 10, 3), { maxLength: 2000 }),
).toEqual({
searchQuery: long,
replacements: [],
});
});
it("keeps the anchors that still fit", () => {
const long = `${"x".repeat(1970)} next year next month`;
const { searchQuery } = normalizeRelativeDates(
long,
new Date(2026, 10, 3),
{
maxLength: 2000,
},
);
expect(searchQuery.endsWith("2027")).toBe(true);
expect(searchQuery).not.toContain("December 2026");
expect(searchQuery.length).toBeLessThanOrEqual(2000);
});
});
|