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
+54 -18
View File
@@ -1,35 +1,35 @@
/// <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)
describe("updateConnectedProvidersCache", () => { return {
beforeEach(() => { fakeUserCacheRoot,
fakeUserCacheRoot = mkdtempSync(join(tmpdir(), "connected-providers-user-cache-")) testCacheDir,
testCacheDir = join(fakeUserCacheRoot, "oh-my-opencode") testCacheStore,
testCacheStore = createConnectedProvidersCacheStore(() => testCacheDir) }
}) }
afterEach(() => { function cleanupTestCacheContext(fakeUserCacheRoot: string): void {
if (existsSync(fakeUserCacheRoot)) { if (existsSync(fakeUserCacheRoot)) {
rmSync(fakeUserCacheRoot, { recursive: true, force: true }) rmSync(fakeUserCacheRoot, { recursive: true, force: true })
} }
fakeUserCacheRoot = "" }
testCacheDir = ""
})
describe("updateConnectedProvidersCache", () => {
test("extracts models from provider.list().all response", async () => { test("extracts models from provider.list().all response", async () => {
const { testCacheStore, fakeUserCacheRoot } = createTestCacheContext()
try {
//#given //#given
const mockClient = { const mockClient = {
provider: { provider: {
@@ -78,9 +78,15 @@ describe("updateConnectedProvidersCache", () => {
{ id: "claude-sonnet-4-6", name: "Claude Sonnet 4.6" }, { id: "claude-sonnet-4-6", name: "Claude Sonnet 4.6" },
], ],
}) })
} finally {
cleanupTestCacheContext(fakeUserCacheRoot)
}
}) })
test("writes empty models when provider has no models", async () => { test("writes empty models when provider has no models", async () => {
const { testCacheStore, fakeUserCacheRoot } = createTestCacheContext()
try {
//#given //#given
const mockClient = { const mockClient = {
provider: { provider: {
@@ -107,9 +113,15 @@ describe("updateConnectedProvidersCache", () => {
const cache = testCacheStore.readProviderModelsCache() const cache = testCacheStore.readProviderModelsCache()
expect(cache).not.toBeNull() expect(cache).not.toBeNull()
expect(cache!.models).toEqual({}) expect(cache!.models).toEqual({})
} finally {
cleanupTestCacheContext(fakeUserCacheRoot)
}
}) })
test("writes empty models when all field is missing", async () => { test("writes empty models when all field is missing", async () => {
const { testCacheStore, fakeUserCacheRoot } = createTestCacheContext()
try {
//#given //#given
const mockClient = { const mockClient = {
provider: { provider: {
@@ -128,9 +140,15 @@ describe("updateConnectedProvidersCache", () => {
const cache = testCacheStore.readProviderModelsCache() const cache = testCacheStore.readProviderModelsCache()
expect(cache).not.toBeNull() expect(cache).not.toBeNull()
expect(cache!.models).toEqual({}) expect(cache!.models).toEqual({})
} finally {
cleanupTestCacheContext(fakeUserCacheRoot)
}
}) })
test("does nothing when client.provider.list is not available", async () => { test("does nothing when client.provider.list is not available", async () => {
const { testCacheStore, fakeUserCacheRoot } = createTestCacheContext()
try {
//#given //#given
const mockClient = {} const mockClient = {}
@@ -140,9 +158,14 @@ describe("updateConnectedProvidersCache", () => {
//#then //#then
const cache = testCacheStore.readProviderModelsCache() const cache = testCacheStore.readProviderModelsCache()
expect(cache).toBeNull() 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,10 +202,14 @@ 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 () => {
const { testCacheStore, fakeUserCacheRoot } = createTestCacheContext()
try {
//#given //#given
const mockClient = { const mockClient = {
provider: { provider: {
@@ -228,9 +255,15 @@ describe("updateConnectedProvidersCache", () => {
}, },
limit: { output: 128000 }, limit: { output: 128000 },
}) })
} finally {
cleanupTestCacheContext(fakeUserCacheRoot)
}
}) })
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 { testCacheStore, fakeUserCacheRoot } = createTestCacheContext()
try {
const mockClient = { const mockClient = {
provider: { provider: {
list: async () => ({ list: async () => ({
@@ -262,5 +295,8 @@ describe("updateConnectedProvidersCache", () => {
id: "o3-mini", id: "o3-mini",
name: "o3-mini", name: "o3-mini",
}) })
} finally {
cleanupTestCacheContext(fakeUserCacheRoot)
}
}) })
}) })
+38 -9
View File
@@ -1,22 +1,24 @@
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"
describe("checkForLegacyPluginEntry", () => { function createTestConfigDir(): string {
let testConfigDir = "" const testConfigDir = join(tmpdir(), `omo-legacy-check-${Date.now()}-${Math.random().toString(36).slice(2)}`)
beforeEach(() => {
testConfigDir = join(tmpdir(), `omo-legacy-check-${Date.now()}-${Math.random().toString(36).slice(2)}`)
mkdirSync(testConfigDir, { recursive: true }) mkdirSync(testConfigDir, { recursive: true })
}) return testConfigDir
}
afterEach(() => { function cleanupTestConfigDir(testConfigDir: string): void {
rmSync(testConfigDir, { recursive: true, force: true }) rmSync(testConfigDir, { recursive: true, force: true })
}) }
describe("checkForLegacyPluginEntry", () => {
it("detects a bare legacy plugin entry", () => { it("detects a bare legacy plugin entry", () => {
const testConfigDir = createTestConfigDir()
try {
// given // given
writeFileSync(join(testConfigDir, "opencode.json"), JSON.stringify({ plugin: ["oh-my-opencode"] }, null, 2)) writeFileSync(join(testConfigDir, "opencode.json"), JSON.stringify({ plugin: ["oh-my-opencode"] }, null, 2))
@@ -28,9 +30,15 @@ describe("checkForLegacyPluginEntry", () => {
expect(result.hasCanonicalEntry).toBe(false) expect(result.hasCanonicalEntry).toBe(false)
expect(result.legacyEntries).toEqual(["oh-my-opencode"]) expect(result.legacyEntries).toEqual(["oh-my-opencode"])
expect(result.configPath).toBe(join(testConfigDir, "opencode.json")) 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", () => {
const testConfigDir = createTestConfigDir()
try {
// given // given
writeFileSync(join(testConfigDir, "opencode.json"), JSON.stringify({ plugin: ["oh-my-opencode@3.10.0"] }, null, 2)) writeFileSync(join(testConfigDir, "opencode.json"), JSON.stringify({ plugin: ["oh-my-opencode@3.10.0"] }, null, 2))
@@ -41,9 +49,15 @@ describe("checkForLegacyPluginEntry", () => {
expect(result.hasLegacyEntry).toBe(true) expect(result.hasLegacyEntry).toBe(true)
expect(result.hasCanonicalEntry).toBe(false) expect(result.hasCanonicalEntry).toBe(false)
expect(result.legacyEntries).toEqual(["oh-my-opencode@3.10.0"]) 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", () => {
const testConfigDir = createTestConfigDir()
try {
// given // given
writeFileSync(join(testConfigDir, "opencode.json"), JSON.stringify({ plugin: ["oh-my-openagent"] }, null, 2)) writeFileSync(join(testConfigDir, "opencode.json"), JSON.stringify({ plugin: ["oh-my-openagent"] }, null, 2))
@@ -54,9 +68,15 @@ describe("checkForLegacyPluginEntry", () => {
expect(result.hasLegacyEntry).toBe(false) expect(result.hasLegacyEntry).toBe(false)
expect(result.hasCanonicalEntry).toBe(true) expect(result.hasCanonicalEntry).toBe(true)
expect(result.legacyEntries).toEqual([]) expect(result.legacyEntries).toEqual([])
} finally {
cleanupTestConfigDir(testConfigDir)
}
}) })
it("detects legacy entries in quoted jsonc config", () => { it("detects legacy entries in quoted jsonc config", () => {
const testConfigDir = createTestConfigDir()
try {
// given // given
writeFileSync(join(testConfigDir, "opencode.jsonc"), '{\n "plugin": ["oh-my-opencode"]\n}\n') writeFileSync(join(testConfigDir, "opencode.jsonc"), '{\n "plugin": ["oh-my-opencode"]\n}\n')
@@ -66,9 +86,15 @@ describe("checkForLegacyPluginEntry", () => {
// then // then
expect(result.hasLegacyEntry).toBe(true) expect(result.hasLegacyEntry).toBe(true)
expect(result.legacyEntries).toEqual(["oh-my-opencode"]) 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", () => {
const testConfigDir = createTestConfigDir()
try {
// when // when
const result = checkForLegacyPluginEntry(testConfigDir) const result = checkForLegacyPluginEntry(testConfigDir)
@@ -77,5 +103,8 @@ describe("checkForLegacyPluginEntry", () => {
expect(result.hasCanonicalEntry).toBe(false) expect(result.hasCanonicalEntry).toBe(false)
expect(result.legacyEntries).toEqual([]) expect(result.legacyEntries).toEqual([])
expect(result.configPath).toBeNull() expect(result.configPath).toBeNull()
} finally {
cleanupTestConfigDir(testConfigDir)
}
}) })
}) })