Refine version command with async update cache and background check
This commit is contained in:
1 parent
68a858b4b9
commit
6ad01893b3
6 files changed
+410
-5
No files matched your search
@@ -3,6 +3,7 @@ import { tmpdir } from "node:os";
|
||||
import { join } from "node:path";
|
||||
import { describe, expect, test } from "bun:test";
|
||||
import { runCli } from "./runner.js";
|
||||
import { saveUpdateCache, UPDATE_CACHE_FILE_ENV } from "../core/update-cache.js";
|
||||
|
||||
describe("CLI runner", () => {
|
||||
test("returns version output without touching the process lifecycle", async () => {
|
||||
@@ -98,4 +99,49 @@ describe("CLI runner", () => {
|
||||
expect(result.stderr).not.toContain("is an alias for 'seal-use update'");
|
||||
expect(JSON.parse(result.stdout)).toMatchObject({ updated: false, targetVersion: "0.3.3" });
|
||||
});
|
||||
|
||||
test("version output enriches with cache and alerts on newer version via stderr", async () => {
|
||||
const tempDir = mkdtempSync(join(tmpdir(), "seal-use-version-test-"));
|
||||
const tempCacheFile = join(tempDir, "update-cache.json");
|
||||
const prevEnv = process.env[UPDATE_CACHE_FILE_ENV];
|
||||
process.env[UPDATE_CACHE_FILE_ENV] = tempCacheFile;
|
||||
|
||||
try {
|
||||
// 1. Without cache: plain version output
|
||||
const coldResult = await runCli(["version"]);
|
||||
expect(coldResult.exitCode).toBe(0);
|
||||
expect(coldResult.stderr).toBe("");
|
||||
const coldJson = JSON.parse(coldResult.stdout);
|
||||
expect(coldJson.name).toBe("seal-use");
|
||||
expect(coldJson.latestVersion).toBeUndefined();
|
||||
|
||||
// 2. With cache (newer version available)
|
||||
saveUpdateCache(
|
||||
{
|
||||
latestVersion: "99.0.0",
|
||||
publishedAt: "2026-09-20T12:00:00Z",
|
||||
releaseUrl: "https://github.com/yunyingk/seal-use/releases/tag/v99.0.0"
|
||||
},
|
||||
tempCacheFile
|
||||
);
|
||||
|
||||
const warmResult = await runCli(["version"]);
|
||||
expect(warmResult.exitCode).toBe(0);
|
||||
expect(warmResult.stderr).toContain("💡 发现新版本可用:");
|
||||
expect(warmResult.stderr).toContain("99.0.0");
|
||||
expect(warmResult.stderr).toContain("👉 Next action: seal-use update");
|
||||
|
||||
const warmJson = JSON.parse(warmResult.stdout);
|
||||
expect(warmJson.latestVersion).toBe("99.0.0");
|
||||
expect(warmJson.updateAvailable).toBe(true);
|
||||
expect(typeof warmJson.lastCheckedAt).toBe("string");
|
||||
} finally {
|
||||
if (prevEnv !== undefined) {
|
||||
process.env[UPDATE_CACHE_FILE_ENV] = prevEnv;
|
||||
} else {
|
||||
delete process.env[UPDATE_CACHE_FILE_ENV];
|
||||
}
|
||||
rmSync(tempDir, { recursive: true, force: true });
|
||||
}
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user