Files
plugins/packages/agy-cli/test/signin.mjs
T
yetone 29547d4200 agy-cli 0.1.97: usage finds an agy installed since at once, and a sign-in is named by its Google account
@tmr11235 on X (magpie v0.1.4520):

1. "The model test works, but the account says there is no CLI."
   Usage held its "agy CLI not found" error for the backoff, which grows
   from 10 minutes to 6 hours. A read made before agy was installed kept
   saying "not found" for hours after a sign-in installed agy, while
   chats and model tests already ran through it. Now:
   - no agy found is never held;
   - a held failure belongs to the agy path it was held for, so an agy
     found since is read on the next refresh;
   - the message says how magpie gets agy (sign in again), in noAgy's
     words.

2. "After removing it and signing out, 登录 restored the account with no
   sign-in, shown as API key." magpie's remove/sign-out deletes only
   magpie's record. agy on the computer stays signed in, so 登录 adopts
   that sign-in again with no browser (method 0's "auto"). That part is
   by design. The "API key" label came from a sign-in saved without an
   email: agy's keyring value names its account in id_token_email, and
   when it doesn't (or the read fails), the saved record had no
   metadata.email. pluginLabels then falls back to the record's type,
   "API key". signedInAs now asks Google's userinfo with agy's own
   access token when the stored value names no account, so the account
   is listed under its email. An account saved before this keeps its
   label until its next sign-in (登录 on it), which saves the email.

Their later quota "as of 2026/11/15, couldn't be read just now" is not
this: "not found" isn't a passing error, so magpie shows it rather than
an old reading. The read behind it fails with a timeout, 429 or network
error that magpie stood an old reading in for; magpie now says which
beside the date (magpie, readError).

Run (at origin/main bb849910 + this commit):
- mote, HOME=$(mktemp -d): packages/agy-cli `bun test`: 41 pass, 0 fail (3 files, 132s)
- test/usage.mjs alone: 46 pass, 0 fail. test/signin.mjs alone: 8 pass.
- The two new usage tests on the old usage.mjs (origin/main's), by hand:
  both fail. signedInEmail's test fails there too (no such export).
- Real: agy 1.5.10's stored access token on this Mac, Google's userinfo
  answers 200 with the account's email (masked in the log).
- Real Claude Code 2.1.327 through a sandbox magpie (agy-cli 0.1.96,
  agy 1.5.10, gemini-3.8-flash, file sign-in in a sandbox HOME), for
  the cache report from @niyezaimenglima: first run with a Read call
  48,427 of 78,349 input cached; a --continue run 30,296 of 33,088
  cached; one agy session for the conversation. Not reproduced, so no
  change here for it.

Not tried: the reporter's own keyring value (whether it lacks
id_token_email, or an env sign-in was set); Windows and Linux; the
sign-in flow end to end in the GUI with the userinfo fallback.
2026-11-19 17:06:10 +08:00

143 lines
8.8 KiB
JavaScript

import assert from "node:assert/strict"
import { test } from "node:test"
import { backgroundBlocked, cannotSignIn, emailOf, forgetStoredSignIn, isDead, noteDead, parseStored, readStoredSignIn, signedInEmail, storedEmail, storedSignIn, storedSignInNow } from "../signin.mjs"
const NOW = Date.parse("2026-11-13T00:00:00Z")
const stored = (token, extra = {}) => JSON.stringify({ token, auth_method: "consumer", id_token_email: "someone@example.com", ...extra })
const b64 = (s) => "go-keyring-base64:" + Buffer.from(s).toString("base64")
test("reads what agy's keyring value says, on macOS (base64) and Windows (raw JSON)", () => {
const renewable = stored({ access_token: "a", refresh_token: "r", expiry: "2026-11-12T13:00:00Z", token_type: "Bearer" })
for (const raw of [b64(renewable), renewable]) {
const s = parseStored(raw, NOW)
assert.equal(s.state, "present")
assert.equal(s.canRenew, true)
assert.equal(s.expired, true)
assert.match(s.fp, /^[0-9a-f]{32}$/)
assert.doesNotMatch(JSON.stringify(s), /someone|"a"|"r"/)
// an ended access token with a refresh token is how a working agy looks
assert.equal(cannotSignIn(s), "")
}
assert.notEqual(parseStored(renewable, NOW).fp, parseStored(stored({ refresh_token: "r2" }), NOW).fp)
})
test("an ended sign-in with nothing to renew it, or no token at all, can't sign agy in", () => {
const ended = parseStored(b64(stored({ access_token: "a", expiry: "2026-11-12T00:00:00Z" })), NOW)
assert.equal(ended.state, "present")
assert.match(cannotSignIn(ended), /expired/)
const live = parseStored(stored({ access_token: "a", expiry: "2026-11-14T00:00:00Z" }), NOW)
assert.equal(cannotSignIn(live), "")
// Go's zero time: doesn't end
const zero = parseStored(stored({ access_token: "a", expiry: "0001-01-01T00:00:00Z" }), NOW)
assert.equal(zero.expired, false)
assert.equal(cannotSignIn(zero), "")
assert.equal(parseStored(stored({ access_token: "", refresh_token: "" }), NOW).state, "missing")
assert.match(cannotSignIn({ state: "missing" }), /isn't signed in/)
})
test("a value of another shape, or an API key, concludes nothing", () => {
for (const raw of ["", "garbage", b64("not json"), JSON.stringify({ hello: 1 }), stored(undefined, { api_key: "k" }), stored({}, { api_key: "k" })]) {
const s = parseStored(raw, NOW)
assert.equal(s.state, "unknown", raw)
assert.equal(cannotSignIn(s), "")
}
})
test("readStoredSignIn asks each system's keyring the way go-keyring stores it", async () => {
const value = stored({ access_token: "a", refresh_token: "r", expiry: "2026-11-12T00:00:00Z" })
const calls = []
const mac = (code, stdout = "") => async (file, args) => { calls.push([file, ...args]); return { code, stdout } }
assert.equal((await readStoredSignIn({ platform: "darwin", exec: mac(44), env: {} })).state, "missing")
assert.deepEqual(calls[0], ["/usr/bin/security", "find-generic-password", "-s", "gemini", "-a", "antigravity", "-w"])
assert.equal((await readStoredSignIn({ platform: "darwin", exec: mac(51), env: {} })).state, "unknown")
assert.equal((await readStoredSignIn({ platform: "darwin", exec: mac(0, b64(value) + "\n"), now: NOW, env: {} })).state, "present")
const win = (stdout, code = 0) => async (file, args) => { calls.push([file, ...args]); return { code, stdout } }
assert.equal((await readStoredSignIn({ platform: "win32", exec: win("MISSING\r\n"), env: {} })).state, "missing")
assert.match(calls.at(-1).at(-1), /CredReadW[\s\S]*'gemini:antigravity'/)
assert.equal((await readStoredSignIn({ platform: "win32", exec: win("WARNING: something\r\nMISSING\r\n"), env: {} })).state, "missing")
assert.equal((await readStoredSignIn({ platform: "win32", exec: win("ERROR 5\r\n"), env: {} })).state, "unknown")
assert.equal((await readStoredSignIn({ platform: "win32", exec: win("", 1), env: {} })).state, "unknown")
const w = await readStoredSignIn({ platform: "win32", exec: win("OK " + Buffer.from(value).toString("base64") + "\r\n"), now: NOW, env: {} })
assert.equal(w.state, "present")
assert.equal(w.canRenew, true)
assert.equal((await readStoredSignIn({ platform: "linux", exec: mac(0), env: {} })).state, "unknown")
const before = calls.length
for (const env of [{ AGY_BRIDGE_STORED_SIGNIN: "skip" }, { GEMINI_API_KEY: "k" }, { AGY_ADC_AUTH: "1" }]) {
assert.equal((await readStoredSignIn({ platform: "darwin", exec: mac(44), env })).state, "unknown")
}
assert.equal(calls.length, before)
})
test("one read serves a burst; a dead sign-in blocks background runs until another is stored", async () => {
forgetStoredSignIn({ dead: true })
let reads = 0
let value = { state: "present", fp: "one", canRenew: true, expiresAt: 0, expired: false }
const read = async () => { reads++; return value }
await Promise.all([storedSignIn({ read }), storedSignIn({ read }), storedSignIn({ read })])
assert.equal(reads, 1)
assert.equal(await backgroundBlocked({ read }), "")
noteDead(value)
assert.ok(isDead(value))
assert.match(await backgroundBlocked({ read }), /no longer works/)
value = { ...value, fp: "two" }
assert.equal(await backgroundBlocked({ read, fresh: true }), "")
value = { state: "missing" }
assert.match(await backgroundBlocked({ read, fresh: true }), /isn't signed in/)
forgetStoredSignIn({ dead: true })
assert.equal(isDead({ state: "present", fp: "one" }), false)
})
test("storedSignInNow answers from the last read without waiting, and reads again when it is old", async () => {
forgetStoredSignIn({ dead: true })
let reads = 0
let release
const read = () => { reads++; return new Promise((r) => { release = r }) }
assert.equal(storedSignInNow({ read }), null)
assert.equal(storedSignInNow({ read }), null)
await new Promise((r) => setTimeout(r, 0))
assert.equal(reads, 1)
release({ state: "missing" })
await new Promise((r) => setTimeout(r, 0))
assert.deepEqual(storedSignInNow({ read }), { state: "missing" })
assert.equal(reads, 1)
forgetStoredSignIn({ dead: true })
})
// Archivemylife on X: the account showed as "API key", and signing in to
// another Google account looked like nothing happened. A sign-in names the
// account by the email agy stored; the cached read still doesn't carry it.
// tmr11235 on X (v0.1.4520): agy's own sign-in, taken again after the
// account was removed, was listed as "API key". A stored sign-in that
// doesn't name its account is named by who Google says its token is for.
test("signedInEmail asks Google who a sign-in is for when agy's doesn't say", async () => {
const mac = (raw) => async () => ({ code: 0, stdout: b64(raw) })
const asked = []
const google = (reply, status = 200) => async (url, init) => {
asked.push([url, init.headers.Authorization])
return new Response(JSON.stringify(reply), { status })
}
const named = stored({ access_token: "a" })
assert.equal(await signedInEmail({ platform: "darwin", exec: mac(named), env: {} }, { fetch: google({ email: "other@example.com" }) }), "someone@example.com")
assert.equal(asked.length, 0, "a sign-in that names its account isn't asked about")
const unnamed = stored({ access_token: "tok" }, { id_token_email: undefined })
assert.equal(await signedInEmail({ platform: "darwin", exec: mac(unnamed), env: {} }, { fetch: google({ email: "who@example.com" }) }), "who@example.com")
assert.deepEqual(asked, [["https://www.googleapis.com/oauth2/v2/userinfo", "Bearer tok"]])
assert.equal(await signedInEmail({ platform: "darwin", exec: mac(unnamed), env: {} }, { fetch: google({ error: "x" }, 401) }), "")
assert.equal(await signedInEmail({ platform: "darwin", exec: mac(unnamed), env: {} }, { fetch: async () => { throw new Error("offline") } }), "")
assert.equal(await signedInEmail({ platform: "darwin", exec: async () => ({ code: 44, stdout: "" }), env: {} }, { fetch: google({ email: "who@example.com" }) }), "")
})
test("storedEmail is the Google account agy's keyring is signed in to, on macOS and Windows", async () => {
const value = stored({ access_token: "a", refresh_token: "r", expiry: "2026-11-12T00:00:00Z" })
assert.equal(emailOf(b64(value)), "someone@example.com")
assert.equal(emailOf(value), "someone@example.com")
assert.equal(emailOf(stored({ access_token: "a" }, { id_token_email: undefined })), "")
assert.equal(emailOf("garbage"), "")
const mac = (code, stdout = "") => async () => ({ code, stdout })
assert.equal(await storedEmail({ platform: "darwin", exec: mac(0, b64(value) + "\n"), env: {} }), "someone@example.com")
assert.equal(await storedEmail({ platform: "darwin", exec: mac(44), env: {} }), "")
assert.equal(await storedEmail({ platform: "darwin", exec: async () => { throw new Error("no security") }, env: {} }), "")
assert.equal(await storedEmail({ platform: "win32", exec: mac(0, "OK " + Buffer.from(value).toString("base64") + "\r\n"), env: {} }), "someone@example.com")
assert.equal(await storedEmail({ platform: "darwin", exec: mac(0, b64(value)), env: { AGY_BRIDGE_STORED_SIGNIN: "skip" } }), "")
})