adapters.test.ts 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. import { describe, expect, test } from "bun:test"
  2. import { getAdapter, registerAdapter } from "../../src/control-plane/adapters"
  3. import { ProjectV2 } from "@kirincode-ai/core/project"
  4. import type { WorkspaceInfo } from "../../src/control-plane/types"
  5. function info(projectID: WorkspaceInfo["projectID"], type: string): WorkspaceInfo {
  6. return {
  7. id: "workspace-test" as WorkspaceInfo["id"],
  8. type,
  9. name: "workspace-test",
  10. branch: null,
  11. directory: null,
  12. extra: null,
  13. projectID,
  14. }
  15. }
  16. function adapter(dir: string) {
  17. return {
  18. name: dir,
  19. description: dir,
  20. configure(input: WorkspaceInfo) {
  21. return input
  22. },
  23. async create() {},
  24. async remove() {},
  25. target() {
  26. return {
  27. type: "local" as const,
  28. directory: dir,
  29. }
  30. },
  31. }
  32. }
  33. describe("control-plane/adapters", () => {
  34. test("isolates custom adapters by project", async () => {
  35. const type = `demo-${Math.random().toString(36).slice(2)}`
  36. const one = ProjectV2.ID.make(`project-${Math.random().toString(36).slice(2)}`)
  37. const two = ProjectV2.ID.make(`project-${Math.random().toString(36).slice(2)}`)
  38. registerAdapter(one, type, adapter("/one"))
  39. registerAdapter(two, type, adapter("/two"))
  40. expect(await (await getAdapter(one, type)).target(info(one, type))).toEqual({
  41. type: "local",
  42. directory: "/one",
  43. })
  44. expect(await (await getAdapter(two, type)).target(info(two, type))).toEqual({
  45. type: "local",
  46. directory: "/two",
  47. })
  48. })
  49. test("latest install wins within a project", async () => {
  50. const type = `demo-${Math.random().toString(36).slice(2)}`
  51. const id = ProjectV2.ID.make(`project-${Math.random().toString(36).slice(2)}`)
  52. registerAdapter(id, type, adapter("/one"))
  53. expect(await (await getAdapter(id, type)).target(info(id, type))).toEqual({
  54. type: "local",
  55. directory: "/one",
  56. })
  57. registerAdapter(id, type, adapter("/two"))
  58. expect(await (await getAdapter(id, type)).target(info(id, type))).toEqual({
  59. type: "local",
  60. directory: "/two",
  61. })
  62. })
  63. })