Files
jiapuapp/tests/auth-sms-cooldown-runtime-smoke.js
T
2026-07-27 06:50:23 +08:00

88 lines
2.1 KiB
JavaScript

"use strict";
const assert = require("assert");
const fs = require("fs");
const path = require("path");
const toDataModuleUrl = (source) =>
`data:text/javascript;base64,${Buffer.from(source).toString("base64")}`;
const run = async () => {
const source = fs.readFileSync(
path.join(__dirname, "../utils/auth-sms-cooldown.js"),
"utf8",
);
const { createAuthSmsCooldown } = await import(toDataModuleUrl(source));
let now = 1_000_000;
const timers = new Map();
let nextTimerId = 1;
const setIntervalFn = (callback) => {
const id = nextTimerId++;
timers.set(id, callback);
return id;
};
const clearIntervalFn = (id) => timers.delete(id);
const firstValues = [];
const first = createAuthSmsCooldown({
operationCode: "sms-login",
onChange: (value) => firstValues.push(value),
now: () => now,
setIntervalFn,
clearIntervalFn,
});
assert.strictEqual(first.sync(), 0);
first.start();
assert.strictEqual(firstValues.at(-1), 60);
now += 10_400;
assert.strictEqual(first.sync(), 50);
first.dispose();
assert.strictEqual(timers.size, 0, "dispose must stop only the page timer");
const restoredValues = [];
const restored = createAuthSmsCooldown({
operationCode: "sms-login",
onChange: (value) => restoredValues.push(value),
now: () => now,
setIntervalFn,
clearIntervalFn,
});
assert.strictEqual(
restored.sync(),
50,
"a recreated page must restore the scene cooldown without storing a phone number",
);
const other = createAuthSmsCooldown({
operationCode: "register",
onChange: () => {},
now: () => now,
setIntervalFn,
clearIntervalFn,
});
assert.strictEqual(other.sync(), 0, "cooldowns must be isolated by TAC scene");
now += 50_000;
assert.strictEqual(restored.sync(), 0);
restored.dispose();
other.dispose();
assert.throws(
() =>
createAuthSmsCooldown({
operationCode: "",
onChange: () => {},
}),
/operationCode/,
);
process.stdout.write("AUTH-SMS-COOLDOWN-RUNTIME-SMOKE PASS\n");
};
run().catch((error) => {
process.stderr.write(`${error.stack || error.message}\n`);
process.exit(1);
});