diff --git a/docs/upstream/bot-social-files.json b/docs/upstream/bot-social-files.json index bdce02f1a..e54e65e72 100644 --- a/docs/upstream/bot-social-files.json +++ b/docs/upstream/bot-social-files.json @@ -647,12 +647,6 @@ "mode": "100644", "bytes": 6173 }, - "test/console_rate_limit_notice.test.ts": { - "sha256": "a777773772b1adabb3166f7de71af787775aef5ba7a6a35e3bd250f50b552254", - "git_blob_sha": "2de42441cb7147c9035e664cec6dd48a4e748803", - "mode": "100644", - "bytes": 3662 - }, "test/quiet_summary_names_only_pending_cards.test.ts": { "sha256": "ef06aba830b613503c2bb373e85b0c67d2e77f9754295b0cf5e6fd6f9e7e70e0", "git_blob_sha": "be18d51ce1f89cf829b759fc86f4ebea7aba4d35", diff --git a/docs/upstream/migration-manifest.json b/docs/upstream/migration-manifest.json index a66b9d1d3..5b0dbf822 100644 --- a/docs/upstream/migration-manifest.json +++ b/docs/upstream/migration-manifest.json @@ -1737,7 +1737,7 @@ "scripts/install.sh", "scripts/mcp-smoke.ts" ], - "file_count": 437 + "file_count": 436 } }, "not_imported": { @@ -2258,6 +2258,16 @@ "license_notice": "MIT (Bot Social snapshot)", "new_sha256": "2a477b7e35cdf1d1245486e6980aeb4769d85fc05c82dac774b32814f6325785", "bytes": 22655 + }, + "services/bot-social/test/console_rate_limit_notice.test.ts": { + "destination": "services/bot-social", + "path": "test/console_rate_limit_notice.test.ts", + "original_sha256": "a777773772b1adabb3166f7de71af787775aef5ba7a6a35e3bd250f50b552254", + "original_git_blob_sha": "2de42441cb7147c9035e664cec6dd48a4e748803", + "new_sha256": "38ba94d50b44b4a4bf1fbc59e6f4fd95b56137d1488dd62c2c1bee55efc8941c", + "bytes": 3982, + "reason": "Derived the rate-limit notice lower bound from the burst's own duration so the tests stop failing when the suite loads the machine; the rendered sentence and product code are untouched.", + "license_notice": "Apache-2.0 (FlyBot) or MIT (Bot Social) file modified in this snapshot; see ORIGIN.md." } }, "core_fork_removed": { diff --git a/services/bot-social/test/console_rate_limit_notice.test.ts b/services/bot-social/test/console_rate_limit_notice.test.ts index 2de42441c..c955c76be 100644 --- a/services/bot-social/test/console_rate_limit_notice.test.ts +++ b/services/bot-social/test/console_rate_limit_notice.test.ts @@ -11,18 +11,25 @@ async function post(base: string, path: string, body: unknown): Promise<{ status return { status: r.status, body: await r.json().catch(() => null) }; } +/** The refusal counts its ten-minute window from the burst's first attempt, so a loaded machine can be seconds + * into it by the time the refusal is written; a wait shorter than that is the burst being slow, not a defect. */ +function assertWaitLeft(retryAfter: unknown, elapsedMs: number) { + const lowest = 600 - Math.ceil(elapsedMs / 1000) - 1; + assert.ok(typeof retryAfter === "number" && retryAfter >= lowest && retryAfter <= 600, `retry_after ${retryAfter} after ${elapsedMs} ms`); +} + test("a refused account burst says how long the wait is and that it is the account's", async () => { const S = await startServer(); try { // A successful sign-up leaves the new account's bucket empty, so the five failures below are the whole budget. assert.equal((await post(S.base, "/v1/owners", { username: "burst", password: "secret-pw" })).status, 201); + const started = Date.now(); for (let i = 1; i <= 5; i++) assert.equal((await post(S.base, "/v1/auth/login", { username: "burst", password: "wrong-pw" })).status, 401, `failed attempt ${i} passes the limiter`); const blocked = await post(S.base, "/v1/auth/login", { username: "burst", password: "wrong-pw" }); assert.equal(blocked.status, 429); assert.equal(blocked.body.error.code, "rate_limited"); assert.equal(blocked.body.error.scope, "account"); - // The window starts at the first attempt, so a slow runner may already be a second into it. - assert.ok(blocked.body.error.retry_after >= 599 && blocked.body.error.retry_after <= 600, `retry_after ${blocked.body.error.retry_after}`); + assertWaitLeft(blocked.body.error.retry_after, Date.now() - started); const { errText } = loadConsole(["errText"]); assert.equal( errText(blocked.body.error.code, blocked.body.error.message, blocked.status, blocked.body), @@ -35,12 +42,13 @@ test("a refused address burst says the wait and that it is the network's", async const S = await startServer(); try { // Distinct unknown usernames, so only the address bucket can refuse. + const started = Date.now(); for (let i = 1; i <= 10; i++) assert.equal((await post(S.base, "/v1/auth/login", { username: `nobody${i}`, password: "wrong-pw" })).status, 401, `attempt ${i} passes the limiter`); const blocked = await post(S.base, "/v1/auth/login", { username: "nobody11", password: "wrong-pw" }); assert.equal(blocked.status, 429); assert.equal(blocked.body.error.code, "rate_limited"); assert.equal(blocked.body.error.scope, "ip"); - assert.ok(blocked.body.error.retry_after >= 599 && blocked.body.error.retry_after <= 600, `retry_after ${blocked.body.error.retry_after}`); + assertWaitLeft(blocked.body.error.retry_after, Date.now() - started); const { errText } = loadConsole(["errText"]); assert.equal( errText(blocked.body.error.code, blocked.body.error.message, blocked.status, blocked.body),