autoclaw: the refresh test reads the expiry between the clock before and after, not the clock again

"refreshTokens orders hosts by the token's source_id and falls back on
400002" compared the expiry refreshTokens computed (Date.now() + 3600s -
10min) with Date.now() read again after it returned. On a millisecond
boundary between the two reads it is off by one: publish run 38885546686
(devin 0.1.20's push, 29adee2) went red on ubuntu with expires
1792148517446 against 1792148517447, and no package published. The test
now takes the clock before and after the call and checks the expiry lies
between them.

Run: with a 3ms busy-wait injected after refreshTokens returns, the old
test is red (0 pass, 1 fail) and the new one green. bun test in
packages/autoclaw under a mktemp HOME: 9 pass. bun scripts/check.mjs
autoclaw: ok. Test only; the package's version is unchanged.
This commit is contained in:
yetone
2026-10-16 18:14:43 +08:00
parent 3d0a546aa3
commit be7b03e090
+8 -1
View File
@@ -132,13 +132,20 @@ test("refreshTokens orders hosts by the token's source_id and falls back on 4000
}
try {
// An agent (SMS) token tries the CN host first, and its /refresh answers 400002
// the expiry is read off the clock while refreshTokens runs: it is
// between the clock before and after, not the clock read again later
const before = Date.now()
const out = await refreshTokens(jwt({ source_id: "agent", device_id: "dev-1" }), [])
const after = Date.now()
expect(requests.map((r) => `${new URL(r.url).origin}${r.url.pathname}`)).toEqual([
"https://autoglm-api.zhipuai.cn/userapi/v1/refresh",
"https://autoglm-api.zhipuai.cn/userapi/v1/agent-refresh",
])
expect(requests[0].body.device_id).toBe("dev-1")
expect(out).toEqual({ access: "a2", refresh: "r2", expires: Date.now() + 3600_000 - 600_000 })
const { expires, ...tokens } = out
expect(tokens).toEqual({ access: "a2", refresh: "r2" })
expect(expires).toBeGreaterThanOrEqual(before + 3600_000 - 600_000)
expect(expires).toBeLessThanOrEqual(after + 3600_000 - 600_000)
} finally {
globalThis.fetch = realFetch
}