lifecycle.test.ts 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. import { afterEach, beforeEach, describe, expect, spyOn, test } from "bun:test"
  2. import path from "path"
  3. import { LayerNode } from "@kirincode-ai/core/effect/layer-node"
  4. import { Effect } from "effect"
  5. import { LSP } from "@/lsp/lsp"
  6. import * as LSPServer from "@/lsp/server"
  7. import { TestInstance } from "../fixture/fixture"
  8. import { testEffect } from "../lib/effect"
  9. const it = testEffect(LayerNode.compile(LSP.node))
  10. describe("LSP service lifecycle", () => {
  11. let spawnSpy: ReturnType<typeof spyOn>
  12. beforeEach(() => {
  13. spawnSpy = spyOn(LSPServer.Typescript, "spawn").mockResolvedValue(undefined)
  14. })
  15. afterEach(() => {
  16. spawnSpy.mockRestore()
  17. })
  18. it.instance("init() completes without error", () => LSP.Service.use((lsp) => lsp.init()))
  19. it.instance("status() returns empty array initially", () =>
  20. LSP.Service.use((lsp) =>
  21. Effect.gen(function* () {
  22. const result = yield* lsp.status()
  23. expect(Array.isArray(result)).toBe(true)
  24. expect(result.length).toBe(0)
  25. }),
  26. ),
  27. )
  28. it.instance("diagnostics() returns empty object initially", () =>
  29. LSP.Service.use((lsp) =>
  30. Effect.gen(function* () {
  31. const result = yield* lsp.diagnostics()
  32. expect(typeof result).toBe("object")
  33. expect(Object.keys(result).length).toBe(0)
  34. }),
  35. ),
  36. )
  37. it.instance("hasClients() returns false for .ts files in instance when LSP is unset", () =>
  38. LSP.Service.use((lsp) =>
  39. Effect.gen(function* () {
  40. const result = yield* lsp.hasClients(path.join((yield* TestInstance).directory, "test.ts"))
  41. expect(result).toBe(false)
  42. }),
  43. ),
  44. )
  45. it.instance(
  46. "hasClients() returns true for .ts files in instance when lsp is true",
  47. () =>
  48. LSP.Service.use((lsp) =>
  49. Effect.gen(function* () {
  50. const result = yield* lsp.hasClients(path.join((yield* TestInstance).directory, "test.ts"))
  51. expect(result).toBe(true)
  52. }),
  53. ),
  54. { config: { lsp: true } },
  55. )
  56. it.instance(
  57. "hasClients() keeps built-in LSPs when config object is provided",
  58. () =>
  59. LSP.Service.use((lsp) =>
  60. Effect.gen(function* () {
  61. const result = yield* lsp.hasClients(path.join((yield* TestInstance).directory, "test.ts"))
  62. expect(result).toBe(true)
  63. }),
  64. ),
  65. { config: { lsp: { eslint: { disabled: true } } } },
  66. )
  67. it.instance("hasClients() returns false for files outside instance", () =>
  68. LSP.Service.use((lsp) =>
  69. Effect.gen(function* () {
  70. const result = yield* lsp.hasClients(path.join((yield* TestInstance).directory, "..", "outside.ts"))
  71. expect(typeof result).toBe("boolean")
  72. }),
  73. ),
  74. )
  75. it.instance("workspaceSymbol() returns empty array with no clients", () =>
  76. LSP.Service.use((lsp) =>
  77. Effect.gen(function* () {
  78. const result = yield* lsp.workspaceSymbol("test")
  79. expect(Array.isArray(result)).toBe(true)
  80. expect(result.length).toBe(0)
  81. }),
  82. ),
  83. )
  84. it.instance("definition() returns empty array for unknown file", () =>
  85. LSP.Service.use((lsp) =>
  86. Effect.gen(function* () {
  87. const result = yield* lsp.definition({
  88. file: path.join((yield* TestInstance).directory, "nonexistent.ts"),
  89. line: 0,
  90. character: 0,
  91. })
  92. expect(Array.isArray(result)).toBe(true)
  93. }),
  94. ),
  95. )
  96. it.instance("references() returns empty array for unknown file", () =>
  97. LSP.Service.use((lsp) =>
  98. Effect.gen(function* () {
  99. const result = yield* lsp.references({
  100. file: path.join((yield* TestInstance).directory, "nonexistent.ts"),
  101. line: 0,
  102. character: 0,
  103. })
  104. expect(Array.isArray(result)).toBe(true)
  105. }),
  106. ),
  107. )
  108. it.instance("multiple init() calls are idempotent", () =>
  109. LSP.Service.use((lsp) =>
  110. Effect.gen(function* () {
  111. yield* lsp.init()
  112. yield* lsp.init()
  113. yield* lsp.init()
  114. }),
  115. ),
  116. )
  117. })
  118. describe("LSP.Diagnostic", () => {
  119. test("pretty() formats error diagnostic", () => {
  120. const result = LSP.Diagnostic.pretty({
  121. range: { start: { line: 9, character: 4 }, end: { line: 9, character: 10 } },
  122. message: "Type 'string' is not assignable to type 'number'",
  123. severity: 1,
  124. } as any)
  125. expect(result).toBe("ERROR [10:5] Type 'string' is not assignable to type 'number'")
  126. })
  127. test("pretty() formats warning diagnostic", () => {
  128. const result = LSP.Diagnostic.pretty({
  129. range: { start: { line: 0, character: 0 }, end: { line: 0, character: 5 } },
  130. message: "Unused variable",
  131. severity: 2,
  132. } as any)
  133. expect(result).toBe("WARN [1:1] Unused variable")
  134. })
  135. test("pretty() defaults to ERROR when no severity", () => {
  136. const result = LSP.Diagnostic.pretty({
  137. range: { start: { line: 0, character: 0 }, end: { line: 0, character: 1 } },
  138. message: "Something wrong",
  139. } as any)
  140. expect(result).toBe("ERROR [1:1] Something wrong")
  141. })
  142. })