project-init-git.test.ts 3.8 KB

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