Home chat surface, onboarding assessment, rectification cases API, Mastra rectification tools and admin telemetry are gone; 生时纠正 is the standalone /rectification page. Old birth_time_rectification sessions are hidden from lists. Consult offer card links to the page. Copy renamed to 生时纠正. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01DbBXozbkyFNqbu8pVEtZ7W
206 lines
7.5 KiB
TypeScript
206 lines
7.5 KiB
TypeScript
import assert from "node:assert/strict";
|
||
import test from "node:test";
|
||
import { readFileSync } from "node:fs";
|
||
import {
|
||
consumeRequestRateLimit,
|
||
consumeUserRateLimitSet,
|
||
requestRateLimits,
|
||
resetRequestRateLimitMaintenanceForTests,
|
||
userRateLimitDeniedResponse,
|
||
userRateLimitMessages,
|
||
} from "../src/lib/request-rate-limit.ts";
|
||
|
||
test("a user is allowed up to the window limit and then must wait", () => {
|
||
const store = new Map<string, number[]>();
|
||
const first = consumeRequestRateLimit({
|
||
key: "search:user-1",
|
||
limit: 2,
|
||
windowMs: 60_000,
|
||
now: 1_000,
|
||
store,
|
||
});
|
||
const second = consumeRequestRateLimit({
|
||
key: "search:user-1",
|
||
limit: 2,
|
||
windowMs: 60_000,
|
||
now: 2_000,
|
||
store,
|
||
});
|
||
const blocked = consumeRequestRateLimit({
|
||
key: "search:user-1",
|
||
limit: 2,
|
||
windowMs: 60_000,
|
||
now: 3_000,
|
||
store,
|
||
});
|
||
const otherUser = consumeRequestRateLimit({
|
||
key: "search:user-2",
|
||
limit: 2,
|
||
windowMs: 60_000,
|
||
now: 3_000,
|
||
store,
|
||
});
|
||
const afterWindow = consumeRequestRateLimit({
|
||
key: "search:user-1",
|
||
limit: 2,
|
||
windowMs: 60_000,
|
||
now: 61_500,
|
||
store,
|
||
});
|
||
|
||
assert.deepEqual(first, { ok: true });
|
||
assert.deepEqual(second, { ok: true });
|
||
assert.deepEqual(blocked, { ok: false, retryAfterSeconds: 58 });
|
||
assert.deepEqual(otherUser, { ok: true });
|
||
assert.deepEqual(afterWindow, { ok: true });
|
||
});
|
||
|
||
test("keys past the longest seen window are swept, and a one-hour key survives 61 seconds", () => {
|
||
resetRequestRateLimitMaintenanceForTests();
|
||
const shortWindow = new Map<string, number[]>();
|
||
consumeRequestRateLimit({
|
||
key: "old",
|
||
limit: 5,
|
||
windowMs: 60_000,
|
||
now: 0,
|
||
store: shortWindow,
|
||
});
|
||
for (let index = 0; index < 998; index += 1) {
|
||
consumeRequestRateLimit({
|
||
key: `pad:${index}`,
|
||
limit: 5,
|
||
windowMs: 60_000,
|
||
now: 0,
|
||
store: shortWindow,
|
||
});
|
||
}
|
||
consumeRequestRateLimit({
|
||
key: "trigger",
|
||
limit: 5,
|
||
windowMs: 60_000,
|
||
now: 61_000,
|
||
store: shortWindow,
|
||
});
|
||
assert.equal(shortWindow.has("old"), false);
|
||
assert.equal(shortWindow.get("old"), undefined);
|
||
assert.equal(shortWindow.has("trigger"), true);
|
||
assert.equal(shortWindow.size, 1);
|
||
|
||
resetRequestRateLimitMaintenanceForTests();
|
||
const mixed = new Map<string, number[]>();
|
||
consumeRequestRateLimit({
|
||
key: "synastry:person",
|
||
limit: 8,
|
||
windowMs: 60 * 60_000,
|
||
now: 0,
|
||
store: mixed,
|
||
});
|
||
consumeRequestRateLimit({
|
||
key: "ephemeris:person",
|
||
limit: 30,
|
||
windowMs: 60_000,
|
||
now: 0,
|
||
store: mixed,
|
||
});
|
||
for (let index = 0; index < 998; index += 1) {
|
||
consumeRequestRateLimit({
|
||
key: `later:${index}`,
|
||
limit: 5,
|
||
windowMs: 60_000,
|
||
now: 61_000,
|
||
store: mixed,
|
||
});
|
||
}
|
||
assert.equal(mixed.has("synastry:person"), true);
|
||
assert.equal(mixed.has("ephemeris:person"), true);
|
||
resetRequestRateLimitMaintenanceForTests();
|
||
});
|
||
|
||
test("consult allows 10 per minute and refuses the 11th without charging the hour bucket further", () => {
|
||
const store = new Map<string, number[]>();
|
||
for (let index = 0; index < 10; index += 1) {
|
||
const decision = consumeUserRateLimitSet(
|
||
["consultMinute", "consultHour"],
|
||
"person",
|
||
1_000 + index,
|
||
store,
|
||
);
|
||
assert.deepEqual(decision, { ok: true });
|
||
}
|
||
const denied = consumeUserRateLimitSet(["consultMinute", "consultHour"], "person", 2_000, store);
|
||
assert.equal(denied.ok, false);
|
||
if (!denied.ok) assert.equal(denied.retryAfterSeconds, 59);
|
||
assert.equal(store.get("consultMinute:person")?.length, 10);
|
||
assert.equal(store.get("consultHour:person")?.length, 10);
|
||
assert.equal(requestRateLimits.consultMinute.limit, 10);
|
||
assert.equal(requestRateLimits.consultHour.limit, 120);
|
||
});
|
||
|
||
test("the hour bucket can refuse while the minute bucket still has room, and neither bucket records the miss", () => {
|
||
const store = new Map<string, number[]>();
|
||
for (let index = 0; index < 120; index += 1) {
|
||
consumeRequestRateLimit({
|
||
key: "consultHour:person",
|
||
limit: 120,
|
||
windowMs: 60 * 60_000,
|
||
now: index * 1_000,
|
||
store,
|
||
});
|
||
}
|
||
const denied = consumeUserRateLimitSet(["consultMinute", "consultHour"], "person", 130_000, store);
|
||
assert.equal(denied.ok, false);
|
||
assert.equal(store.has("consultMinute:person"), false);
|
||
assert.equal(store.get("consultHour:person")?.length, 120);
|
||
});
|
||
|
||
test("rectification (chart choice) allows 30 steps a minute; reports have no hourly cap", () => {
|
||
// 原值:校正回合 5 次/分钟、40 次/小时(rectificationTurnMinute / rectificationTurnHour)
|
||
// 新值:生时纠正页每步一次排盘扫描,30 次/分钟(chartChoice);旧回合桶删除
|
||
// 原因:2026-11-06 会话式生时纠正下线
|
||
assert.equal(requestRateLimits.chartChoice.limit, 30);
|
||
assert.equal(requestRateLimits.chartChoice.windowMs, 60_000);
|
||
assert.equal("rectificationTurnMinute" in requestRateLimits, false);
|
||
assert.equal("rectificationTurnHour" in requestRateLimits, false);
|
||
// 原值:报告生成另有 3 次/小时;新值:报告按点数付费,不再按小时限(BUG-1641,产品 2026-11-05)
|
||
assert.equal("reportGenerateHour" in requestRateLimits, false);
|
||
});
|
||
|
||
test("a denied response carries 429, rate_limited, and Retry-After", () => {
|
||
const denied = userRateLimitDeniedResponse(12, userRateLimitMessages.consult);
|
||
assert.equal(denied.status, 429);
|
||
assert.equal(denied.headers["Retry-After"], "12");
|
||
assert.deepEqual(denied.body, {
|
||
error: "这一会儿问得有点密,先停一下。",
|
||
code: "rate_limited",
|
||
retryAfterSeconds: 12,
|
||
});
|
||
});
|
||
|
||
test("consult, rectification, and report creation check the limit before they reserve", () => {
|
||
const consult = readFileSync(new URL("../src/app/api/consult/route.ts", import.meta.url), "utf8");
|
||
const consultSettlement = readFileSync(new URL("../src/lib/consult/settlement.ts", import.meta.url), "utf8");
|
||
const chartChoice = readFileSync(new URL("../src/lib/chart-choice/route-support.ts", import.meta.url), "utf8");
|
||
const reports = readFileSync(new URL("../src/app/api/reports/route.ts", import.meta.url), "utf8");
|
||
const reportCore = readFileSync(new URL("../src/lib/personal-report-route-core.ts", import.meta.url), "utf8");
|
||
// 原值: route.ts 里 consumeUserRateLimitSet( 早于 reserve_consultation_usage
|
||
// 新值: route.ts 里限额调用早于 consultSettlement.reserveCredit;RPC 名在 settlement.ts
|
||
// 原因: 预留回调体搬进结算工厂,限额仍在开流前,顺序不跨文件拼接
|
||
assert.ok(consult.indexOf("consumeUserRateLimitSet(") < consult.indexOf("consultSettlement.reserveCredit"));
|
||
assert.match(consultSettlement, /rpc\("reserve_consultation_usage"/);
|
||
assert.match(consult, /userRateLimitDeniedResponse\(/);
|
||
assert.match(chartChoice, /consumeUserRequestRateLimit\("chartChoice", user\.id\)/);
|
||
// Reports are paid in credits and have no hourly cap any more (BUG-1641).
|
||
assert.equal(reports.includes("reportGenerateHour"), false);
|
||
assert.equal(reportCore.includes("consumeReportRateLimit"), false);
|
||
});
|
||
|
||
test("a failed report does not use up the daily count of an unbilled deployment (BUG-1641)", () => {
|
||
const reports = readFileSync(new URL("../src/app/api/reports/route.ts", import.meta.url), "utf8");
|
||
assert.match(reports, /status !== "failed"/);
|
||
for (const path of ["../src/lib/annual-report-server.ts", "../src/lib/synastry-full-create.ts"]) {
|
||
const source = readFileSync(new URL(path, import.meta.url), "utf8");
|
||
assert.equal(source.includes("今日报告生成次数已达上限"), false, path);
|
||
assert.match(source, /已有报告正在生成中/, path);
|
||
}
|
||
});
|