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

summaryrefslogtreecommitdiffhomepage
path: root/frontend/tests/sites.test.mjs
blob: 331eee8229e441fd6d6564a685bc036e5a4cc547 (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
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
// 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 { test } from "node:test";
import { contentRoutes, sidebarFor } from "../src/content/routes.ts";
import { supportedLocales } from "../src/locales.ts";
import { pageMetadata } from "../src/seo.ts";
import { siteHome, siteHosts, siteOf, siteTopics, siteUrl } from "../src/site.ts";

function memoryStorage() {
  const values = new Map();
  return {
    getItem: (key) => values.get(key) ?? null,
    setItem: (key, value) => values.set(key, value),
  };
}

test("a host name selects its site and anything else is the language site", () => {
  assert.equal(siteOf("tala.run", ""), "language");
  assert.equal(siteOf("talac.tala.run", ""), "talac");
  assert.equal(siteOf("tazel.tala.run", ""), "tazel");
  assert.equal(siteOf("talafmt.tala.run", ""), "talafmt");
  assert.equal(siteOf("talalint.tala.run", ""), "talalint");
  assert.equal(siteOf("localhost", "?site=talalint"), "talalint");
  assert.equal(siteOf("www.tala.run", ""), "language");
  // A query string selects a site only on a development host.
  assert.equal(siteOf("tala.run", "?site=talac"), "language");
  assert.equal(siteOf("talac.example.com", ""), "language");
});

test("a development host keeps the site it was asked for in the tab", () => {
  const storage = memoryStorage();
  assert.equal(siteOf("localhost", "?site=tazel", storage), "tazel");
  assert.equal(siteOf("localhost", "", storage), "tazel");
  assert.equal(siteOf("127.0.0.1", "?site=language", storage), "language");
  assert.equal(siteOf("localhost", "?site=nonsense", memoryStorage()), "language");
  const broken = {
    getItem() {
      throw new Error("storage is blocked");
    },
    setItem() {
      throw new Error("storage is blocked");
    },
  };
  assert.equal(siteOf("localhost", "?site=talac", broken), "talac");
  assert.equal(siteOf("localhost", "", broken), "language");
});

test("links to another site are absolute in production and stay on a development host", () => {
  assert.equal(
    siteUrl("talac", "/docs/outputs", "tala.run"),
    "https://talac.tala.run/docs/outputs",
  );
  assert.equal(siteUrl("language", "/", "tazel.tala.run"), "https://tala.run/");
  assert.equal(siteUrl("tazel", "/docs/commands", "localhost"), "/docs/commands?site=tazel");
});

test("every topic a sidebar lists is a page of its site", () => {
  for (const site of ["language", "talac", "tazel", "talafmt", "talalint"]) {
    const routes = contentRoutes[site].map((route) => route.path);
    for (const path of routes)
      assert.ok(siteTopics[site].includes(path), `${site} does not list ${path}`);
    for (const locale of supportedLocales) {
      const sidebar = sidebarFor(site, locale);
      assert.deepEqual(
        sidebar.map((entry) => entry.path),
        [...siteTopics[site]],
      );
      assert.ok(sidebar.every((entry) => entry.label.length > 0 && !entry.label.startsWith("/")));
    }
  }
  // Every site opens on its own landing page rather than on a documentation topic.
  assert.deepEqual(siteHome, {
    language: "/",
    talac: "/",
    tazel: "/",
    talafmt: "/",
    talalint: "/",
  });
});

test("every topic is written in every language with the same code in each", () => {
  for (const site of ["language", "talac", "tazel", "talafmt", "talalint"]) {
    for (const route of contentRoutes[site]) {
      const reference = route.topic["en-US"];
      const codes = (page) =>
        page.blocks.filter((block) => block.kind === "code").map((block) => block.code);
      const terms = (page) =>
        page.blocks
          .filter((block) => block.kind === "terms")
          .flatMap((block) => block.items.map((item) => item.term));
      for (const locale of supportedLocales) {
        const page = route.topic[locale];
        for (const field of ["nav", "title", "description", "section", "heading", "lead"]) {
          assert.ok(page[field].trim().length > 0, `${route.path} ${locale} has no ${field}`);
        }
        assert.equal(
          page.blocks.length,
          reference.blocks.length,
          `${route.path} ${locale} lost a block`,
        );
        assert.deepEqual(
          codes(page),
          codes(reference),
          `${route.path} ${locale} changed a code sample`,
        );
        assert.deepEqual(
          terms(page),
          terms(reference),
          `${route.path} ${locale} changed an option name`,
        );
        if (locale !== "en-US") {
          assert.notEqual(page.lead, reference.lead, `${route.path} ${locale} is not translated`);
        }
      }
    }
  }
});

test("the documentation teaches the current command lines only", () => {
  const all = Object.values(contentRoutes)
    .flat()
    .flatMap((route) => Object.values(route.topic))
    .map((page) => JSON.stringify(page))
    .join("\n");
  for (const retired of [
    /-File\b/,
    /-Emit\b/,
    /-Help\b/,
    /\.vxse\b/,
    /\btadc\b/,
    /talac (?:check|build|run)\b/,
  ]) {
    assert.doesNotMatch(all, retired);
  }
  assert.match(all, /--emit=metadata/);
  assert.match(all, /tazel lint -- --fix/);
});

test("each site names its pages and points canonical URLs at its own host", () => {
  const talac = pageMetadata("/docs/outputs", "de-DE", "talac");
  assert.equal(talac.title, "Ausgaben · Talac");
  assert.equal(talac.canonicalPath, "/docs/outputs");
  const tazel = pageMetadata("/", "en-US", "tazel");
  assert.equal(tazel.canonicalPath, "/");
  assert.equal(tazel.title, "Tazel — The build system of Tala");
  assert.equal(pageMetadata("/docs/syntax", "ru-RU", "language").title, "Синтаксис · Tala");
  assert.equal(pageMetadata("/docs/outputs", "en-US", "language").canonicalPath, "/");
  assert.equal(pageMetadata("/docs/layout", "en-US", "talafmt").title, "What it changes · Talafmt");
  assert.equal(pageMetadata("/docs/checks", "de-DE", "talalint").title, "Was es prüft · Talalint");
  assert.equal(pageMetadata("/docs/layout", "en-US", "talalint").canonicalPath, "/");
  assert.deepEqual(siteHosts, {
    language: "tala.run",
    talac: "talac.tala.run",
    tazel: "tazel.tala.run",
    talafmt: "talafmt.tala.run",
    talalint: "talalint.tala.run",
  });
});