index.test.ts 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. import { describe, expect, spyOn } from "bun:test"
  2. import path from "path"
  3. import { Effect, Layer } from "effect"
  4. import { LSP } from "@/lsp/lsp"
  5. import * as LSPServer from "@/lsp/server"
  6. import { CrossSpawnSpawner } from "@opencode-ai/core/cross-spawn-spawner"
  7. import { provideTmpdirInstance } from "../fixture/fixture"
  8. import { testEffect } from "../lib/effect"
  9. const it = testEffect(Layer.mergeAll(LSP.defaultLayer, CrossSpawnSpawner.defaultLayer))
  10. describe("lsp.spawn", () => {
  11. it.live("does not spawn builtin LSP for files outside instance", () =>
  12. provideTmpdirInstance(
  13. (dir) =>
  14. LSP.Service.use((lsp) =>
  15. Effect.gen(function* () {
  16. const spy = spyOn(LSPServer.Typescript, "spawn").mockResolvedValue(undefined)
  17. try {
  18. yield* lsp.touchFile(path.join(dir, "..", "outside.ts"))
  19. yield* lsp.hover({
  20. file: path.join(dir, "..", "hover.ts"),
  21. line: 0,
  22. character: 0,
  23. })
  24. expect(spy).toHaveBeenCalledTimes(0)
  25. } finally {
  26. spy.mockRestore()
  27. }
  28. }),
  29. ),
  30. { config: { lsp: true } },
  31. ),
  32. )
  33. it.live("does not spawn builtin LSP for files inside instance when LSP is unset", () =>
  34. provideTmpdirInstance((dir) =>
  35. LSP.Service.use((lsp) =>
  36. Effect.gen(function* () {
  37. const spy = spyOn(LSPServer.Typescript, "spawn").mockResolvedValue(undefined)
  38. try {
  39. yield* lsp.hover({
  40. file: path.join(dir, "src", "inside.ts"),
  41. line: 0,
  42. character: 0,
  43. })
  44. expect(spy).toHaveBeenCalledTimes(0)
  45. } finally {
  46. spy.mockRestore()
  47. }
  48. }),
  49. ),
  50. ),
  51. )
  52. it.live("would spawn builtin LSP for files inside instance when lsp is true", () =>
  53. provideTmpdirInstance(
  54. (dir) =>
  55. LSP.Service.use((lsp) =>
  56. Effect.gen(function* () {
  57. const spy = spyOn(LSPServer.Typescript, "spawn").mockResolvedValue(undefined)
  58. try {
  59. yield* lsp.hover({
  60. file: path.join(dir, "src", "inside.ts"),
  61. line: 0,
  62. character: 0,
  63. })
  64. expect(spy).toHaveBeenCalledTimes(1)
  65. } finally {
  66. spy.mockRestore()
  67. }
  68. }),
  69. ),
  70. { config: { lsp: true } },
  71. ),
  72. )
  73. it.live("would spawn builtin LSP for files inside instance when config object is provided", () =>
  74. provideTmpdirInstance(
  75. (dir) =>
  76. LSP.Service.use((lsp) =>
  77. Effect.gen(function* () {
  78. const spy = spyOn(LSPServer.Typescript, "spawn").mockResolvedValue(undefined)
  79. try {
  80. yield* lsp.hover({
  81. file: path.join(dir, "src", "inside.ts"),
  82. line: 0,
  83. character: 0,
  84. })
  85. expect(spy).toHaveBeenCalledTimes(1)
  86. } finally {
  87. spy.mockRestore()
  88. }
  89. }),
  90. ),
  91. {
  92. config: {
  93. lsp: {
  94. eslint: { disabled: true },
  95. },
  96. },
  97. },
  98. ),
  99. )
  100. })