forked from anomalyco/opencode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.test.ts
More file actions
232 lines (214 loc) · 7.07 KB
/
Copy pathindex.test.ts
File metadata and controls
232 lines (214 loc) · 7.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
import { describe, expect, spyOn } from "bun:test"
import path from "path"
import { Deferred, Effect, Layer } from "effect"
import { EventV2Bridge } from "@/event-v2-bridge"
import { Config } from "@/config/config"
import { RuntimeFlags } from "@/effect/runtime-flags"
import { LSP } from "@/lsp/lsp"
import * as LSPServer from "@/lsp/server"
import { CrossSpawnSpawner } from "@opencode-ai/core/cross-spawn-spawner"
import { TestInstance } from "../fixture/fixture"
import { awaitWithTimeout, testEffect } from "../lib/effect"
const lspLayer = (flags: Parameters<typeof RuntimeFlags.layer>[0] = {}) =>
LSP.layer.pipe(
Layer.provide(Config.defaultLayer),
Layer.provide(RuntimeFlags.layer(flags)),
Layer.provideMerge(EventV2Bridge.defaultLayer),
)
const it = testEffect(Layer.mergeAll(lspLayer(), CrossSpawnSpawner.defaultLayer))
const experimentalTyIt = testEffect(
Layer.mergeAll(lspLayer({ experimentalLspTy: true }), CrossSpawnSpawner.defaultLayer),
)
const fakeServerPath = path.join(__dirname, "../fixture/lsp/fake-lsp-server.js")
const disabledDownloadIt = testEffect(
Layer.mergeAll(lspLayer({ disableLspDownload: true }), CrossSpawnSpawner.defaultLayer),
)
describe("lsp.spawn", () => {
it.instance(
"does not spawn builtin LSP for files outside instance",
() =>
LSP.Service.use((lsp) =>
Effect.gen(function* () {
const dir = (yield* TestInstance).directory
const spy = spyOn(LSPServer.Typescript, "spawn").mockResolvedValue(undefined)
try {
yield* lsp.touchFile(path.join(dir, "..", "outside.ts"))
yield* lsp.hover({
file: path.join(dir, "..", "hover.ts"),
line: 0,
character: 0,
})
expect(spy).toHaveBeenCalledTimes(0)
} finally {
spy.mockRestore()
}
}),
),
{ config: { lsp: true } },
)
it.instance("does not spawn builtin LSP for files inside instance when LSP is unset", () =>
LSP.Service.use((lsp) =>
Effect.gen(function* () {
const dir = (yield* TestInstance).directory
const spy = spyOn(LSPServer.Typescript, "spawn").mockResolvedValue(undefined)
try {
yield* lsp.hover({
file: path.join(dir, "src", "inside.ts"),
line: 0,
character: 0,
})
expect(spy).toHaveBeenCalledTimes(0)
} finally {
spy.mockRestore()
}
}),
),
)
it.instance(
"would spawn builtin LSP for files inside instance when lsp is true",
() =>
LSP.Service.use((lsp) =>
Effect.gen(function* () {
const dir = (yield* TestInstance).directory
const spy = spyOn(LSPServer.Typescript, "spawn").mockResolvedValue(undefined)
try {
yield* lsp.hover({
file: path.join(dir, "src", "inside.ts"),
line: 0,
character: 0,
})
expect(spy).toHaveBeenCalledTimes(1)
} finally {
spy.mockRestore()
}
}),
),
{ config: { lsp: true } },
)
it.instance(
"publishes lsp.updated after custom LSP initialization",
() =>
Effect.gen(function* () {
const dir = (yield* TestInstance).directory
const lsp = yield* LSP.Service
const updated = yield* Deferred.make<void>()
const events = yield* EventV2Bridge.Service
const unsubscribe = yield* events.listen((event) => {
if (event.type === LSP.Event.Updated.type) Deferred.doneUnsafe(updated, Effect.void)
return Effect.void
})
yield* Effect.addFinalizer(() => unsubscribe)
const file = path.join(dir, "sample.repro")
yield* Effect.promise(() => Bun.write(file, "sample\n"))
yield* lsp.touchFile(file)
yield* awaitWithTimeout(Deferred.await(updated), "lsp.updated event was not published")
}),
{
config: {
lsp: {
fake: {
command: [process.execPath, fakeServerPath],
extensions: [".repro"],
},
},
},
},
)
it.instance(
"would spawn builtin LSP for files inside instance when config object is provided",
() =>
LSP.Service.use((lsp) =>
Effect.gen(function* () {
const dir = (yield* TestInstance).directory
const spy = spyOn(LSPServer.Typescript, "spawn").mockResolvedValue(undefined)
try {
yield* lsp.hover({
file: path.join(dir, "src", "inside.ts"),
line: 0,
character: 0,
})
expect(spy).toHaveBeenCalledTimes(1)
} finally {
spy.mockRestore()
}
}),
),
{
config: {
lsp: {
eslint: { disabled: true },
},
},
},
)
it.instance(
"uses pyright instead of ty by default",
() =>
LSP.Service.use((lsp) =>
Effect.gen(function* () {
const dir = (yield* TestInstance).directory
const ty = spyOn(LSPServer.Ty, "spawn").mockResolvedValue(undefined)
const pyright = spyOn(LSPServer.Pyright, "spawn").mockResolvedValue(undefined)
try {
yield* lsp.hover({
file: path.join(dir, "src", "inside.py"),
line: 0,
character: 0,
})
expect(ty).toHaveBeenCalledTimes(0)
expect(pyright).toHaveBeenCalledTimes(1)
} finally {
ty.mockRestore()
pyright.mockRestore()
}
}),
),
{ config: { lsp: true } },
)
experimentalTyIt.instance(
"uses ty instead of pyright when experimentalLspTy is enabled",
() =>
LSP.Service.use((lsp) =>
Effect.gen(function* () {
const dir = (yield* TestInstance).directory
const ty = spyOn(LSPServer.Ty, "spawn").mockResolvedValue(undefined)
const pyright = spyOn(LSPServer.Pyright, "spawn").mockResolvedValue(undefined)
try {
yield* lsp.hover({
file: path.join(dir, "src", "inside.py"),
line: 0,
character: 0,
})
expect(ty).toHaveBeenCalledTimes(1)
expect(pyright).toHaveBeenCalledTimes(0)
} finally {
ty.mockRestore()
pyright.mockRestore()
}
}),
),
{ config: { lsp: true } },
)
disabledDownloadIt.instance(
"passes disableLspDownload to builtin LSP spawn",
() =>
LSP.Service.use((lsp) =>
Effect.gen(function* () {
const dir = (yield* TestInstance).directory
const pyright = spyOn(LSPServer.Pyright, "spawn").mockResolvedValue(undefined)
try {
yield* lsp.hover({
file: path.join(dir, "src", "inside.py"),
line: 0,
character: 0,
})
expect(pyright).toHaveBeenCalledTimes(1)
expect(pyright.mock.calls[0]?.[2]).toMatchObject({ disableLspDownload: true })
} finally {
pyright.mockRestore()
}
}),
),
{ config: { lsp: true } },
)
})