Files
bitgate/packages/standalone/test/standalone.test.js
T
Cursor AgentandthePR0M3TH3AN 9e6f6c659e Add --bitgate-* theme tokens and a build-injected VERSION.
Widget styles now prefer --bitgate-* custom properties, falling back to the
deprecated --gov-* names and then the existing defaults so current pages and
overrides keep working. Browser bundles and ESM entry points expose VERSION
(and window.bitgate.version) from package.json. Document allowlist on the
widget policy preset list. Sync the lockfile so npm ci sees @bitgate/standalone.

Co-authored-by: thePR0M3TH3AN <[email protected]>
2026-10-08 19:14:44 +00:00

89 lines
3.0 KiB
JavaScript

// The standalone entry must surface the whole headless path from one import —
// engine (core) + label codec (nostr) — so a no-build site gets everything from
// the single bundled file. Tests the entry, not the built artifact (dist is
// gitignored); the build itself is exercised by `npm run build:standalone`.
import { readFileSync } from "node:fs";
import { dirname, join } from "node:path";
import { fileURLToPath } from "node:url";
import { describe, expect, it } from "vitest";
import * as bitgate from "../src/index.js";
const HERE = dirname(fileURLToPath(import.meta.url));
const rootPkg = JSON.parse(readFileSync(join(HERE, "..", "..", "..", "package.json"), "utf8"));
describe("standalone entry surface", () => {
it("re-exports the core engine and the Nostr label codec together", () => {
for (const name of [
// core engine
"evaluateTarget",
"createSnapshot",
"reduceAdminState",
"createAuthorityState",
"createPolicyDefinition",
"createEventTarget",
"ALLOWLIST_POLICY",
"VERSION",
// nostr label codec
"decodeLabels",
"encodeLabel",
"labelsToContributions",
"LABEL_KIND",
]) {
expect(bitgate[name], name).toBeDefined();
}
});
it("composes a headless decision end to end from the single entry", () => {
const ROOT = "a1".repeat(32);
const MOD = "b2".repeat(32);
const PLUGIN = "e5".repeat(32);
const NOW = 1_750_000_000;
const target = bitgate.createEventTarget(PLUGIN);
expect(target).not.toBeNull();
const labelEvent = {
...bitgate.encodeLabel({ value: "deny", namespace: "ns", targets: [/** @type {any} */ (target)] }),
pubkey: MOD,
created_at: NOW,
id: "0".repeat(64),
sig: "",
};
const contributions = bitgate.labelsToContributions(bitgate.decodeLabels(labelEvent), {
namespace: "ns",
denyValues: ["deny"],
});
const authority = bitgate.createAuthorityState({ root: ROOT, actors: { [MOD]: ["moderator"] } });
const snapshot = bitgate.createSnapshot({ authority, admin: bitgate.reduceAdminState(contributions, authority) });
const policy = bitgate.createPolicyDefinition({
id: "p",
version: "1.0.0",
defaultProfile: "d",
profiles: { d: { name: "d", administrativeDeny: { visibility: "warn", transaction: "deny" }, reports: {}, mutes: {} } },
});
const decision = bitgate.evaluateTarget(/** @type {any} */ (target), snapshot, {
surface: "d",
policyProfile: "d",
policy,
now: NOW,
});
expect(decision.transaction?.effect).toBe("deny");
});
it("exports VERSION matching package.json", () => {
expect(bitgate.VERSION).toBe(rootPkg.version);
});
it("publishes version onto a window-like global without replacing other keys", () => {
const target = { bitgate: { extra: true } };
bitgate.exposeBitGateVersion(target);
expect(target.bitgate.version).toBe(bitgate.VERSION);
expect(target.bitgate.extra).toBe(true);
});
});