test: isolate flaky shared-state tests
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
/// <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 type { AgentConfig } from "@opencode-ai/sdk"
|
||||
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"
|
||||
|
||||
beforeEach(() => {
|
||||
mock.restore()
|
||||
clearSkillCache()
|
||||
connectedProvidersCache._resetMemCacheForTesting()
|
||||
})
|
||||
|
||||
afterEach(() => {
|
||||
clearSkillCache()
|
||||
connectedProvidersCache._resetMemCacheForTesting()
|
||||
mock.restore()
|
||||
})
|
||||
|
||||
describe("createBuiltinAgents with model overrides", () => {
|
||||
test("Sisyphus with default model has thinking config when all models available", async () => {
|
||||
// #given
|
||||
|
||||
@@ -1,35 +1,35 @@
|
||||
/// <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 { tmpdir } from "node:os"
|
||||
import { join } from "node:path"
|
||||
import {
|
||||
createConnectedProvidersCacheStore,
|
||||
findProviderModelMetadata,
|
||||
} from "./connected-providers-cache"
|
||||
import { createConnectedProvidersCacheStore, findProviderModelMetadata } from "./connected-providers-cache"
|
||||
|
||||
let fakeUserCacheRoot = ""
|
||||
let testCacheDir = ""
|
||||
let testCacheStore: ReturnType<typeof createConnectedProvidersCacheStore>
|
||||
function createTestCacheContext() {
|
||||
const fakeUserCacheRoot = mkdtempSync(join(tmpdir(), "connected-providers-user-cache-"))
|
||||
const testCacheDir = join(fakeUserCacheRoot, "oh-my-opencode")
|
||||
const testCacheStore = createConnectedProvidersCacheStore(() => testCacheDir)
|
||||
|
||||
describe("updateConnectedProvidersCache", () => {
|
||||
beforeEach(() => {
|
||||
fakeUserCacheRoot = mkdtempSync(join(tmpdir(), "connected-providers-user-cache-"))
|
||||
testCacheDir = join(fakeUserCacheRoot, "oh-my-opencode")
|
||||
testCacheStore = createConnectedProvidersCacheStore(() => testCacheDir)
|
||||
})
|
||||
return {
|
||||
fakeUserCacheRoot,
|
||||
testCacheDir,
|
||||
testCacheStore,
|
||||
}
|
||||
}
|
||||
|
||||
afterEach(() => {
|
||||
function cleanupTestCacheContext(fakeUserCacheRoot: string): void {
|
||||
if (existsSync(fakeUserCacheRoot)) {
|
||||
rmSync(fakeUserCacheRoot, { recursive: true, force: true })
|
||||
}
|
||||
fakeUserCacheRoot = ""
|
||||
testCacheDir = ""
|
||||
})
|
||||
}
|
||||
|
||||
describe("updateConnectedProvidersCache", () => {
|
||||
test("extracts models from provider.list().all response", async () => {
|
||||
const { testCacheStore, fakeUserCacheRoot } = createTestCacheContext()
|
||||
|
||||
try {
|
||||
//#given
|
||||
const mockClient = {
|
||||
provider: {
|
||||
@@ -78,9 +78,15 @@ describe("updateConnectedProvidersCache", () => {
|
||||
{ id: "claude-sonnet-4-6", name: "Claude Sonnet 4.6" },
|
||||
],
|
||||
})
|
||||
} finally {
|
||||
cleanupTestCacheContext(fakeUserCacheRoot)
|
||||
}
|
||||
})
|
||||
|
||||
test("writes empty models when provider has no models", async () => {
|
||||
const { testCacheStore, fakeUserCacheRoot } = createTestCacheContext()
|
||||
|
||||
try {
|
||||
//#given
|
||||
const mockClient = {
|
||||
provider: {
|
||||
@@ -107,9 +113,15 @@ describe("updateConnectedProvidersCache", () => {
|
||||
const cache = testCacheStore.readProviderModelsCache()
|
||||
expect(cache).not.toBeNull()
|
||||
expect(cache!.models).toEqual({})
|
||||
} finally {
|
||||
cleanupTestCacheContext(fakeUserCacheRoot)
|
||||
}
|
||||
})
|
||||
|
||||
test("writes empty models when all field is missing", async () => {
|
||||
const { testCacheStore, fakeUserCacheRoot } = createTestCacheContext()
|
||||
|
||||
try {
|
||||
//#given
|
||||
const mockClient = {
|
||||
provider: {
|
||||
@@ -128,9 +140,15 @@ describe("updateConnectedProvidersCache", () => {
|
||||
const cache = testCacheStore.readProviderModelsCache()
|
||||
expect(cache).not.toBeNull()
|
||||
expect(cache!.models).toEqual({})
|
||||
} finally {
|
||||
cleanupTestCacheContext(fakeUserCacheRoot)
|
||||
}
|
||||
})
|
||||
|
||||
test("does nothing when client.provider.list is not available", async () => {
|
||||
const { testCacheStore, fakeUserCacheRoot } = createTestCacheContext()
|
||||
|
||||
try {
|
||||
//#given
|
||||
const mockClient = {}
|
||||
|
||||
@@ -140,9 +158,14 @@ describe("updateConnectedProvidersCache", () => {
|
||||
//#then
|
||||
const cache = testCacheStore.readProviderModelsCache()
|
||||
expect(cache).toBeNull()
|
||||
} finally {
|
||||
cleanupTestCacheContext(fakeUserCacheRoot)
|
||||
}
|
||||
})
|
||||
|
||||
test("does not remove unrelated files in the cache directory", async () => {
|
||||
const { testCacheStore, fakeUserCacheRoot } = createTestCacheContext()
|
||||
|
||||
//#given
|
||||
const realCacheDir = join(fakeUserCacheRoot, "oh-my-opencode")
|
||||
const sentinelPath = join(realCacheDir, "connected-providers-cache.test-sentinel.json")
|
||||
@@ -179,10 +202,14 @@ describe("updateConnectedProvidersCache", () => {
|
||||
if (existsSync(sentinelPath)) {
|
||||
rmSync(sentinelPath, { force: true })
|
||||
}
|
||||
cleanupTestCacheContext(fakeUserCacheRoot)
|
||||
}
|
||||
})
|
||||
|
||||
test("findProviderModelMetadata returns rich cached metadata", async () => {
|
||||
const { testCacheStore, fakeUserCacheRoot } = createTestCacheContext()
|
||||
|
||||
try {
|
||||
//#given
|
||||
const mockClient = {
|
||||
provider: {
|
||||
@@ -228,9 +255,15 @@ describe("updateConnectedProvidersCache", () => {
|
||||
},
|
||||
limit: { output: 128000 },
|
||||
})
|
||||
} finally {
|
||||
cleanupTestCacheContext(fakeUserCacheRoot)
|
||||
}
|
||||
})
|
||||
|
||||
test("keeps normalized fallback ids when raw metadata id is not a string", async () => {
|
||||
const { testCacheStore, fakeUserCacheRoot } = createTestCacheContext()
|
||||
|
||||
try {
|
||||
const mockClient = {
|
||||
provider: {
|
||||
list: async () => ({
|
||||
@@ -262,5 +295,8 @@ describe("updateConnectedProvidersCache", () => {
|
||||
id: "o3-mini",
|
||||
name: "o3-mini",
|
||||
})
|
||||
} finally {
|
||||
cleanupTestCacheContext(fakeUserCacheRoot)
|
||||
}
|
||||
})
|
||||
})
|
||||
|
||||
@@ -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 { tmpdir } from "node:os"
|
||||
import { join } from "node:path"
|
||||
import { checkForLegacyPluginEntry } from "./legacy-plugin-warning"
|
||||
|
||||
describe("checkForLegacyPluginEntry", () => {
|
||||
let testConfigDir = ""
|
||||
|
||||
beforeEach(() => {
|
||||
testConfigDir = join(tmpdir(), `omo-legacy-check-${Date.now()}-${Math.random().toString(36).slice(2)}`)
|
||||
function createTestConfigDir(): string {
|
||||
const testConfigDir = join(tmpdir(), `omo-legacy-check-${Date.now()}-${Math.random().toString(36).slice(2)}`)
|
||||
mkdirSync(testConfigDir, { recursive: true })
|
||||
})
|
||||
return testConfigDir
|
||||
}
|
||||
|
||||
afterEach(() => {
|
||||
function cleanupTestConfigDir(testConfigDir: string): void {
|
||||
rmSync(testConfigDir, { recursive: true, force: true })
|
||||
})
|
||||
}
|
||||
|
||||
describe("checkForLegacyPluginEntry", () => {
|
||||
it("detects a bare legacy plugin entry", () => {
|
||||
const testConfigDir = createTestConfigDir()
|
||||
|
||||
try {
|
||||
// given
|
||||
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.legacyEntries).toEqual(["oh-my-opencode"])
|
||||
expect(result.configPath).toBe(join(testConfigDir, "opencode.json"))
|
||||
} finally {
|
||||
cleanupTestConfigDir(testConfigDir)
|
||||
}
|
||||
})
|
||||
|
||||
it("detects a version-pinned legacy plugin entry", () => {
|
||||
const testConfigDir = createTestConfigDir()
|
||||
|
||||
try {
|
||||
// given
|
||||
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.hasCanonicalEntry).toBe(false)
|
||||
expect(result.legacyEntries).toEqual(["oh-my-opencode@3.10.0"])
|
||||
} finally {
|
||||
cleanupTestConfigDir(testConfigDir)
|
||||
}
|
||||
})
|
||||
|
||||
it("does not flag a canonical plugin entry", () => {
|
||||
const testConfigDir = createTestConfigDir()
|
||||
|
||||
try {
|
||||
// given
|
||||
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.hasCanonicalEntry).toBe(true)
|
||||
expect(result.legacyEntries).toEqual([])
|
||||
} finally {
|
||||
cleanupTestConfigDir(testConfigDir)
|
||||
}
|
||||
})
|
||||
|
||||
it("detects legacy entries in quoted jsonc config", () => {
|
||||
const testConfigDir = createTestConfigDir()
|
||||
|
||||
try {
|
||||
// given
|
||||
writeFileSync(join(testConfigDir, "opencode.jsonc"), '{\n "plugin": ["oh-my-opencode"]\n}\n')
|
||||
|
||||
@@ -66,9 +86,15 @@ describe("checkForLegacyPluginEntry", () => {
|
||||
// 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", () => {
|
||||
const testConfigDir = createTestConfigDir()
|
||||
|
||||
try {
|
||||
// when
|
||||
const result = checkForLegacyPluginEntry(testConfigDir)
|
||||
|
||||
@@ -77,5 +103,8 @@ describe("checkForLegacyPluginEntry", () => {
|
||||
expect(result.hasCanonicalEntry).toBe(false)
|
||||
expect(result.legacyEntries).toEqual([])
|
||||
expect(result.configPath).toBeNull()
|
||||
} finally {
|
||||
cleanupTestConfigDir(testConfigDir)
|
||||
}
|
||||
})
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user