rooms-kit / test / helpers / load.mjs
 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
/**
 * Module loader for the black-box tests.
 *
 * Normally it imports the committed `dist/` straight from disk — the tests run
 * against the shipped bytes, never against src/, per dev/docs/CLJS.md.
 *
 * With `ROOMS_KIT_TS=1` it instead loads the TypeScript build through
 * helpers/expose.mjs. That mode exists so this suite could be validated against
 * the implementation the vectors were extracted from BEFORE the port landed: a
 * test that passes on the CLJS dist but was never checked against the TS one
 * cannot tell "the port is right" from "the assertion is wrong". Keep it
 * working — it is the suite's own regression harness.
 */
import { join } from "node:path";
import { fileURLToPath } from "node:url";
import { dirname } from "node:path";
import { loadExposed } from "./expose.mjs";

const HERE = dirname(fileURLToPath(import.meta.url));
export const DIST = join(HERE, "..", "..", "dist");
const SCRATCH = join(HERE, "..", "..", "node_modules", ".rooms-kit-exposed");

export const TS_MODE = process.env.ROOMS_KIT_TS === "1";

/** CLJS keeps "private" members as `_name` string keys (peer-kit's idiom). */
export const priv = TS_MODE ? (o, n) => o.__t[n] : (o, n) => o[`_${n}`].bind(o);

/**
 * @param {string} rel   path under dist/, e.g. "lib/crypto.js"
 * @param {Record<string,string[]>} [expose]  classes whose `#private` members
 *        the test needs (TS mode only; ignored for the port)
 */
export function load(rel, expose) {
  const file = join(DIST, rel);
  if (TS_MODE && expose) return loadExposed(file, SCRATCH, expose);
  return import(file);
}

export const readVector = async (name) => {
  const { readFileSync } = await import("node:fs");
  return JSON.parse(readFileSync(join(HERE, "..", "vectors", `${name}.json`), "utf8"));
};

export const hex = (u8) => [...u8].map((b) => b.toString(16).padStart(2, "0")).join("");

export const fromHex = (s) => {
  const out = new Uint8Array(s.length / 2);
  for (let i = 0; i < out.length; i++) out[i] = parseInt(s.slice(i * 2, i * 2 + 2), 16);
  return out;
};

static mirror of HEAD · about · clone: git clone https://git.ardegazu.ro/rooms-kit.git