aboutsummaryrefslogtreecommitdiff
path: root/castle-search/test/resolve.test.ts
blob: bcd4ef4fd9e4c240fe5d1107950f50e4bcc09a76 (plain)
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
import assert from "node:assert/strict";
import { test } from "node:test";

import { isGoogleGoto, resolveGoogleLinks } from "../src/resolve.ts";
import { getEngine } from "../src/engines.ts";

test("google /goto links are recognised, other google links are not", () => {
  assert.equal(isGoogleGoto("https://www.google.com/goto?url=CAES"), true);
  assert.equal(isGoogleGoto("https://www.google.co.uk/goto?url=CAES"), true);
  assert.equal(isGoogleGoto("https://www.google.com/search?q=x"), false);
  assert.equal(isGoogleGoto("https://example.com/goto?url=x"), false);
});

test("the google self-host filter lets /goto through but still blocks google's own pages", () => {
  const re = new RegExp(getEngine("google").extraction.selfHostPattern);
  assert.equal(re.test("https://www.google.com/goto?url=CAES"), false);
  assert.equal(re.test("https://www.google.com/search?q=x"), true);
  assert.equal(re.test("https://accounts.google.com/ServiceLogin"), false);
});

test("non-google results pass through untouched", async () => {
  const input = [{ title: "t", url: "https://example.com/", snippet: "" }];
  assert.deepEqual(await resolveGoogleLinks(input), input);
});

test("a link that cannot be resolved keeps the page's breadcrumb in its snippet", async () => {
  const wrapped = "https://www.google.com/goto?url=CAES";
  const real = globalThis.fetch;
  // Google's interstitial: 200 with no Location header.
  globalThis.fetch = (async () => new Response("<html>Verifying your request</html>", { status: 200 })) as typeof fetch;
  try {
    const [r] = await resolveGoogleLinks([{ title: "t", url: wrapped, snippet: "s", display: "GitHub — https://github.com › a › b" }]);
    assert.equal(r?.url, wrapped);
    assert.match(r?.snippet ?? "", /^\[link unresolved; Google shows it as: GitHub — https:\/\/github\.com › a › b\] s$/);
  } finally {
    globalThis.fetch = real;
  }
});

test("a link that resolves is replaced and the snippet is left alone", async () => {
  const real = globalThis.fetch;
  globalThis.fetch = (async () => new Response(null, { status: 302, headers: { location: "https://github.com/a/b" } })) as typeof fetch;
  try {
    const [r] = await resolveGoogleLinks([{ title: "t", url: "https://www.google.com/goto?url=CAES", snippet: "s", display: "x" }]);
    assert.equal(r?.url, "https://github.com/a/b");
    assert.equal(r?.snippet, "s");
  } finally {
    globalThis.fetch = real;
  }
});