project-init-git.test.ts 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. import { afterEach, describe, expect, spyOn, test } from "bun:test"
  2. import { Effect } from "effect"
  3. import path from "path"
  4. import { GlobalBus } from "../../src/bus/global"
  5. import { Snapshot } from "../../src/snapshot"
  6. import { InstanceBootstrap } from "../../src/project/bootstrap"
  7. import { Instance } from "../../src/project/instance"
  8. import { Server } from "../../src/server/server"
  9. import { Filesystem } from "../../src/util/filesystem"
  10. import { Log } from "../../src/util/log"
  11. import { resetDatabase } from "../fixture/db"
  12. import { provideInstance, tmpdir } from "../fixture/fixture"
  13. Log.init({ print: false })
  14. afterEach(async () => {
  15. await resetDatabase()
  16. })
  17. describe("project.initGit endpoint", () => {
  18. test("initializes git and reloads immediately", async () => {
  19. await using tmp = await tmpdir()
  20. const app = Server.Default().app
  21. const seen: { directory?: string; payload: { type: string } }[] = []
  22. const fn = (evt: { directory?: string; payload: { type: string } }) => {
  23. seen.push(evt)
  24. }
  25. const reload = Instance.reload
  26. const reloadSpy = spyOn(Instance, "reload").mockImplementation((input) => reload(input))
  27. GlobalBus.on("event", fn)
  28. try {
  29. const init = await app.request("/project/git/init", {
  30. method: "POST",
  31. headers: {
  32. "x-kilo-directory": tmp.path,
  33. },
  34. })
  35. const body = await init.json()
  36. expect(init.status).toBe(200)
  37. expect(body).toMatchObject({
  38. id: "global",
  39. vcs: "git",
  40. worktree: tmp.path,
  41. })
  42. expect(reloadSpy).toHaveBeenCalledTimes(1)
  43. expect(seen.some((evt) => evt.directory === tmp.path && evt.payload.type === "server.instance.disposed")).toBe(
  44. true,
  45. )
  46. expect(await Filesystem.exists(path.join(tmp.path, ".git", "opencode"))).toBe(false)
  47. const current = await app.request("/project/current", {
  48. headers: {
  49. "x-kilo-directory": tmp.path,
  50. },
  51. })
  52. expect(current.status).toBe(200)
  53. expect(await current.json()).toMatchObject({
  54. id: "global",
  55. vcs: "git",
  56. worktree: tmp.path,
  57. })
  58. expect(
  59. await Effect.runPromise(
  60. Snapshot.Service.use((svc) => svc.track()).pipe(
  61. provideInstance(tmp.path),
  62. Effect.provide(Snapshot.defaultLayer),
  63. ),
  64. ),
  65. ).toBeTruthy()
  66. } finally {
  67. await Instance.disposeAll()
  68. reloadSpy.mockRestore()
  69. GlobalBus.off("event", fn)
  70. }
  71. })
  72. test("does not reload when the project is already git", async () => {
  73. await using tmp = await tmpdir({ git: true })
  74. const app = Server.Default().app
  75. const seen: { directory?: string; payload: { type: string } }[] = []
  76. const fn = (evt: { directory?: string; payload: { type: string } }) => {
  77. seen.push(evt)
  78. }
  79. const reload = Instance.reload
  80. const reloadSpy = spyOn(Instance, "reload").mockImplementation((input) => reload(input))
  81. GlobalBus.on("event", fn)
  82. try {
  83. const init = await app.request("/project/git/init", {
  84. method: "POST",
  85. headers: {
  86. "x-kilo-directory": tmp.path,
  87. },
  88. })
  89. expect(init.status).toBe(200)
  90. expect(await init.json()).toMatchObject({
  91. vcs: "git",
  92. worktree: tmp.path,
  93. })
  94. expect(
  95. seen.filter((evt) => evt.directory === tmp.path && evt.payload.type === "server.instance.disposed").length,
  96. ).toBe(0)
  97. expect(reloadSpy).toHaveBeenCalledTimes(0)
  98. const current = await app.request("/project/current", {
  99. headers: {
  100. "x-kilo-directory": tmp.path,
  101. },
  102. })
  103. expect(current.status).toBe(200)
  104. expect(await current.json()).toMatchObject({
  105. vcs: "git",
  106. worktree: tmp.path,
  107. })
  108. } finally {
  109. await Instance.disposeAll()
  110. reloadSpy.mockRestore()
  111. GlobalBus.off("event", fn)
  112. }
  113. })
  114. })