Spaces:
Running
Running
File size: 5,681 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 | import { afterEach, describe, expect, it, vi } from "vitest";
import {
isOfficialLaneEnabled,
MAX_OFFICIAL_LANE_RESULTS,
mergeOfficialLaneResults,
officialLaneRequest,
} from "./officialLane.ts";
type TextResult = [title: string, content: string, url: string];
const results = (...urls: string[]): TextResult[] =>
urls.map((url) => ["title", "snippet", url]);
describe("officialLaneRequest", () => {
it("asks for the named site's official page", () => {
expect(
officialLaneRequest(
"disney ไธไธชๆๆ็",
results("https://news.example.com/a", "https://www.disney.com/movies"),
),
).toEqual({
domain: "disney.com",
query: "site:disney.com disney ไธไธชๆๆ็",
});
});
it("treats a subdomain as the same site", () => {
expect(
officialLaneRequest(
"disney official parking hours",
results("https://investor.disney.com/reports"),
)?.domain,
).toBe("disney.com");
});
it("recognises a label spelled by the query's own words", () => {
expect(
officialLaneRequest(
"disney plus showtimes",
results("https://www.disneyplus.com/welcome"),
)?.domain,
).toBe("disneyplus.com");
});
it("reads the English intent words on their own", () => {
for (const query of [
"disney hours",
"disney opening times",
"disney tickets",
"disney pricing",
"disney schedule",
"disney showtimes",
"disney timetable",
]) {
expect(
officialLaneRequest(query, results("https://disney.com/"))?.domain,
query,
).toBe("disney.com");
}
});
it("needs navigation intent, not just a named site", () => {
expect(
officialLaneRequest(
"disney movie reviews",
results("https://disney.com/"),
),
).toBeNull();
});
it("needs a result that already names the site", () => {
expect(
officialLaneRequest(
"disney showtimes",
results(
"https://www.fandango.com/disney",
"https://www.imdb.com/title",
),
),
).toBeNull();
});
it("ignores a result whose label is not the query's word", () => {
expect(
officialLaneRequest(
"disney showtimes",
results("https://disneyfan.com/showtimes"),
),
).toBeNull();
});
it("never scopes a host with no registrable domain", () => {
for (const url of [
"http://127.0.0.1:8888/search?q=disney",
"http://localhost:5173/",
"not a url",
]) {
expect(
officialLaneRequest("disney showtimes", results(url)),
url,
).toBeNull();
}
});
it("never scopes a whole country suffix or a shared host", () => {
for (const url of [
"https://shop.example.com.hk/x",
"https://someone.github.io/x",
]) {
expect(
officialLaneRequest("amazon.com github hours", results(url)),
url,
).toBeNull();
}
});
it("does not scope a query too long to carry the prefix", () => {
const query = `disney showtimes ${"x".repeat(2000)}`;
expect(
officialLaneRequest(query, results("https://disney.com/")),
).toBeNull();
});
});
describe("mergeOfficialLaneResults", () => {
const main = results(
"https://aggregator.example.com/disney",
"https://news.example.com/disney",
);
it("places the lane's pages behind the pinned first result", () => {
expect(
mergeOfficialLaneResults(
main,
results("https://disney.com/schedule"),
"disney.com",
),
).toEqual([
main[0],
["title", "snippet", "https://disney.com/schedule"],
main[1],
]);
});
it("leaves a page the main set already has alone", () => {
expect(
mergeOfficialLaneResults(main, results(main[0][2]), "disney.com"),
).toBe(main);
});
it("reads the scheme and www spellings of a page as one page", () => {
const withOfficialPage = results(
"https://www.disney.com/schedule",
"https://news.example.com/disney",
);
expect(
mergeOfficialLaneResults(
withOfficialPage,
results("http://disney.com/schedule/", "https://disney.com/schedule"),
"disney.com",
),
).toBe(withOfficialPage);
});
it("drops a host the engine returned outside the scope", () => {
expect(
mergeOfficialLaneResults(
main,
results("https://fandango.com/x"),
"disney.com",
),
).toBe(main);
});
it(`adds at most ${MAX_OFFICIAL_LANE_RESULTS} pages`, () => {
const laneResults = results(
"https://disney.com/1",
"https://disney.com/2",
"https://disney.com/3",
"https://disney.com/4",
);
const merged = mergeOfficialLaneResults(main, laneResults, "disney.com");
expect(merged.length).toBe(main.length + MAX_OFFICIAL_LANE_RESULTS);
});
it("changes nothing when the main set is empty", () => {
expect(
mergeOfficialLaneResults(
[],
results("https://disney.com/"),
"disney.com",
),
).toEqual([]);
});
});
describe("isOfficialLaneEnabled", () => {
afterEach(() => {
vi.unstubAllEnvs();
});
it("is off unless the environment turns it on", () => {
for (const value of ["", "false", "0", "off", "yes"]) {
vi.stubEnv("SEARCH_OFFICIAL_LANE_ENABLED", value);
expect(isOfficialLaneEnabled(), value).toBe(false);
}
});
it("accepts 'true', ' TRUE ' and '1'", () => {
for (const value of ["true", " TRUE ", "1"]) {
vi.stubEnv("SEARCH_OFFICIAL_LANE_ENABLED", value);
expect(isOfficialLaneEnabled(), value).toBe(true);
}
});
});
|