Skip to content
Myr

prelude.test.ts

This file

Path

packages/rules/test/prelude.test.ts

169 lines · 4.8 KiB · TypeScript

Last changed by

Merge build/comments-packages-rest into mainoscabrielbefore adoption38cd503

History of this file

Take it

The walk stopped after 200 commits: lines older than 678c8b8 point at it.

Merge build/614-env-service: deciders set write-only values for @sensitive items, the public sees the env shape, and checks see only placeholdersoscabrielbefore adoption678c8b8

lines 1–169why, one step back
1import { describe, expect, it } from "@effect/vitest";
23import {4 installFrozenGlobals,5 mainModule,6 preludeModule,7} from "../src/prelude.ts";8import type { FreezableScope, FrozenFacts } from "../src/prelude.ts";910const facts: FrozenFacts = { now: 1_791_374_400_000, seed: 7 };1112/**13 * mulberry32 seeded with 7, worked out from the published algorithm apart14 * from the code under test: the first eight numbers it gives.15 */16const seven = [17 0.011704753153026104, 0.06195825757458806, 0.97690763277933,18 0.6990287057124078, 0.5214452685322613, 0.4055216880515218,19 0.4662326325196773, 0.23992518591694534,20];2122/**23 * A scope of the test's own, so the test process keeps its real clock: its24 * `Date` has a prototype of its own, and its randomness answers 0.5.25 */26const scopeOf = (): FreezableScope => ({27 Date: class extends Date {},28 Math: { random: () => 0.5 },29 crypto: {30 getRandomValues: (array) => array,31 randomUUID: () => "00000000-0000-4000-8000-000000000000",32 },33 performance: { now: () => 1 },34});3536const draws = (seed: number) => {37 const scope = scopeOf();3839 installFrozenGlobals(scope, { ...facts, seed });4041 return {42 bytes: [...scope.crypto.getRandomValues(new Uint32Array(2))],43 uuid: scope.crypto.randomUUID(),44 };45};4647const UUID_V4 =48 /^[\da-f]{8}-[\da-f]{4}-4[\da-f]{3}-[89ab][\da-f]{3}-[\da-f]{12}$/u;4950describe(installFrozenGlobals, () => {51 it("gives the scope the facts' clock", () => {52 const scope = scopeOf();5354 installFrozenGlobals(scope, facts);5556 expect({57 constructed: new scope.Date().toISOString(),58 dated: new scope.Date(0).toISOString(),59 now: scope.Date.now(),60 performance: scope.performance.now(),61 throughConstructor: new scope.Date().constructor === scope.Date,62 }).toStrictEqual({63 constructed: "2026-10-07T12:00:00.000Z",64 // A date that names its own time is left alone.65 dated: "1970-01-01T00:00:00.000Z",66 now: 1_791_374_400_000,67 performance: 0,68 // A date's own constructor leads back to the frozen clock.69 throughConstructor: true,70 });71 });7273 it("leaves no way back to the scope's own clock", () => {74 const scope = scopeOf();75 const own = scope.Date;7677 installFrozenGlobals(scope, facts);7879 expect({80 // A date the runtime makes itself (`structuredClone` does) is the81 // scope's own Date's: its constructor is the frozen one too.82 madeByTheRuntime: new own(0).constructor === scope.Date,83 // The frozen Date shares its dates' prototype, so nothing of the84 // scope's own sits above it.85 oneDatePrototype: scope.Date.prototype === own.prototype,86 ownNow: own.now(),87 }).toStrictEqual({88 madeByTheRuntime: true,89 oneDatePrototype: true,90 ownNow: 1_791_374_400_000,91 });92 });9394 it("gives the scope randomness seeded from the facts", () => {95 const scope = scopeOf();9697 installFrozenGlobals(scope, facts);9899 const numbers = [100 scope.Math.random(),101 scope.Math.random(),102 scope.Math.random(),103 ];104105 // Each byte is the next number, times 256, rounded down.106 const bytes = [...scope.crypto.getRandomValues(new Uint8Array(4))];107108 expect({ bytes, numbers }).toStrictEqual({109 bytes: [178, 133, 103, 119],110 numbers: seven.slice(0, 3),111 });112 });113114 it("gives every install with the same seed the same randomness", () => {115 const first = draws(7);116117 expect(draws(7)).toStrictEqual(first);118 expect(draws(8)).not.toStrictEqual(first);119 expect(first.uuid).toMatch(UUID_V4);120 });121122 it("starts every job's code from where the first settled", () => {123 const scope = scopeOf();124 const frozen = installFrozenGlobals(scope, facts);125126 // Loading the modules draws once; then each job starts from there.127 scope.Math.random();128 frozen.settle();129 const first = scope.Math.random();130 scope.Math.random();131 frozen.settle();132 const second = scope.Math.random();133134 expect([first, second]).toStrictEqual([seven[1], seven[1]]);135 });136});137138describe(preludeModule, () => {139 it("holds the installer and the facts as JSON, and nothing else", () => {140 const source = preludeModule(facts)141 .replace(String(installFrozenGlobals), "<installFrozenGlobals>")142 .replace('{"now":1791374400000,"seed":7}', "<facts>");143144 expect(source).toBe(145 "export const frozen = (<installFrozenGlobals>)(globalThis, <facts>);\n"146 );147 });148});149150describe(mainModule, () => {151 it("imports the prelude before anything else", () => {152 const source = mainModule({153 asks: [".cfg/asks/a.ask.ts"],154 project: ".cfg/project.ts",155 });156157 const imports = [158 ...source.matchAll(159 /\bimport\s*(?:\(\s*|[^"'()]*?from\s*)(?<quote>["'])(?<specifier>[^"']+)\k<quote>/gu160 ),161 ].map((match) => match.groups?.specifier);162163 expect(imports).toStrictEqual([164 "./prelude.js",165 "./.cfg/project.ts",166 "./.cfg/asks/a.ask.ts",167 ]);168 });169});