Read-only mirrors. Development, issues and pull requests are on GitHub.

summaryrefslogtreecommitdiffhomepage
path: root/frontend/tests/seo.test.mjs
blob: 0ca9d211fcce192c9b975f97f6577049a90ea3da (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
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
// SPDX-FileCopyrightText: 2026 Progmasoft <support@progmasoft.com>
// SPDX-License-Identifier: AGPL-3.0-or-later WITH AdditionRef-Progmasoft-Patent-Grant-1.1

import assert from "node:assert/strict";
import { afterEach, test } from "node:test";
import { applyPageMetadata } from "../src/seo.ts";

const originalDocument = globalThis.document;

afterEach(() => {
  globalThis.document = originalDocument;
});

function fakeDocument() {
  const elements = new Map([
    ['meta[name="description"]', { content: "" }],
    ['meta[property="og:title"]', { content: "" }],
    ['meta[property="og:description"]', { content: "" }],
    ['meta[property="og:url"]', { content: "" }],
    ['link[rel="canonical"]', { href: "" }],
  ]);
  const document = {
    title: "",
    querySelector(selector) {
      return elements.get(selector) ?? null;
    },
  };
  globalThis.document = document;
  return { document, elements };
}

test("the English home page has a canonical URL and matching social metadata", () => {
  const { document, elements } = fakeDocument();
  applyPageMetadata("/", "en-US");
  assert.match(document.title, /^Tala/);
  assert.equal(elements.get('meta[property="og:title"]').content, document.title);
  assert.match(elements.get('meta[name="description"]').content, /native programming language/);
  assert.equal(elements.get('meta[property="og:url"]').content, "https://tala.run/");
  assert.equal(elements.get('link[rel="canonical"]').href, "https://tala.run/");
});

test("the overview route uses its own German title and description", () => {
  const { document, elements } = fakeDocument();
  applyPageMetadata("/docs/overview", "de-DE");
  assert.equal(document.title, "Überblick · Tala");
  assert.match(elements.get('meta[name="description"]').content, /Ownership-Regeln/);
  assert.equal(elements.get('link[rel="canonical"]').href, "https://tala.run/docs/overview");
});

test("the getting-started route does not inherit the overview metadata", () => {
  const { document, elements } = fakeDocument();
  applyPageMetadata("/docs/overview", "en-US");
  applyPageMetadata("/docs/getting-started", "en-US");
  assert.equal(document.title, "Getting started · Tala");
  assert.match(elements.get('meta[name="description"]').content, /toolchain/);
  assert.equal(
    elements.get('meta[property="og:url"]').content,
    "https://tala.run/docs/getting-started",
  );
});

test("unknown routes never become public canonical URLs", () => {
  const { elements } = fakeDocument();
  applyPageMetadata("/old/login", "en-US");
  assert.equal(elements.get('link[rel="canonical"]').href, "https://tala.run/");
});

test("a missing optional metadata tag does not stop the page title", () => {
  globalThis.document = {
    title: "",
    querySelector() {
      return null;
    },
  };
  applyPageMetadata("/docs/getting-started", "de-DE");
  assert.equal(globalThis.document.title, "Erste Schritte · Tala");
});

test("the Russian routes have their own titles and keep the canonical URLs", () => {
  const { document, elements } = fakeDocument();
  applyPageMetadata("/", "ru-RU");
  assert.match(document.title, /^Tala — Сделать понятной всю программу$/);
  assert.match(elements.get('meta[name="description"]').content, /нативный язык программирования/);
  assert.equal(elements.get('link[rel="canonical"]').href, "https://tala.run/");
  applyPageMetadata("/docs/overview", "ru-RU");
  assert.equal(document.title, "Обзор · Tala");
  applyPageMetadata("/docs/getting-started", "ru-RU");
  assert.equal(document.title, "Начало работы · Tala");
  assert.equal(elements.get('link[rel="canonical"]').href, "https://tala.run/docs/getting-started");
});

test("the Hebrew routes have their own titles and keep the canonical URLs", () => {
  const { document, elements } = fakeDocument();
  applyPageMetadata("/", "he-IL");
  assert.match(document.title, /^Tala — להפוך את התוכנית כולה למובנת$/);
  assert.match(elements.get('meta[name="description"]').content, /שמהודרת לקוד מכונה/);
  assert.equal(elements.get('link[rel="canonical"]').href, "https://tala.run/");
  applyPageMetadata("/docs/overview", "he-IL");
  assert.equal(document.title, "סקירה · Tala");
  applyPageMetadata("/docs/getting-started", "he-IL");
  assert.equal(document.title, "צעדים ראשונים · Tala");
  assert.equal(elements.get('link[rel="canonical"]').href, "https://tala.run/docs/getting-started");
});