test(hooks): update multiple hook test suites
Ultraworked with [Sisyphus](https://github.com/code-yeongyu/oh-my-openagent) Co-authored-by: Sisyphus <clio-agent@sisyphuslabs.ai>
This commit is contained in:
@@ -2,59 +2,40 @@ import { afterAll, afterEach, beforeEach, describe, expect, it, mock } from "bun
|
||||
import { existsSync, mkdirSync, readFileSync, rmSync, writeFileSync } from "node:fs"
|
||||
import { join } from "node:path"
|
||||
import type { PluginEntryInfo } from "../auto-update-checker/checker/plugin-entry"
|
||||
import { CACHE_DIR } from "../auto-update-checker/constants"
|
||||
|
||||
const TEST_CACHE_DIR = join(import.meta.dir, "__test-sync-cache__")
|
||||
const CACHE_PACKAGES_DIR = CACHE_DIR
|
||||
const CACHE_PACKAGE_JSON_PATH = join(CACHE_PACKAGES_DIR, "package.json")
|
||||
const ORIGINAL_CACHE_PACKAGE_JSON = existsSync(CACHE_PACKAGE_JSON_PATH)
|
||||
? readFileSync(CACHE_PACKAGE_JSON_PATH, "utf-8")
|
||||
: null
|
||||
|
||||
let importCounter = 0
|
||||
|
||||
// Capture real modules BEFORE mocking
|
||||
const _realConstants = require("../auto-update-checker/constants")
|
||||
const _realLogger = require("../../shared/logger")
|
||||
const _realNodeFs = require("node:fs")
|
||||
|
||||
async function importFreshSyncPackageJsonModule(): Promise<typeof import("../auto-update-checker/checker/sync-package-json")> {
|
||||
mock.module("../auto-update-checker/constants", () => ({
|
||||
CACHE_DIR: TEST_CACHE_DIR,
|
||||
PACKAGE_NAME: "oh-my-opencode",
|
||||
NPM_REGISTRY_URL: "https://registry.npmjs.org/-/package/oh-my-opencode/dist-tags",
|
||||
NPM_FETCH_TIMEOUT: 5000,
|
||||
VERSION_FILE: join(TEST_CACHE_DIR, "version"),
|
||||
INSTALLED_PACKAGE_JSON: join(TEST_CACHE_DIR, "node_modules", "oh-my-opencode", "package.json"),
|
||||
getUserConfigDir: () => "/tmp/opencode-config",
|
||||
getUserOpencodeConfig: () => "/tmp/opencode-config/opencode.json",
|
||||
getUserOpencodeConfigJsonc: () => "/tmp/opencode-config/opencode.jsonc",
|
||||
getWindowsAppdataDir: () => null,
|
||||
}))
|
||||
|
||||
mock.module("../../shared/logger", () => ({
|
||||
log: () => {},
|
||||
}))
|
||||
|
||||
const syncPackageJsonModule = await import(`../auto-update-checker/checker/sync-package-json?test=${importCounter++}`)
|
||||
mock.restore()
|
||||
return syncPackageJsonModule
|
||||
return import(`../auto-update-checker/checker/sync-package-json?test=${importCounter++}`)
|
||||
}
|
||||
|
||||
function resetTestCache(currentVersion = "3.10.0"): void {
|
||||
if (existsSync(TEST_CACHE_DIR)) {
|
||||
rmSync(TEST_CACHE_DIR, { recursive: true, force: true })
|
||||
}
|
||||
|
||||
mkdirSync(TEST_CACHE_DIR, { recursive: true })
|
||||
mkdirSync(CACHE_PACKAGES_DIR, { recursive: true })
|
||||
writeFileSync(
|
||||
join(TEST_CACHE_DIR, "package.json"),
|
||||
CACHE_PACKAGE_JSON_PATH,
|
||||
JSON.stringify({ dependencies: { "oh-my-opencode": currentVersion, other: "1.0.0" } }, null, 2)
|
||||
)
|
||||
}
|
||||
|
||||
function cleanupTestCache(): void {
|
||||
if (existsSync(TEST_CACHE_DIR)) {
|
||||
rmSync(TEST_CACHE_DIR, { recursive: true, force: true })
|
||||
if (existsSync(CACHE_PACKAGE_JSON_PATH)) {
|
||||
rmSync(CACHE_PACKAGE_JSON_PATH, { force: true })
|
||||
}
|
||||
}
|
||||
|
||||
function readCachePackageJsonVersion(): string | undefined {
|
||||
const content = readFileSync(join(TEST_CACHE_DIR, "package.json"), "utf-8")
|
||||
const content = readFileSync(CACHE_PACKAGE_JSON_PATH, "utf-8")
|
||||
const pkg = JSON.parse(content) as { dependencies?: Record<string, string> }
|
||||
return pkg.dependencies?.["oh-my-opencode"]
|
||||
}
|
||||
@@ -65,6 +46,7 @@ describe("syncCachePackageJsonToIntent", () => {
|
||||
})
|
||||
|
||||
afterEach(() => {
|
||||
mock.restore()
|
||||
cleanupTestCache()
|
||||
})
|
||||
|
||||
@@ -170,9 +152,9 @@ describe("syncCachePackageJsonToIntent", () => {
|
||||
describe("#given plugin not in cache package.json dependencies", () => {
|
||||
it("#then adds the plugin dependency and preserves existing dependencies", async () => {
|
||||
cleanupTestCache()
|
||||
mkdirSync(TEST_CACHE_DIR, { recursive: true })
|
||||
mkdirSync(CACHE_PACKAGES_DIR, { recursive: true })
|
||||
writeFileSync(
|
||||
join(TEST_CACHE_DIR, "package.json"),
|
||||
join(CACHE_PACKAGES_DIR, "package.json"),
|
||||
JSON.stringify({ dependencies: { other: "1.0.0" } }, null, 2)
|
||||
)
|
||||
|
||||
@@ -190,10 +172,10 @@ describe("syncCachePackageJsonToIntent", () => {
|
||||
expect(result.synced).toBe(true)
|
||||
expect(result.error).toBeNull()
|
||||
|
||||
const content = readFileSync(join(TEST_CACHE_DIR, "package.json"), "utf-8")
|
||||
const pkg = JSON.parse(content) as { dependencies?: Record<string, string> }
|
||||
expect(pkg.dependencies?.["oh-my-opencode"]).toBe("latest")
|
||||
expect(pkg.dependencies?.other).toBe("1.0.0")
|
||||
const content = readFileSync(join(CACHE_PACKAGES_DIR, "package.json"), "utf-8")
|
||||
const pkg = JSON.parse(content) as { dependencies?: Record<string, string> }
|
||||
expect(pkg.dependencies?.["oh-my-opencode"]).toBe("latest")
|
||||
expect(pkg.dependencies?.other).toBe("1.0.0")
|
||||
})
|
||||
})
|
||||
|
||||
@@ -233,17 +215,17 @@ describe("syncCachePackageJsonToIntent", () => {
|
||||
expect(result.synced).toBe(true)
|
||||
expect(result.error).toBeNull()
|
||||
|
||||
const content = readFileSync(join(TEST_CACHE_DIR, "package.json"), "utf-8")
|
||||
const pkg = JSON.parse(content) as { dependencies?: Record<string, string> }
|
||||
expect(pkg.dependencies?.["other"]).toBe("1.0.0")
|
||||
const content = readFileSync(join(CACHE_PACKAGES_DIR, "package.json"), "utf-8")
|
||||
const pkg = JSON.parse(content) as { dependencies?: Record<string, string> }
|
||||
expect(pkg.dependencies?.["other"]).toBe("1.0.0")
|
||||
})
|
||||
})
|
||||
|
||||
describe("#given malformed JSON in cache package.json", () => {
|
||||
it("#then returns parse_error", async () => {
|
||||
cleanupTestCache()
|
||||
mkdirSync(TEST_CACHE_DIR, { recursive: true })
|
||||
writeFileSync(join(TEST_CACHE_DIR, "package.json"), "{ invalid json }")
|
||||
mkdirSync(CACHE_PACKAGES_DIR, { recursive: true })
|
||||
writeFileSync(join(CACHE_PACKAGES_DIR, "package.json"), "{ invalid json }")
|
||||
|
||||
const { syncCachePackageJsonToIntent } = await importFreshSyncPackageJsonModule()
|
||||
|
||||
@@ -264,9 +246,9 @@ describe("syncCachePackageJsonToIntent", () => {
|
||||
describe("#given write permission denied", () => {
|
||||
it("#then returns write_error", async () => {
|
||||
cleanupTestCache()
|
||||
mkdirSync(TEST_CACHE_DIR, { recursive: true })
|
||||
mkdirSync(CACHE_PACKAGES_DIR, { recursive: true })
|
||||
writeFileSync(
|
||||
join(TEST_CACHE_DIR, "package.json"),
|
||||
join(CACHE_PACKAGES_DIR, "package.json"),
|
||||
JSON.stringify({ dependencies: { "oh-my-opencode": "3.10.0" } }, null, 2)
|
||||
)
|
||||
|
||||
@@ -309,9 +291,9 @@ describe("syncCachePackageJsonToIntent", () => {
|
||||
describe("#given rename fails after successful write", () => {
|
||||
it("#then returns write_error and cleans up temp file", async () => {
|
||||
cleanupTestCache()
|
||||
mkdirSync(TEST_CACHE_DIR, { recursive: true })
|
||||
mkdirSync(CACHE_PACKAGES_DIR, { recursive: true })
|
||||
writeFileSync(
|
||||
join(TEST_CACHE_DIR, "package.json"),
|
||||
join(CACHE_PACKAGES_DIR, "package.json"),
|
||||
JSON.stringify({ dependencies: { "oh-my-opencode": "3.10.0" } }, null, 2)
|
||||
)
|
||||
|
||||
@@ -360,8 +342,11 @@ describe("syncCachePackageJsonToIntent", () => {
|
||||
})
|
||||
|
||||
afterAll(() => {
|
||||
mock.module("../auto-update-checker/constants", () => _realConstants)
|
||||
mock.module("../../shared/logger", () => _realLogger)
|
||||
mock.module("node:fs", () => _realNodeFs)
|
||||
if (ORIGINAL_CACHE_PACKAGE_JSON === null) {
|
||||
cleanupTestCache()
|
||||
} else {
|
||||
mkdirSync(CACHE_PACKAGES_DIR, { recursive: true })
|
||||
writeFileSync(CACHE_PACKAGE_JSON_PATH, ORIGINAL_CACHE_PACKAGE_JSON)
|
||||
}
|
||||
mock.restore()
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user