test: isolate flaky shared-state tests

This commit is contained in:
YeonGyu-Kim
2026-04-04 19:20:40 +09:00
parent 5213525a95
commit ad025ee0f8
3 changed files with 314 additions and 237 deletions
+13 -1
View File
@@ -1,6 +1,6 @@
/// <reference types="bun-types" /> /// <reference types="bun-types" />
import { describe, test, expect, beforeEach, afterEach, spyOn } from "bun:test" import { describe, test, expect, beforeEach, afterEach, spyOn, mock } from "bun:test"
import { createBuiltinAgents } from "./builtin-agents" import { createBuiltinAgents } from "./builtin-agents"
import type { AgentConfig } from "@opencode-ai/sdk" import type { AgentConfig } from "@opencode-ai/sdk"
import { clearSkillCache } from "../features/opencode-skill-loader/skill-content" import { clearSkillCache } from "../features/opencode-skill-loader/skill-content"
@@ -10,6 +10,18 @@ import * as shared from "../shared"
const TEST_DEFAULT_MODEL = "anthropic/claude-opus-4-6" const TEST_DEFAULT_MODEL = "anthropic/claude-opus-4-6"
beforeEach(() => {
mock.restore()
clearSkillCache()
connectedProvidersCache._resetMemCacheForTesting()
})
afterEach(() => {
clearSkillCache()
connectedProvidersCache._resetMemCacheForTesting()
mock.restore()
})
describe("createBuiltinAgents with model overrides", () => { describe("createBuiltinAgents with model overrides", () => {
test("Sisyphus with default model has thinking config when all models available", async () => { test("Sisyphus with default model has thinking config when all models available", async () => {
// #given // #given
+221 -185
View File
@@ -1,148 +1,171 @@
/// <reference types="bun-types" /> /// <reference types="bun-types" />
import { beforeEach, afterEach, describe, expect, test } from "bun:test" import { describe, expect, test } from "bun:test"
import { existsSync, mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from "node:fs" import { existsSync, mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from "node:fs"
import { tmpdir } from "node:os" import { tmpdir } from "node:os"
import { join } from "node:path" import { join } from "node:path"
import { import { createConnectedProvidersCacheStore, findProviderModelMetadata } from "./connected-providers-cache"
createConnectedProvidersCacheStore,
findProviderModelMetadata,
} from "./connected-providers-cache"
let fakeUserCacheRoot = "" function createTestCacheContext() {
let testCacheDir = "" const fakeUserCacheRoot = mkdtempSync(join(tmpdir(), "connected-providers-user-cache-"))
let testCacheStore: ReturnType<typeof createConnectedProvidersCacheStore> const testCacheDir = join(fakeUserCacheRoot, "oh-my-opencode")
const testCacheStore = createConnectedProvidersCacheStore(() => testCacheDir)
return {
fakeUserCacheRoot,
testCacheDir,
testCacheStore,
}
}
function cleanupTestCacheContext(fakeUserCacheRoot: string): void {
if (existsSync(fakeUserCacheRoot)) {
rmSync(fakeUserCacheRoot, { recursive: true, force: true })
}
}
describe("updateConnectedProvidersCache", () => { describe("updateConnectedProvidersCache", () => {
beforeEach(() => {
fakeUserCacheRoot = mkdtempSync(join(tmpdir(), "connected-providers-user-cache-"))
testCacheDir = join(fakeUserCacheRoot, "oh-my-opencode")
testCacheStore = createConnectedProvidersCacheStore(() => testCacheDir)
})
afterEach(() => {
if (existsSync(fakeUserCacheRoot)) {
rmSync(fakeUserCacheRoot, { recursive: true, force: true })
}
fakeUserCacheRoot = ""
testCacheDir = ""
})
test("extracts models from provider.list().all response", async () => { test("extracts models from provider.list().all response", async () => {
//#given const { testCacheStore, fakeUserCacheRoot } = createTestCacheContext()
const mockClient = {
provider: { try {
list: async () => ({ //#given
data: { const mockClient = {
connected: ["openai", "anthropic"], provider: {
all: [ list: async () => ({
{ data: {
id: "openai", connected: ["openai", "anthropic"],
name: "OpenAI", all: [
env: [], {
models: { id: "openai",
"gpt-5.3-codex": { id: "gpt-5.3-codex", name: "GPT-5.3 Codex" }, name: "OpenAI",
"gpt-5.4": { id: "gpt-5.4", name: "GPT-5.4" }, env: [],
models: {
"gpt-5.3-codex": { id: "gpt-5.3-codex", name: "GPT-5.3 Codex" },
"gpt-5.4": { id: "gpt-5.4", name: "GPT-5.4" },
},
}, },
}, {
{ id: "anthropic",
id: "anthropic", name: "Anthropic",
name: "Anthropic", env: [],
env: [], models: {
models: { "claude-opus-4-6": { id: "claude-opus-4-6", name: "Claude Opus 4.6" },
"claude-opus-4-6": { id: "claude-opus-4-6", name: "Claude Opus 4.6" }, "claude-sonnet-4-6": { id: "claude-sonnet-4-6", name: "Claude Sonnet 4.6" },
"claude-sonnet-4-6": { id: "claude-sonnet-4-6", name: "Claude Sonnet 4.6" }, },
}, },
}, ],
], },
}, }),
}), },
}, }
//#when
await testCacheStore.updateConnectedProvidersCache(mockClient)
//#then
const cache = testCacheStore.readProviderModelsCache()
expect(cache).not.toBeNull()
expect(cache!.connected).toEqual(["openai", "anthropic"])
expect(cache!.models).toEqual({
openai: [
{ id: "gpt-5.3-codex", name: "GPT-5.3 Codex" },
{ id: "gpt-5.4", name: "GPT-5.4" },
],
anthropic: [
{ id: "claude-opus-4-6", name: "Claude Opus 4.6" },
{ id: "claude-sonnet-4-6", name: "Claude Sonnet 4.6" },
],
})
} finally {
cleanupTestCacheContext(fakeUserCacheRoot)
} }
//#when
await testCacheStore.updateConnectedProvidersCache(mockClient)
//#then
const cache = testCacheStore.readProviderModelsCache()
expect(cache).not.toBeNull()
expect(cache!.connected).toEqual(["openai", "anthropic"])
expect(cache!.models).toEqual({
openai: [
{ id: "gpt-5.3-codex", name: "GPT-5.3 Codex" },
{ id: "gpt-5.4", name: "GPT-5.4" },
],
anthropic: [
{ id: "claude-opus-4-6", name: "Claude Opus 4.6" },
{ id: "claude-sonnet-4-6", name: "Claude Sonnet 4.6" },
],
})
}) })
test("writes empty models when provider has no models", async () => { test("writes empty models when provider has no models", async () => {
//#given const { testCacheStore, fakeUserCacheRoot } = createTestCacheContext()
const mockClient = {
provider: { try {
list: async () => ({ //#given
data: { const mockClient = {
connected: ["empty-provider"], provider: {
all: [ list: async () => ({
{ data: {
id: "empty-provider", connected: ["empty-provider"],
name: "Empty", all: [
env: [], {
models: {}, id: "empty-provider",
}, name: "Empty",
], env: [],
}, models: {},
}), },
}, ],
},
}),
},
}
//#when
await testCacheStore.updateConnectedProvidersCache(mockClient)
//#then
const cache = testCacheStore.readProviderModelsCache()
expect(cache).not.toBeNull()
expect(cache!.models).toEqual({})
} finally {
cleanupTestCacheContext(fakeUserCacheRoot)
} }
//#when
await testCacheStore.updateConnectedProvidersCache(mockClient)
//#then
const cache = testCacheStore.readProviderModelsCache()
expect(cache).not.toBeNull()
expect(cache!.models).toEqual({})
}) })
test("writes empty models when all field is missing", async () => { test("writes empty models when all field is missing", async () => {
//#given const { testCacheStore, fakeUserCacheRoot } = createTestCacheContext()
const mockClient = {
provider: { try {
list: async () => ({ //#given
data: { const mockClient = {
connected: ["openai"], provider: {
}, list: async () => ({
}), data: {
}, connected: ["openai"],
},
}),
},
}
//#when
await testCacheStore.updateConnectedProvidersCache(mockClient)
//#then
const cache = testCacheStore.readProviderModelsCache()
expect(cache).not.toBeNull()
expect(cache!.models).toEqual({})
} finally {
cleanupTestCacheContext(fakeUserCacheRoot)
} }
//#when
await testCacheStore.updateConnectedProvidersCache(mockClient)
//#then
const cache = testCacheStore.readProviderModelsCache()
expect(cache).not.toBeNull()
expect(cache!.models).toEqual({})
}) })
test("does nothing when client.provider.list is not available", async () => { test("does nothing when client.provider.list is not available", async () => {
//#given const { testCacheStore, fakeUserCacheRoot } = createTestCacheContext()
const mockClient = {}
//#when try {
await testCacheStore.updateConnectedProvidersCache(mockClient) //#given
const mockClient = {}
//#then //#when
const cache = testCacheStore.readProviderModelsCache() await testCacheStore.updateConnectedProvidersCache(mockClient)
expect(cache).toBeNull()
//#then
const cache = testCacheStore.readProviderModelsCache()
expect(cache).toBeNull()
} finally {
cleanupTestCacheContext(fakeUserCacheRoot)
}
}) })
test("does not remove unrelated files in the cache directory", async () => { test("does not remove unrelated files in the cache directory", async () => {
const { testCacheStore, fakeUserCacheRoot } = createTestCacheContext()
//#given //#given
const realCacheDir = join(fakeUserCacheRoot, "oh-my-opencode") const realCacheDir = join(fakeUserCacheRoot, "oh-my-opencode")
const sentinelPath = join(realCacheDir, "connected-providers-cache.test-sentinel.json") const sentinelPath = join(realCacheDir, "connected-providers-cache.test-sentinel.json")
@@ -179,88 +202,101 @@ describe("updateConnectedProvidersCache", () => {
if (existsSync(sentinelPath)) { if (existsSync(sentinelPath)) {
rmSync(sentinelPath, { force: true }) rmSync(sentinelPath, { force: true })
} }
cleanupTestCacheContext(fakeUserCacheRoot)
} }
}) })
test("findProviderModelMetadata returns rich cached metadata", async () => { test("findProviderModelMetadata returns rich cached metadata", async () => {
//#given const { testCacheStore, fakeUserCacheRoot } = createTestCacheContext()
const mockClient = {
provider: { try {
list: async () => ({ //#given
data: { const mockClient = {
connected: ["openai"], provider: {
all: [ list: async () => ({
{ data: {
id: "openai", connected: ["openai"],
models: { all: [
"gpt-5.4": { {
id: "gpt-5.4", id: "openai",
name: "GPT-5.4", models: {
temperature: false, "gpt-5.4": {
variants: { id: "gpt-5.4",
low: {}, name: "GPT-5.4",
high: {}, temperature: false,
variants: {
low: {},
high: {},
},
limit: { output: 128000 },
}, },
limit: { output: 128000 },
}, },
}, },
}, ],
], },
}, }),
}), },
}, }
await testCacheStore.updateConnectedProvidersCache(mockClient)
const cache = testCacheStore.readProviderModelsCache()
//#when
const result = findProviderModelMetadata("openai", "gpt-5.4", cache)
//#then
expect(result).toEqual({
id: "gpt-5.4",
name: "GPT-5.4",
temperature: false,
variants: {
low: {},
high: {},
},
limit: { output: 128000 },
})
} finally {
cleanupTestCacheContext(fakeUserCacheRoot)
} }
await testCacheStore.updateConnectedProvidersCache(mockClient)
const cache = testCacheStore.readProviderModelsCache()
//#when
const result = findProviderModelMetadata("openai", "gpt-5.4", cache)
//#then
expect(result).toEqual({
id: "gpt-5.4",
name: "GPT-5.4",
temperature: false,
variants: {
low: {},
high: {},
},
limit: { output: 128000 },
})
}) })
test("keeps normalized fallback ids when raw metadata id is not a string", async () => { test("keeps normalized fallback ids when raw metadata id is not a string", async () => {
const mockClient = { const { testCacheStore, fakeUserCacheRoot } = createTestCacheContext()
provider: {
list: async () => ({ try {
data: { const mockClient = {
connected: ["openai"], provider: {
all: [ list: async () => ({
{ data: {
id: "openai", connected: ["openai"],
models: { all: [
"o3-mini": { {
id: 123, id: "openai",
name: "o3-mini", models: {
"o3-mini": {
id: 123,
name: "o3-mini",
},
}, },
}, },
}, ],
], },
}, }),
}), },
}, }
await testCacheStore.updateConnectedProvidersCache(mockClient)
const cache = testCacheStore.readProviderModelsCache()
expect(cache?.models.openai).toEqual([
{ id: "o3-mini", name: "o3-mini" },
])
expect(findProviderModelMetadata("openai", "o3-mini", cache)).toEqual({
id: "o3-mini",
name: "o3-mini",
})
} finally {
cleanupTestCacheContext(fakeUserCacheRoot)
} }
await testCacheStore.updateConnectedProvidersCache(mockClient)
const cache = testCacheStore.readProviderModelsCache()
expect(cache?.models.openai).toEqual([
{ id: "o3-mini", name: "o3-mini" },
])
expect(findProviderModelMetadata("openai", "o3-mini", cache)).toEqual({
id: "o3-mini",
name: "o3-mini",
})
}) })
}) })
+80 -51
View File
@@ -1,81 +1,110 @@
import { afterEach, beforeEach, describe, expect, it } from "bun:test" import { describe, expect, it } from "bun:test"
import { mkdirSync, rmSync, writeFileSync } from "node:fs" import { mkdirSync, rmSync, writeFileSync } from "node:fs"
import { tmpdir } from "node:os" import { tmpdir } from "node:os"
import { join } from "node:path" import { join } from "node:path"
import { checkForLegacyPluginEntry } from "./legacy-plugin-warning" import { checkForLegacyPluginEntry } from "./legacy-plugin-warning"
function createTestConfigDir(): string {
const testConfigDir = join(tmpdir(), `omo-legacy-check-${Date.now()}-${Math.random().toString(36).slice(2)}`)
mkdirSync(testConfigDir, { recursive: true })
return testConfigDir
}
function cleanupTestConfigDir(testConfigDir: string): void {
rmSync(testConfigDir, { recursive: true, force: true })
}
describe("checkForLegacyPluginEntry", () => { describe("checkForLegacyPluginEntry", () => {
let testConfigDir = ""
beforeEach(() => {
testConfigDir = join(tmpdir(), `omo-legacy-check-${Date.now()}-${Math.random().toString(36).slice(2)}`)
mkdirSync(testConfigDir, { recursive: true })
})
afterEach(() => {
rmSync(testConfigDir, { recursive: true, force: true })
})
it("detects a bare legacy plugin entry", () => { it("detects a bare legacy plugin entry", () => {
// given const testConfigDir = createTestConfigDir()
writeFileSync(join(testConfigDir, "opencode.json"), JSON.stringify({ plugin: ["oh-my-opencode"] }, null, 2))
// when try {
const result = checkForLegacyPluginEntry(testConfigDir) // given
writeFileSync(join(testConfigDir, "opencode.json"), JSON.stringify({ plugin: ["oh-my-opencode"] }, null, 2))
// then // when
expect(result.hasLegacyEntry).toBe(true) const result = checkForLegacyPluginEntry(testConfigDir)
expect(result.hasCanonicalEntry).toBe(false)
expect(result.legacyEntries).toEqual(["oh-my-opencode"]) // then
expect(result.configPath).toBe(join(testConfigDir, "opencode.json")) expect(result.hasLegacyEntry).toBe(true)
expect(result.hasCanonicalEntry).toBe(false)
expect(result.legacyEntries).toEqual(["oh-my-opencode"])
expect(result.configPath).toBe(join(testConfigDir, "opencode.json"))
} finally {
cleanupTestConfigDir(testConfigDir)
}
}) })
it("detects a version-pinned legacy plugin entry", () => { it("detects a version-pinned legacy plugin entry", () => {
// given const testConfigDir = createTestConfigDir()
writeFileSync(join(testConfigDir, "opencode.json"), JSON.stringify({ plugin: ["oh-my-opencode@3.10.0"] }, null, 2))
// when try {
const result = checkForLegacyPluginEntry(testConfigDir) // given
writeFileSync(join(testConfigDir, "opencode.json"), JSON.stringify({ plugin: ["oh-my-opencode@3.10.0"] }, null, 2))
// then // when
expect(result.hasLegacyEntry).toBe(true) const result = checkForLegacyPluginEntry(testConfigDir)
expect(result.hasCanonicalEntry).toBe(false)
expect(result.legacyEntries).toEqual(["oh-my-opencode@3.10.0"]) // then
expect(result.hasLegacyEntry).toBe(true)
expect(result.hasCanonicalEntry).toBe(false)
expect(result.legacyEntries).toEqual(["oh-my-opencode@3.10.0"])
} finally {
cleanupTestConfigDir(testConfigDir)
}
}) })
it("does not flag a canonical plugin entry", () => { it("does not flag a canonical plugin entry", () => {
// given const testConfigDir = createTestConfigDir()
writeFileSync(join(testConfigDir, "opencode.json"), JSON.stringify({ plugin: ["oh-my-openagent"] }, null, 2))
// when try {
const result = checkForLegacyPluginEntry(testConfigDir) // given
writeFileSync(join(testConfigDir, "opencode.json"), JSON.stringify({ plugin: ["oh-my-openagent"] }, null, 2))
// then // when
expect(result.hasLegacyEntry).toBe(false) const result = checkForLegacyPluginEntry(testConfigDir)
expect(result.hasCanonicalEntry).toBe(true)
expect(result.legacyEntries).toEqual([]) // then
expect(result.hasLegacyEntry).toBe(false)
expect(result.hasCanonicalEntry).toBe(true)
expect(result.legacyEntries).toEqual([])
} finally {
cleanupTestConfigDir(testConfigDir)
}
}) })
it("detects legacy entries in quoted jsonc config", () => { it("detects legacy entries in quoted jsonc config", () => {
// given const testConfigDir = createTestConfigDir()
writeFileSync(join(testConfigDir, "opencode.jsonc"), '{\n "plugin": ["oh-my-opencode"]\n}\n')
// when try {
const result = checkForLegacyPluginEntry(testConfigDir) // given
writeFileSync(join(testConfigDir, "opencode.jsonc"), '{\n "plugin": ["oh-my-opencode"]\n}\n')
// then // when
expect(result.hasLegacyEntry).toBe(true) const result = checkForLegacyPluginEntry(testConfigDir)
expect(result.legacyEntries).toEqual(["oh-my-opencode"])
// then
expect(result.hasLegacyEntry).toBe(true)
expect(result.legacyEntries).toEqual(["oh-my-opencode"])
} finally {
cleanupTestConfigDir(testConfigDir)
}
}) })
it("returns no warning data when config is missing", () => { it("returns no warning data when config is missing", () => {
// when const testConfigDir = createTestConfigDir()
const result = checkForLegacyPluginEntry(testConfigDir)
// then try {
expect(result.hasLegacyEntry).toBe(false) // when
expect(result.hasCanonicalEntry).toBe(false) const result = checkForLegacyPluginEntry(testConfigDir)
expect(result.legacyEntries).toEqual([])
expect(result.configPath).toBeNull() // then
expect(result.hasLegacyEntry).toBe(false)
expect(result.hasCanonicalEntry).toBe(false)
expect(result.legacyEntries).toEqual([])
expect(result.configPath).toBeNull()
} finally {
cleanupTestConfigDir(testConfigDir)
}
}) })
}) })