Skip to content

Lightweight native Discord client for Windows, Linux and macOS, written in Rust. No Electron, no browser engine.

3.3 KB
Raw Blame
// node --test server/test.mjs

import assert from "node:assert/strict";
import { test } from "node:test";
import { handle, memoryStore, MAX_LOOKUP } from "./registry.mjs";

const A = "a".repeat(64);
const B = "b".repeat(64);
const C = "c".repeat(64);
const post = (path, body, type = "application/json") =>
  new Request(`http://x${path}`, { method: "POST", headers: type ? { "content-type": type } : {}, body: typeof body === "string" ? body : JSON.stringify(body) });
const get = (path) => new Request(`http://x${path}`);
const call = async (store, now, path, body) => (await handle(post(path, body), store, now)).json();

test("the first hello sets the join date; later ones only move last-seen", async () => {
  const store = memoryStore();
  assert.deepEqual(await call(store, 1000, "/v1/hello", { key: A }), { now: 1000, users: { [A]: [1000, 1000] } });
  assert.deepEqual(await call(store, 1300, "/v1/hello", { key: A }), { now: 1300, users: { [A]: [1000, 1300] } });
});

test("hello answers only about yourself; lookup only about who you asked", async () => {
  const store = memoryStore();
  await call(store, 1000, "/v1/hello", { key: A });
  assert.deepEqual(await call(store, 1400, "/v1/hello", { key: B }), { now: 1400, users: { [B]: [1400, 1400] } });
  assert.deepEqual(await call(store, 1500, "/v1/lookup", { keys: [A, C, A] }), { now: 1500, users: { [A]: [1000, 1000] } });
  assert.deepEqual(await call(store, 1500, "/v1/lookup", { keys: [] }), { now: 1500, users: {} });
});

test("bye hides the row; hello brings it back with the same join date", async () => {
  const store = memoryStore();
  await call(store, 1000, "/v1/hello", { key: A });
  assert.deepEqual(await call(store, 2000, "/v1/bye", { key: A }), { now: 2000, users: {} });
  assert.deepEqual((await call(store, 2100, "/v1/lookup", { keys: [A] })).users, {});
  assert.deepEqual((await call(store, 3000, "/v1/hello", { key: A })).users[A], [1000, 3000]);
});

test("the rows can't be listed, and only hashed keys are accepted", async () => {
  const store = memoryStore();
  await call(store, 1000, "/v1/hello", { key: A });
  for (const path of ["/v1/users", "/v1/lookup", "/v1/hello", "/"]) {
    assert.equal((await handle(get(path), store)).status, 404, path);
  }
  for (const body of [{ key: "123456789012345678" }, { key: A.toUpperCase() }, { key: A + "a" }, {}, "nope"]) {
    assert.ok((await handle(post("/v1/hello", body), store, 1000)).status >= 400, JSON.stringify(body));
  }
  const tooMany = Array.from({ length: MAX_LOOKUP + 1 }, (_, i) => i.toString(16).padStart(64, "0"));
  for (const body of [{ keys: "x" }, { keys: [A, "123"] }, { keys: tooMany }, {}]) {
    assert.ok((await handle(post("/v1/lookup", body), store, 1000)).status >= 400, JSON.stringify(body).slice(0, 60));
  }
  assert.equal(store.rows.size, 1);
});

test("only JSON requests are read, so a web page can't write rows through a browser", async () => {
  const store = memoryStore();
  for (const type of [null, "text/plain", "text/plain;charset=UTF-8", "application/x-www-form-urlencoded"]) {
    assert.equal((await handle(post("/v1/hello", { key: A }, type), store, 1000)).status, 415, String(type));
  }
  assert.equal(store.rows.size, 0);
  assert.equal((await handle(post("/v1/hello", { key: A }, "Application/JSON; charset=utf-8"), store, 1000)).status, 200);
  assert.equal(store.rows.size, 1);
});