Files
plugins/packages/gcp-ai/test/lock.test.mjs
T
Yaka e801e434f3 gcp-ai: Vertex models and Google Cloud credits with your own gcloud ADC (#307)
New package gcp-ai (Yaka88): Vertex models and Google Cloud credits through the user's own gcloud ADC, with multiple accounts, a signed-in model list, and a pipe-only Chrome for the credits read.

Reviewed at 8989b58 against the 10-24 review at 4586c99. I read the whole incremental diff (index.mjs, lib/browser, lib/credentials, lib/lock, test/*). Earlier rounds had already reviewed the rest.
- Tests reaching the network: fixed. test/network.mjs replaces fetch and blocks non-local net/tls/http/dns. `bun test` (40 pass, 1 skip [Linux-only lock test], 0 fail, 106s) ran in a sandbox HOME/TMPDIR behind a dead HTTPS_PROXY, with a preload that logged non-local fetch and connect calls: 0 lines logged. Broken by hand: with the fetch stub removed from "already filed", the guard prints "Unexpected network fetch … userinfo" and bun exits 1. Restored, it exits 0.
- Chrome background traffic: lsof on the test Chrome processes (56 samples during browser.test.mjs, 5 pass) found no inet sockets at all.
- Leftover work after a test: close() now awaits settled, and every sign-in test closes in finally before restoring fetch.
- Smaller points: the lock's age ceiling now comes after the Linux /proc check; settings() is auth > options > env for every field; resolveAdcDirectory and authorizeAdc are removed.
- `bun scripts/check.mjs gcp-ai`: ok. The merge onto main 98985a2 is clean. Loaded into magpie's plugin host (origin/main 4dfd88206) with scripts/try.sh in a sandbox HOME: gcp-ai, 12 models, not signed in.

Not tried: a real Google account or gcloud sign-in, Windows, Linux (the lock test is skipped on macOS). The headless Console credits scrape still runs for every signed-in account, cached on failure, as noted in the earlier review.
2026-10-25 14:33:15 +08:00

105 lines
4.6 KiB
JavaScript

import assert from "node:assert/strict"
import test from "node:test"
import { mkdtemp, readFile, rm, utimes, writeFile } from "node:fs/promises"
import { tmpdir } from "node:os"
import { join } from "node:path"
import { apart, take } from "../lib/lock.mjs"
// Two accounts on one Chrome profile are read one after the other, and magpie
// may run each account's read in a process of its own — so the lock that keeps
// them apart has to be a file, and a hold has to be something another process
// waits on rather than fails on.
test("a lock held by another process is waited for, then taken", async () => {
const directory = await mkdtemp(join(tmpdir(), "vertex-lock-"))
try {
const file = join(directory, "balance")
const release = await take(file)
// A second taker does not fail: it waits, and gets the lock once released.
let taken = false
const waiter = take(file, { poll: 20 }).then((done) => { taken = true; return done })
await new Promise((resolve) => setTimeout(resolve, 150))
assert.equal(taken, false)
await release()
const second = await waiter
assert.equal(taken, true)
await second()
} finally { await rm(directory, { recursive: true, force: true }) }
})
test("releasing twice frees the lock once, and the gap counts from the release", async () => {
const directory = await mkdtemp(join(tmpdir(), "vertex-lock-"))
try {
const file = join(directory, "balance")
const release = await take(file)
await release()
// A second release must not remove a lock someone else now holds.
const held = await take(file)
await release()
await assert.rejects(take(file, { timeout: 50, poll: 10 }), /still using|retry shortly/)
await held()
// The recorded release time is what the next reader waits out.
const before = Date.now()
await apart(file, 300)
assert.ok(Date.now() - before >= 250)
await apart(file, 0)
} finally { await rm(directory, { recursive: true, force: true }) }
})
test("a live pid cannot keep an abandoned lock past fifteen minutes", async () => {
const directory = await mkdtemp(join(tmpdir(), "vertex-lock-"))
// With no /proc start time available, a reused pid is indistinguishable
// from the holder. The age ceiling must still leave a full sign-in time.
const original = process.platform
Object.defineProperty(process, "platform", { value: "darwin", configurable: true })
try {
const file = join(directory, "balance")
await writeFile(`${file}.lock`, String(process.pid))
const recent = new Date(Date.now() - 11 * 60 * 1000)
await utimes(`${file}.lock`, recent, recent)
await assert.rejects(take(file, { timeout: 50, poll: 10 }), /retry shortly/)
const stale = new Date(Date.now() - 16 * 60 * 1000)
await utimes(`${file}.lock`, stale, stale)
const release = await take(file, { timeout: 500, poll: 20 })
assert.equal(await readFile(`${file}.lock`, "utf8"), String(process.pid))
await release()
} finally {
Object.defineProperty(process, "platform", { value: original, configurable: true })
await rm(directory, { recursive: true, force: true })
}
})
test("a lock left by a process that has ended is not waited on", async () => {
const directory = await mkdtemp(join(tmpdir(), "vertex-lock-"))
try {
const file = join(directory, "balance")
// A pid that cannot be running, as a crash or a reboot leaves behind.
await writeFile(`${file}.lock`, String(2 ** 22 + 7))
const release = await take(file, { timeout: 500, poll: 20 })
await release()
// A lock that is still empty is one just being created, not an abandoned
// one, and it is waited on rather than swept away.
await writeFile(`${file}.lock`, "")
await assert.rejects(take(file, { timeout: 300, poll: 20 }), /retry shortly/)
assert.equal(await readFile(`${file}.lock`, "utf8"), "")
} finally { await rm(directory, { recursive: true, force: true }) }
})
test("Linux keeps a live holder even when its lock is over fifteen minutes old", { skip: process.platform !== "linux" }, async () => {
const directory = await mkdtemp(join(tmpdir(), "vertex-lock-"))
const original = Date.now
let release
try {
const file = join(directory, "balance")
release = await take(file)
// Advance wall time without placing the lock before this process started.
Date.now = () => original() + 16 * 60 * 1000
await assert.rejects(take(file, { timeout: 50, poll: 10 }), /retry shortly/)
assert.equal(await readFile(`${file}.lock`, "utf8"), String(process.pid))
} finally {
Date.now = original
await release?.()
await rm(directory, { recursive: true, force: true })
}
})