mirror of
https://github.com/Kilo-Org/kilocode.git
synced 2026-09-19 01:51:21 +08:00
refactor(test): replace direct Config.get assignment with spyOn mocking
Swap manual save-and-restore of Config.get for bun:test spyOn/mock.restore across session-list and recall test suites, ensuring proper mock teardown and consistent namespace imports.
This commit is contained in:
@@ -2,7 +2,7 @@
|
||||
import { afterEach, beforeEach, describe, expect, mock, spyOn, test } from "bun:test"
|
||||
import { $ } from "bun"
|
||||
import path from "path"
|
||||
import { Config } from "../../src/config"
|
||||
import * as Config from "../../src/config/config"
|
||||
import { Instance } from "../../src/project/instance"
|
||||
import { Log } from "../../src/util"
|
||||
import { resetDatabase } from "../fixture/db"
|
||||
@@ -29,8 +29,9 @@ describe("experimental.session.list", () => {
|
||||
try {
|
||||
await $`git worktree add ${worktree} -b test-branch-${Date.now()}`.cwd(first.path).quiet()
|
||||
|
||||
const share = Config.get
|
||||
Config.get = async () => ({ share: "manual" }) as Awaited<ReturnType<typeof Config.get>>
|
||||
spyOn(Config, "get").mockImplementation(
|
||||
async () => ({ share: "manual" }) as Awaited<ReturnType<typeof Config.get>>,
|
||||
)
|
||||
|
||||
try {
|
||||
const { Server } = await import("../../src/server/server")
|
||||
@@ -83,7 +84,7 @@ describe("experimental.session.list", () => {
|
||||
expect(dirs).toContain(worktree)
|
||||
expect(body.some((item: { title: string }) => item.title === "other-project-session")).toBe(false)
|
||||
} finally {
|
||||
Config.get = share
|
||||
mock.restore()
|
||||
}
|
||||
} finally {
|
||||
await $`git worktree remove ${worktree}`.cwd(first.path).quiet().nothrow()
|
||||
@@ -98,8 +99,9 @@ describe("experimental.session.list", () => {
|
||||
try {
|
||||
await $`git worktree add ${worktree} -b test-branch-sdk-${Date.now()}`.cwd(first.path).quiet()
|
||||
|
||||
const share = Config.get
|
||||
Config.get = async () => ({ share: "manual" }) as Awaited<ReturnType<typeof Config.get>>
|
||||
spyOn(Config, "get").mockImplementation(
|
||||
async () => ({ share: "manual" }) as Awaited<ReturnType<typeof Config.get>>,
|
||||
)
|
||||
|
||||
try {
|
||||
const { Server } = await import("../../src/server/server")
|
||||
@@ -147,7 +149,7 @@ describe("experimental.session.list", () => {
|
||||
expect(ids).toContain(branch.id)
|
||||
expect(body.some((item: { title: string }) => item.title === "other-project-session")).toBe(false)
|
||||
} finally {
|
||||
Config.get = share
|
||||
mock.restore()
|
||||
}
|
||||
} finally {
|
||||
await $`git worktree remove ${worktree}`.cwd(first.path).quiet().nothrow()
|
||||
|
||||
@@ -4,7 +4,7 @@ import { $ } from "bun"
|
||||
import { Effect } from "effect"
|
||||
import path from "path"
|
||||
import { Instance } from "../../src/project/instance"
|
||||
import { Config } from "../../src/config"
|
||||
import * as Config from "../../src/config/config"
|
||||
import { RecallTool } from "../../src/tool/recall"
|
||||
import { AppRuntime } from "../../src/effect/app-runtime"
|
||||
import { resetDatabase } from "../fixture/db"
|
||||
@@ -43,8 +43,9 @@ describe("tool.recall", () => {
|
||||
await $`git worktree add ${worktree} -b test-branch-${Date.now()}`.cwd(first.path).quiet()
|
||||
await Bun.write(path.join(first.path, ".git", "opencode"), "stale-project-id")
|
||||
|
||||
const share = Config.get
|
||||
Config.get = async () => ({ share: "manual" }) as Awaited<ReturnType<typeof Config.get>>
|
||||
spyOn(Config, "get").mockImplementation(
|
||||
async () => ({ share: "manual" }) as Awaited<ReturnType<typeof Config.get>>,
|
||||
)
|
||||
|
||||
try {
|
||||
const { Session } = await import("../../src/session/index")
|
||||
@@ -74,7 +75,7 @@ describe("tool.recall", () => {
|
||||
expect(result.output).toContain("search-target worktree")
|
||||
expect(result.output).not.toContain("search-target other")
|
||||
} finally {
|
||||
Config.get = share
|
||||
mock.restore()
|
||||
}
|
||||
} finally {
|
||||
await $`git worktree remove ${worktree}`.cwd(first.path).quiet().nothrow()
|
||||
@@ -85,8 +86,7 @@ describe("tool.recall", () => {
|
||||
await using first = await tmpdir({ git: true })
|
||||
await using second = await tmpdir({ git: true })
|
||||
|
||||
const share = Config.get
|
||||
Config.get = async () => ({ share: "manual" }) as Awaited<ReturnType<typeof Config.get>>
|
||||
spyOn(Config, "get").mockImplementation(async () => ({ share: "manual" }) as Awaited<ReturnType<typeof Config.get>>)
|
||||
|
||||
try {
|
||||
const { Session } = await import("../../src/session/index")
|
||||
@@ -109,7 +109,7 @@ describe("tool.recall", () => {
|
||||
expect(err).toBeInstanceOf(Error)
|
||||
expect((err as Error).message).toContain("belongs to a different workspace")
|
||||
} finally {
|
||||
Config.get = share
|
||||
mock.restore()
|
||||
}
|
||||
})
|
||||
|
||||
@@ -121,8 +121,9 @@ describe("tool.recall", () => {
|
||||
await $`git worktree add ${worktree} -b test-branch-${Date.now()}`.cwd(first.path).quiet()
|
||||
await Bun.write(path.join(first.path, ".git", "opencode"), "stale-project-id")
|
||||
|
||||
const share = Config.get
|
||||
Config.get = async () => ({ share: "manual" }) as Awaited<ReturnType<typeof Config.get>>
|
||||
spyOn(Config, "get").mockImplementation(
|
||||
async () => ({ share: "manual" }) as Awaited<ReturnType<typeof Config.get>>,
|
||||
)
|
||||
|
||||
try {
|
||||
const { Session } = await import("../../src/session/index")
|
||||
@@ -142,7 +143,7 @@ describe("tool.recall", () => {
|
||||
|
||||
expect(result.output).toContain("# Session: worktree readable")
|
||||
} finally {
|
||||
Config.get = share
|
||||
mock.restore()
|
||||
}
|
||||
} finally {
|
||||
await $`git worktree remove ${worktree}`.cwd(first.path).quiet().nothrow()
|
||||
|
||||
Reference in New Issue
Block a user