diff --git a/src/version.test.ts b/src/version.test.ts index 97fbbcde3f3..75bcedb1a13 100644 --- a/src/version.test.ts +++ b/src/version.test.ts @@ -1,8 +1,8 @@ import fs from "node:fs/promises"; import path from "node:path"; import { pathToFileURL } from "node:url"; -import { describe, expect, it } from "vitest"; -import { withTempDir } from "./test-helpers/temp-dir.js"; +import { afterAll, beforeAll, describe, expect, it } from "vitest"; +import { createSuiteTempRootTracker } from "./test-helpers/temp-dir.js"; import { VERSION, readVersionFromBuildInfoForModuleUrl, @@ -14,10 +14,24 @@ import { resolveVersionFromModuleUrl, } from "./version.js"; +const versionFixtureRoot = createSuiteTempRootTracker({ prefix: "openclaw-version-" }); + +beforeAll(async () => { + await versionFixtureRoot.setup(); +}); + +afterAll(async () => { + await versionFixtureRoot.cleanup(); +}); + function moduleUrlFrom(root: string, relativePath: string): string { return pathToFileURL(path.join(root, relativePath)).href; } +async function withVersionFixtureDir(run: (root: string) => Promise): Promise { + return await run(await versionFixtureRoot.make("case")); +} + async function ensureModuleFixture(root: string, relativePath = "dist/plugin-sdk/index.js") { await fs.mkdir(path.dirname(path.join(root, relativePath)), { recursive: true }); return moduleUrlFrom(root, relativePath); @@ -37,7 +51,7 @@ function expectVersionMetadataToBeMissing(moduleUrl: string) { describe("version resolution", () => { it("resolves package version from nested dist/plugin-sdk module URL", async () => { - await withTempDir({ prefix: "openclaw-version-" }, async (root) => { + await withVersionFixtureDir(async (root) => { await writeJsonFixture(root, "package.json", { name: "openclaw", version: "1.2.3" }); const moduleUrl = await ensureModuleFixture(root); expect(readVersionFromPackageJsonForModuleUrl(moduleUrl)).toBe("1.2.3"); @@ -46,7 +60,7 @@ describe("version resolution", () => { }); it("ignores unrelated nearby package.json files", async () => { - await withTempDir({ prefix: "openclaw-version-" }, async (root) => { + await withVersionFixtureDir(async (root) => { await writeJsonFixture(root, "package.json", { name: "openclaw", version: "2.3.4" }); await writeJsonFixture(root, "dist/package.json", { name: "other-package", @@ -58,7 +72,7 @@ describe("version resolution", () => { }); it("falls back to build-info when package metadata is unavailable", async () => { - await withTempDir({ prefix: "openclaw-version-" }, async (root) => { + await withVersionFixtureDir(async (root) => { await writeJsonFixture(root, "build-info.json", { version: "4.5.6" }); const moduleUrl = await ensureModuleFixture(root); expect(readVersionFromPackageJsonForModuleUrl(moduleUrl)).toBeNull(); @@ -68,14 +82,14 @@ describe("version resolution", () => { }); it("returns null when no version metadata exists", async () => { - await withTempDir({ prefix: "openclaw-version-" }, async (root) => { + await withVersionFixtureDir(async (root) => { const moduleUrl = await ensureModuleFixture(root); expectVersionMetadataToBeMissing(moduleUrl); }); }); it("ignores non-openclaw package and blank build-info versions", async () => { - await withTempDir({ prefix: "openclaw-version-" }, async (root) => { + await withVersionFixtureDir(async (root) => { await writeJsonFixture(root, "package.json", { name: "other-package", version: "9.9.9" }); await writeJsonFixture(root, "build-info.json", { version: " " }); const moduleUrl = await ensureModuleFixture(root); @@ -90,7 +104,7 @@ describe("version resolution", () => { }); it("resolves binary version with explicit precedence", async () => { - await withTempDir({ prefix: "openclaw-version-" }, async (root) => { + await withVersionFixtureDir(async (root) => { await writeJsonFixture(root, "package.json", { name: "openclaw", version: "2.3.4" }); const moduleUrl = await ensureModuleFixture(root); expect(