refactor: improve test isolation and DI for cache/port-utils/resolve-file-uri
- connected-providers-cache: extract factory pattern (createConnectedProvidersCacheStore) for testable cache dir injection - port-utils.test: environment-independent tests with real socket probing and contiguous port detection - resolve-file-uri.test: mock homedir instead of touching real home directory - github-triage: update SKILL.md
This commit is contained in:
@@ -1,45 +1,30 @@
|
||||
/// <reference types="bun-types" />
|
||||
|
||||
import { beforeAll, beforeEach, afterEach, describe, expect, mock, test } from "bun:test"
|
||||
import { beforeEach, afterEach, describe, expect, test } from "bun:test"
|
||||
|
||||
import { existsSync, mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from "node:fs"
|
||||
import { tmpdir } from "node:os"
|
||||
import { join } from "node:path"
|
||||
import * as dataPath from "./data-path"
|
||||
import {
|
||||
createConnectedProvidersCacheStore,
|
||||
} from "./connected-providers-cache"
|
||||
|
||||
let fakeUserCacheRoot = ""
|
||||
let testCacheDir = ""
|
||||
let moduleImportCounter = 0
|
||||
|
||||
const getOmoOpenCodeCacheDirMock = mock(() => testCacheDir)
|
||||
|
||||
let updateConnectedProvidersCache: typeof import("./connected-providers-cache").updateConnectedProvidersCache
|
||||
let readProviderModelsCache: typeof import("./connected-providers-cache").readProviderModelsCache
|
||||
|
||||
async function prepareConnectedProvidersCacheTestModule(): Promise<void> {
|
||||
testCacheDir = mkdtempSync(join(tmpdir(), "connected-providers-cache-test-"))
|
||||
getOmoOpenCodeCacheDirMock.mockClear()
|
||||
mock.module("./data-path", () => ({
|
||||
getOmoOpenCodeCacheDir: getOmoOpenCodeCacheDirMock,
|
||||
}))
|
||||
moduleImportCounter += 1
|
||||
;({ updateConnectedProvidersCache, readProviderModelsCache } = await import(`./connected-providers-cache?test=${moduleImportCounter}`))
|
||||
}
|
||||
let testCacheStore: ReturnType<typeof createConnectedProvidersCacheStore>
|
||||
|
||||
describe("updateConnectedProvidersCache", () => {
|
||||
beforeAll(() => {
|
||||
mock.restore()
|
||||
})
|
||||
|
||||
beforeEach(async () => {
|
||||
mock.restore()
|
||||
await prepareConnectedProvidersCacheTestModule()
|
||||
beforeEach(() => {
|
||||
fakeUserCacheRoot = mkdtempSync(join(tmpdir(), "connected-providers-user-cache-"))
|
||||
testCacheDir = join(fakeUserCacheRoot, "oh-my-opencode")
|
||||
testCacheStore = createConnectedProvidersCacheStore(() => testCacheDir)
|
||||
})
|
||||
|
||||
afterEach(() => {
|
||||
mock.restore()
|
||||
if (existsSync(testCacheDir)) {
|
||||
rmSync(testCacheDir, { recursive: true, force: true })
|
||||
if (existsSync(fakeUserCacheRoot)) {
|
||||
rmSync(fakeUserCacheRoot, { recursive: true, force: true })
|
||||
}
|
||||
fakeUserCacheRoot = ""
|
||||
testCacheDir = ""
|
||||
})
|
||||
|
||||
@@ -76,10 +61,10 @@ describe("updateConnectedProvidersCache", () => {
|
||||
}
|
||||
|
||||
//#when
|
||||
await updateConnectedProvidersCache(mockClient)
|
||||
await testCacheStore.updateConnectedProvidersCache(mockClient)
|
||||
|
||||
//#then
|
||||
const cache = readProviderModelsCache()
|
||||
const cache = testCacheStore.readProviderModelsCache()
|
||||
expect(cache).not.toBeNull()
|
||||
expect(cache!.connected).toEqual(["openai", "anthropic"])
|
||||
expect(cache!.models).toEqual({
|
||||
@@ -109,10 +94,10 @@ describe("updateConnectedProvidersCache", () => {
|
||||
}
|
||||
|
||||
//#when
|
||||
await updateConnectedProvidersCache(mockClient)
|
||||
await testCacheStore.updateConnectedProvidersCache(mockClient)
|
||||
|
||||
//#then
|
||||
const cache = readProviderModelsCache()
|
||||
const cache = testCacheStore.readProviderModelsCache()
|
||||
expect(cache).not.toBeNull()
|
||||
expect(cache!.models).toEqual({})
|
||||
})
|
||||
@@ -130,10 +115,10 @@ describe("updateConnectedProvidersCache", () => {
|
||||
}
|
||||
|
||||
//#when
|
||||
await updateConnectedProvidersCache(mockClient)
|
||||
await testCacheStore.updateConnectedProvidersCache(mockClient)
|
||||
|
||||
//#then
|
||||
const cache = readProviderModelsCache()
|
||||
const cache = testCacheStore.readProviderModelsCache()
|
||||
expect(cache).not.toBeNull()
|
||||
expect(cache!.models).toEqual({})
|
||||
})
|
||||
@@ -143,25 +128,44 @@ describe("updateConnectedProvidersCache", () => {
|
||||
const mockClient = {}
|
||||
|
||||
//#when
|
||||
await updateConnectedProvidersCache(mockClient)
|
||||
await testCacheStore.updateConnectedProvidersCache(mockClient)
|
||||
|
||||
//#then
|
||||
const cache = readProviderModelsCache()
|
||||
const cache = testCacheStore.readProviderModelsCache()
|
||||
expect(cache).toBeNull()
|
||||
})
|
||||
|
||||
test("does not remove the user's real cache directory during test setup", async () => {
|
||||
test("does not remove unrelated files in the cache directory", async () => {
|
||||
//#given
|
||||
const realCacheDir = join(dataPath.getCacheDir(), "oh-my-opencode")
|
||||
const realCacheDir = join(fakeUserCacheRoot, "oh-my-opencode")
|
||||
const sentinelPath = join(realCacheDir, "connected-providers-cache.test-sentinel.json")
|
||||
mkdirSync(realCacheDir, { recursive: true })
|
||||
writeFileSync(sentinelPath, JSON.stringify({ keep: true }))
|
||||
|
||||
const mockClient = {
|
||||
provider: {
|
||||
list: async () => ({
|
||||
data: {
|
||||
connected: ["openai"],
|
||||
all: [
|
||||
{
|
||||
id: "openai",
|
||||
models: {
|
||||
"gpt-5.4": { id: "gpt-5.4" },
|
||||
},
|
||||
},
|
||||
],
|
||||
},
|
||||
}),
|
||||
},
|
||||
}
|
||||
|
||||
try {
|
||||
//#when
|
||||
await prepareConnectedProvidersCacheTestModule()
|
||||
await testCacheStore.updateConnectedProvidersCache(mockClient)
|
||||
|
||||
//#then
|
||||
expect(testCacheStore.readConnectedProvidersCache()).toEqual(["openai"])
|
||||
expect(existsSync(sentinelPath)).toBe(true)
|
||||
expect(readFileSync(sentinelPath, "utf-8")).toBe(JSON.stringify({ keep: true }))
|
||||
} finally {
|
||||
|
||||
Reference in New Issue
Block a user