{"draft":null,"from":2,"to":318,"files":[{"path":"client/test/client-version.test.ts","added":95,"removed":0,"diff":"--- /dev/null\n+++ b/client/test/client-version.test.ts\n@@ -0,0 +1,95 @@\n+// An npm install of abso says when npm has a later version, asking npm at most\n+// once a day. The registry here is a stand-in that counts how often it is asked.\n+\n+import assert from \"node:assert/strict\";\n+import { existsSync, mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from \"node:fs\";\n+import { createServer, type Server } from \"node:http\";\n+import type { AddressInfo } from \"node:net\";\n+import { tmpdir } from \"node:os\";\n+import { join } from \"node:path\";\n+import { after, before, beforeEach, describe, it } from \"node:test\";\n+import { newerClientNote } from \"../src/client-version.ts\";\n+\n+const DAY = 24 * 60 * 60 * 1000;\n+const stamp = globalThis as { __ABSO_PACKAGE_VERSION__?: string };\n+let registry: Server;\n+let registryUrl: string;\n+let latest = \"0.67.0\";\n+let asked = 0;\n+let home: string;\n+\n+before(async () => {\n+\tregistry = createServer((_req, res) => {\n+\t\tasked++;\n+\t\tres\n+\t\t\t.writeHead(200, { \"content-type\": \"application/json\" })\n+\t\t\t.end(JSON.stringify({ name: \"@abso-cli/abso\", version: latest }));\n+\t});\n+\t// As fake-server.ts: never close an idle kept-alive connection, or under load the client can reuse one as it closes\n+\t// and fail with ECONNRESET (abso 410).\n+\tregistry.keepAliveTimeout = 0;\n+\tawait new Promise<void>((done) => registry.listen(0, \"127.0.0.1\", done));\n+\tregistryUrl = `http://127.0.0.1:${(registry.address() as AddressInfo).port}/@abso-cli/abso/latest`;\n+});\n+after(() => {\n+\tregistry.close();\n+\tdelete stamp.__ABSO_PACKAGE_VERSION__;\n+});\n+beforeEach(() => {\n+\thome = mkdtempSync(join(tmpdir(), \"abso-test-home-\"));\n+\tasked = 0;\n+\tlatest = \"0.67.0\";\n+});\n+\n+describe(\"newerClientNote for the npm package\", () => {\n+\tit(\"says so when npm has a later version, and asks npm once a day\", async () => {\n+\t\tstamp.__ABSO_PACKAGE_VERSION__ = \"0.66.0\";\n+\t\tconst now = 1_000_000_000_000;\n+\t\tconst note = await newerClientNote(\"http://unused.test\", { home, registry: registryUrl, now });\n+\t\tassert.equal(note, \"abso 0.67.0 is out and this is 0.66.0: npm install -g @abso-cli/abso updates it.\");\n+\t\tassert.equal(asked, 1);\n+\t\tassert.ok(existsSync(join(home, \".abso\", \"npm-latest.json\")));\n+\n+\t\tlatest = \"0.68.0\";\n+\t\tconst kept = await newerClientNote(\"http://unused.test\", { home, registry: registryUrl, now: now + DAY - 1 });\n+\t\tassert.match(kept ?? \"\", /^abso 0\\.67\\.0 is out/, \"within a day the kept answer is used\");\n+\t\tassert.equal(asked, 1);\n+\n+\t\tconst fresh = await newerClientNote(\"http://unused.test\", { home, registry: registryUrl, now: now + DAY });\n+\t\tassert.match(fresh ?? \"\", /^abso 0\\.68\\.0 is out/);\n+\t\tassert.equal(asked, 2);\n+\t\trmSync(home, { recursive: true, force: true });\n+\t});\n+\n+\tit(\"says nothing when this is npm's latest or later, and compares numbers, not text\", async () => {\n+\t\tstamp.__ABSO_PACKAGE_VERSION__ = \"0.67.0\";\n+\t\tassert.equal(await newerClientNote(\"http://unused.test\", { home, registry: registryUrl, now: 1 }), undefined);\n+\t\trmSync(home, { recursive: true, force: true });\n+\n+\t\thome = mkdtempSync(join(tmpdir(), \"abso-test-home-\"));\n+\t\tstamp.__ABSO_PACKAGE_VERSION__ = \"0.100.0\";\n+\t\tlatest = \"0.99.0\";\n+\t\tassert.equal(await newerClientNote(\"http://unused.test\", { home, registry: registryUrl, now: 1 }), undefined);\n+\t\trmSync(home, { recursive: true, force: true });\n+\t});\n+\n+\tit(\"says nothing when npm can't be asked, and keeps no answer\", async () => {\n+\t\tstamp.__ABSO_PACKAGE_VERSION__ = \"0.66.0\";\n+\t\tconst note = await newerClientNote(\"http://unused.test\", { home, registry: \"http://127.0.0.1:1/latest\", now: 1 });\n+\t\tassert.equal(note, undefined);\n+\t\tassert.equal(existsSync(join(home, \".abso\", \"npm-latest.json\")), false);\n+\t\trmSync(home, { recursive: true, force: true });\n+\t});\n+\n+\tit(\"asks npm again when the kept answer isn't JSON, and keeps the new one\", async () => {\n+\t\tstamp.__ABSO_PACKAGE_VERSION__ = \"0.66.0\";\n+\t\tconst file = join(home, \".abso\", \"npm-latest.json\");\n+\t\tmkdirSync(join(home, \".abso\"), { recursive: true });\n+\t\twriteFileSync(file, \"not json\");\n+\t\tconst note = await newerClientNote(\"http://unused.test\", { home, registry: registryUrl, now: 5 });\n+\t\tassert.match(note ?? \"\", /^abso 0\\.67\\.0 is out/);\n+\t\tassert.equal(asked, 1);\n+\t\tassert.deepEqual(JSON.parse(readFileSync(file, \"utf8\")), { checked: 5, latest: \"0.67.0\" });\n+\t\trmSync(home, { recursive: true, force: true });\n+\t});\n+});"}],"unmatched":[]}