From 806a3f7aedf706b85f07ad09d0a0f7b8909962e0 Mon Sep 17 00:00:00 2001 From: "zhou.han" Date: Wed, 11 Nov 2026 21:10:53 +0800 Subject: [PATCH] inkstone: the cookie overwrite test freezes its clock (#997) (#1003) The cookie-probe test "a new row, or an overwritten one, is this sign-in" read Date.now() twice, once for the inserted row's last_update_utc and once for the UPDATE. When both calls landed in the same millisecond the value didn't change, rowKey matched the baseline, and the test went red (#997). The test now freezes Date.now() while it builds the profile and takes the baseline, writes the overwritten row's last_update_utc one second later, and checks that the unchanged row is not taken as a new sign-in. Date.now is put back in finally. Test-only: lib/cookieProbe.mjs is unchanged. What ran, at eda11c37 merged with origin/main dea9e4e3, on WSL Ubuntu 24.04 (Bun 1.4.2, HOME=$(mktemp -d)): - bun test packages/inkstone: 129 pass, 0 fail. - The changed test alone, 30 runs: 30 pass. - Same-millisecond case injected (a --preload that freezes Date.now): the old test fails, the new one passes. - bun scripts/check.mjs inkstone: ok. - Root bun test: 8315 pass, 0 fail, 8334 tests in 584 files. Broken by hand (each went red, then reverted): - rowKey without last_update_utc: the new test is red, so it still guards the in-place overwrite. - profileHasNewCookie always true: the new "unchanged row is not new" assertion is red. Not tried: Windows and macOS runs of this test. Fixes #997. --- packages/inkstone/cookie-probe.test.mjs | 25 ++++++++++++++++--------- 1 file changed, 16 insertions(+), 9 deletions(-) diff --git a/packages/inkstone/cookie-probe.test.mjs b/packages/inkstone/cookie-probe.test.mjs index c83feff..c676976 100644 --- a/packages/inkstone/cookie-probe.test.mjs +++ b/packages/inkstone/cookie-probe.test.mjs @@ -58,15 +58,22 @@ test("a row that was there at launch is not this sign-in (the #847 blocking case }) test("a new row, or an overwritten one, is this sign-in", () => { - // a brand-new row (creation_utc advanced) - let profile = fakeProfileWith([{ name: "uaa-token", creation_utc: chromiumMicros(Date.now() - 60_000) }]) - const baseline = cookieBaseline(profile, "uaa-token", DOMAIN) - // Chrome overwrites in place: creation_utc stays, last_update_utc moves - const { Database } = require("bun:sqlite") - const db = new Database(join(profile, "Default", "Cookies")) - db.query("UPDATE cookies SET last_update_utc = ? WHERE name = 'uaa-token'").run(chromiumMicros()) - db.close() - expect(profileHasNewCookie(profile, "uaa-token", baseline, DOMAIN)).toBe(true) + const frozenNowMs = Date.now() + const originalDateNow = Date.now + Date.now = () => frozenNowMs + try { + const profile = fakeProfileWith([{ name: "uaa-token", creation_utc: chromiumMicros(frozenNowMs - 60_000) }]) + const baseline = cookieBaseline(profile, "uaa-token", DOMAIN) + expect(profileHasNewCookie(profile, "uaa-token", baseline, DOMAIN)).toBe(false) + + const { Database } = require("bun:sqlite") + const db = new Database(join(profile, "Default", "Cookies")) + db.query("UPDATE cookies SET last_update_utc = ? WHERE name = 'uaa-token'").run(chromiumMicros(frozenNowMs + 1000)) + db.close() + expect(profileHasNewCookie(profile, "uaa-token", baseline, DOMAIN)).toBe(true) + } finally { + Date.now = originalDateNow + } }) test("an expired row, another domain's row, another cookie's row: all refused", () => {