iterate.com / config-template

git clone https://git.iterate.com/repos/config-template

config-template / tests / profile.test.mjsmain at 3b8ad1b

// tests/profile.test.mjs — the mind's public profile (../profile.ts): what the owner's two files
// become on a page and in the profile's answer, and what a missing or broken file falls back to.
// How the website answers the profile is in worker.test.mjs.
// Run it from the repo's root with `node --test "tests/*.test.mjs"`.
import assert from "node:assert/strict";
import { test } from "node:test";
import { profileHtml, profileOf, serveProfile } from "../profile.ts";

const PROFILE_PATH = "/.well-known/iterate-profile";
/** The answer for a folder of the owner's. */
const askOf = (files, path) => serveProfile(new Request(`https://example.com${path}`), files);

test("on a page, the name is the heading and the description is under it, both as text", () => {
  const hostile = profileOf(
    {
      "profile.json": JSON.stringify({
        name: 'Ada <script>alert(1)</script> & "co"',
        description: "First line <b>bold</b>\nSecond & last",
      }),
      "icon.svg": "<svg/>",
    },
    "example.com",
  );
  assert.deepEqual(profileHtml(hostile), {
    head: [
      '<title>Ada &lt;script>alert(1)&lt;/script> &amp; "co"</title>',
      '<link rel="icon" href="/favicon.svg" type="image/svg+xml">',
    ],
    body: [
      '<img src="/favicon.svg" alt="" width="64" height="64" style="border-radius:14px">',
      '<h1>Ada &lt;script>alert(1)&lt;/script> &amp; "co"</h1>',
      '<p style="white-space:pre-line">First line &lt;b>bold&lt;/b>\nSecond &amp; last</p>',
    ],
  });
  // no icon and no description: the name alone
  assert.deepEqual(profileHtml(profileOf({}, "example.com")), {
    head: ["<title>example.com</title>"],
    body: ["<h1>example.com</h1>"],
  });
});

test("the owner's words are served trimmed, and no other key of the file is", async () => {
  const files = {
    "profile.json": JSON.stringify({
      name: "  Ada's mind ",
      description: " Thinks about engines.\nWrites notes. ",
      icon: "https://elsewhere.example/x.svg",
      apiKey: "not for the world",
    }),
    "icon.svg": '<svg xmlns="http://www.w3.org/2000/svg"/>',
    "notes.txt": "the owner's own file",
  };
  const said = {
    name: "Ada's mind",
    description: "Thinks about engines.\nWrites notes.",
    icon: "https://example.com/favicon.svg",
  };
  assert.deepEqual(await askOf(files, PROFILE_PATH).json(), said);
  assert.equal(await askOf(files, "/favicon.svg").text(), files["icon.svg"]);
  assert.equal(askOf(files, "/notes.txt"), null);
  assert.deepEqual(profileOf(files, "example.com"), { ...said, problem: undefined });
});

for (const [name, owners, problem] of [
  ["no profile.json", {}, undefined],
  ["an empty profile.json", { "profile.json": "" }, undefined],
  ["text that is no JSON", { "profile.json": "{ name: Ada" }, /^profile\/profile\.json is not valid JSON\.$/],
  [
    "a file that is no text",
    { "profile.json": new Uint8Array([255, 254]) },
    /^profile\/profile\.json is not text\.$/,
  ],
  [
    "no object",
    { "profile.json": JSON.stringify("Ada") },
    /^profile\/profile\.json: the file is not what a profile holds/,
  ],
  [
    "a name of two lines",
    { "profile.json": JSON.stringify({ name: "Ada\nLovelace" }) },
    /^profile\/profile\.json: name is not what a profile holds/,
  ],
  [
    "a name of 81 characters",
    { "profile.json": JSON.stringify({ name: "a".repeat(81) }) },
    /^profile\/profile\.json: name is not/,
  ],
  [
    "a description with a tab",
    { "profile.json": JSON.stringify({ description: "a\tb" }) },
    /^profile\/profile\.json: description is not/,
  ],
  [
    "a description of 1001 characters",
    { "profile.json": JSON.stringify({ description: "a".repeat(1001) }) },
    /^profile\/profile\.json: description is not/,
  ],
])
  test(`${name}: the name is the zone and the description is empty`, async () => {
    const files = { ...owners, "icon.svg": "<svg/>" };
    const fallback = {
      name: "example.com",
      description: "",
      icon: "https://example.com/favicon.svg",
    };
    assert.deepEqual(await askOf(files, PROFILE_PATH).json(), fallback);
    const { problem: said, ...rest } = profileOf(files, "example.com");
    assert.deepEqual(rest, fallback);
    if (problem) assert.match(said, problem);
    else assert.equal(said, undefined);
    // what an agent's script returns stays short
    assert.ok(JSON.stringify({ ...rest, problem: said }).length < 400);
  });

test("a mind with no icon, one that is no text or one over the bound has none: the icon's paths are not found", async () => {
  const none = { "profile.json": JSON.stringify({ name: "Ada" }) };
  const large = { ...none, "icon.svg": `<svg>${"a".repeat(256 * 1024)}</svg>` };
  const bytes = { ...none, "icon.svg": new Uint8Array([255, 254, 60, 0]) };
  for (const files of [none, large, bytes]) {
    assert.deepEqual(await askOf(files, PROFILE_PATH).json(), {
      name: "Ada",
      description: "",
      icon: null,
    });
    assert.equal(askOf(files, "/favicon.svg").status, 404);
    assert.equal(askOf(files, "/favicon.ico").status, 404);
  }
  assert.equal(profileOf(none, "example.com").problem, undefined);
  assert.equal(
    profileOf(bytes, "example.com").problem,
    "profile/icon.svg is not text and is not served.",
  );
  assert.equal(
    profileOf(large, "example.com").problem,
    "profile/icon.svg is over 262144 characters and is not served.",
  );
});