dap-mcp-server 0.1.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (65) hide show
  1. package/.claude/settings.local.json +31 -0
  2. package/.forgejo/workflows/publish.yml +63 -0
  3. package/CLAUDE.md +71 -0
  4. package/LICENSE +21 -0
  5. package/README.md +105 -0
  6. package/SKILL.md +62 -0
  7. package/build/adapters/builtin.d.ts +24 -0
  8. package/build/adapters/builtin.js +46 -0
  9. package/build/adapters/builtin.js.map +1 -0
  10. package/build/adapters/detect.d.ts +23 -0
  11. package/build/adapters/detect.js +36 -0
  12. package/build/adapters/detect.js.map +1 -0
  13. package/build/adapters/registry.d.ts +39 -0
  14. package/build/adapters/registry.js +79 -0
  15. package/build/adapters/registry.js.map +1 -0
  16. package/build/dap/dap-client.d.ts +46 -0
  17. package/build/dap/dap-client.js +190 -0
  18. package/build/dap/dap-client.js.map +1 -0
  19. package/build/dap/protocol.d.ts +32 -0
  20. package/build/dap/protocol.js +71 -0
  21. package/build/dap/protocol.js.map +1 -0
  22. package/build/dap/ring-buffer.d.ts +35 -0
  23. package/build/dap/ring-buffer.js +57 -0
  24. package/build/dap/ring-buffer.js.map +1 -0
  25. package/build/dap/types.d.ts +87 -0
  26. package/build/dap/types.js +25 -0
  27. package/build/dap/types.js.map +1 -0
  28. package/build/index.d.ts +24 -0
  29. package/build/index.js +30 -0
  30. package/build/index.js.map +1 -0
  31. package/build/server.d.ts +24 -0
  32. package/build/server.js +735 -0
  33. package/build/server.js.map +1 -0
  34. package/build/session/session-manager.d.ts +32 -0
  35. package/build/session/session-manager.js +60 -0
  36. package/build/session/session-manager.js.map +1 -0
  37. package/build/session/session.d.ts +85 -0
  38. package/build/session/session.js +374 -0
  39. package/build/session/session.js.map +1 -0
  40. package/docs/plans/2026-03-05-dap-wrapper-mcp-design.md +326 -0
  41. package/docs/plans/2026-03-05-dap-wrapper-mcp-implementation.md +2815 -0
  42. package/package.json +43 -0
  43. package/src/adapters/builtin.ts +47 -0
  44. package/src/adapters/detect.test.ts +36 -0
  45. package/src/adapters/detect.ts +36 -0
  46. package/src/adapters/registry.test.ts +73 -0
  47. package/src/adapters/registry.ts +86 -0
  48. package/src/dap/dap-client.test.ts +87 -0
  49. package/src/dap/dap-client.ts +216 -0
  50. package/src/dap/protocol.test.ts +82 -0
  51. package/src/dap/protocol.ts +78 -0
  52. package/src/dap/ring-buffer.test.ts +82 -0
  53. package/src/dap/ring-buffer.ts +64 -0
  54. package/src/dap/types.ts +92 -0
  55. package/src/index.ts +30 -0
  56. package/src/integration/debug-lifecycle.test.ts +107 -0
  57. package/src/server.ts +866 -0
  58. package/src/session/session-manager.test.ts +74 -0
  59. package/src/session/session-manager.ts +67 -0
  60. package/src/session/session.test.ts +84 -0
  61. package/src/session/session.ts +435 -0
  62. package/src/test-fixtures/crash-adapter.js +23 -0
  63. package/src/test-fixtures/mock-adapter.js +142 -0
  64. package/src/test-fixtures/silent-adapter.js +24 -0
  65. package/tsconfig.json +16 -0
@@ -0,0 +1,82 @@
1
+ /**
2
+ * Copyright (c) 2026 Ivan Iraci <ivan.iraci@professioneit.com>
3
+ *
4
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
5
+ * of this software and associated documentation files (the "Software"), to deal
6
+ * in the Software without restriction, including without limitation the rights
7
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8
+ * copies of the Software, and to permit persons to whom the Software is
9
+ * furnished to do so, subject to the following conditions:
10
+ *
11
+ * The above copyright notice and this permission notice shall be included in
12
+ * all copies or substantial portions of the Software.
13
+ *
14
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20
+ * SOFTWARE.
21
+ */
22
+
23
+ import { describe, it, expect } from "vitest";
24
+ import { encodeMessage, MessageParser } from "./protocol.js";
25
+ import { DebugProtocol } from "@vscode/debugprotocol";
26
+
27
+ describe("encodeMessage", () => {
28
+ it("encodes a DAP message with Content-Length header", () => {
29
+ const msg: DebugProtocol.Request = {
30
+ seq: 1,
31
+ type: "request",
32
+ command: "initialize",
33
+ arguments: { adapterID: "test" },
34
+ };
35
+ const encoded = encodeMessage(msg);
36
+ const str = encoded.toString("utf-8");
37
+ const json = JSON.stringify(msg);
38
+ expect(str).toBe(`Content-Length: ${Buffer.byteLength(json)}\r\n\r\n${json}`);
39
+ });
40
+ });
41
+
42
+ describe("MessageParser", () => {
43
+ it("parses a single complete message", () => {
44
+ const messages: DebugProtocol.ProtocolMessage[] = [];
45
+ const parser = new MessageParser((msg) => messages.push(msg));
46
+
47
+ const body = JSON.stringify({ seq: 1, type: "response", command: "initialize", success: true, request_seq: 1 });
48
+ const raw = `Content-Length: ${Buffer.byteLength(body)}\r\n\r\n${body}`;
49
+ parser.feed(Buffer.from(raw));
50
+
51
+ expect(messages).toHaveLength(1);
52
+ expect(messages[0].seq).toBe(1);
53
+ });
54
+
55
+ it("handles messages split across multiple chunks", () => {
56
+ const messages: DebugProtocol.ProtocolMessage[] = [];
57
+ const parser = new MessageParser((msg) => messages.push(msg));
58
+
59
+ const body = JSON.stringify({ seq: 1, type: "event", event: "initialized" });
60
+ const raw = `Content-Length: ${Buffer.byteLength(body)}\r\n\r\n${body}`;
61
+ const mid = Math.floor(raw.length / 2);
62
+
63
+ parser.feed(Buffer.from(raw.slice(0, mid)));
64
+ expect(messages).toHaveLength(0);
65
+ parser.feed(Buffer.from(raw.slice(mid)));
66
+ expect(messages).toHaveLength(1);
67
+ });
68
+
69
+ it("handles multiple messages in one chunk", () => {
70
+ const messages: DebugProtocol.ProtocolMessage[] = [];
71
+ const parser = new MessageParser((msg) => messages.push(msg));
72
+
73
+ const body1 = JSON.stringify({ seq: 1, type: "event", event: "initialized" });
74
+ const body2 = JSON.stringify({ seq: 2, type: "event", event: "stopped" });
75
+ const raw = `Content-Length: ${Buffer.byteLength(body1)}\r\n\r\n${body1}Content-Length: ${Buffer.byteLength(body2)}\r\n\r\n${body2}`;
76
+ parser.feed(Buffer.from(raw));
77
+
78
+ expect(messages).toHaveLength(2);
79
+ expect(messages[0].seq).toBe(1);
80
+ expect(messages[1].seq).toBe(2);
81
+ });
82
+ });
@@ -0,0 +1,78 @@
1
+ /**
2
+ * Copyright (c) 2026 Ivan Iraci <ivan.iraci@professioneit.com>
3
+ *
4
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
5
+ * of this software and associated documentation files (the "Software"), to deal
6
+ * in the Software without restriction, including without limitation the rights
7
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8
+ * copies of the Software, and to permit persons to whom the Software is
9
+ * furnished to do so, subject to the following conditions:
10
+ *
11
+ * The above copyright notice and this permission notice shall be included in
12
+ * all copies or substantial portions of the Software.
13
+ *
14
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20
+ * SOFTWARE.
21
+ */
22
+
23
+ import { DebugProtocol } from "@vscode/debugprotocol";
24
+
25
+ const TWO_CRLF = "\r\n\r\n";
26
+ const CONTENT_LENGTH = "Content-Length: ";
27
+
28
+ export function encodeMessage(msg: DebugProtocol.ProtocolMessage): Buffer {
29
+ const json = JSON.stringify(msg);
30
+ const header = `${CONTENT_LENGTH}${Buffer.byteLength(json)}${TWO_CRLF}`;
31
+ return Buffer.from(header + json, "utf-8");
32
+ }
33
+
34
+ export class MessageParser {
35
+ private buffer = Buffer.alloc(0);
36
+ private contentLength = -1;
37
+ private onMessage: (msg: DebugProtocol.ProtocolMessage) => void;
38
+
39
+ constructor(onMessage: (msg: DebugProtocol.ProtocolMessage) => void) {
40
+ this.onMessage = onMessage;
41
+ }
42
+
43
+ feed(data: Buffer): void {
44
+ this.buffer = Buffer.concat([this.buffer, data]);
45
+ this.parse();
46
+ }
47
+
48
+ private parse(): void {
49
+ while (true) {
50
+ if (this.contentLength === -1) {
51
+ const headerEnd = this.buffer.indexOf(TWO_CRLF);
52
+ if (headerEnd === -1) return;
53
+
54
+ const header = this.buffer.subarray(0, headerEnd).toString("utf-8");
55
+ const match = header.match(/Content-Length:\s*(\d+)/i);
56
+ if (!match) {
57
+ this.buffer = this.buffer.subarray(headerEnd + TWO_CRLF.length);
58
+ continue;
59
+ }
60
+ this.contentLength = parseInt(match[1], 10);
61
+ this.buffer = this.buffer.subarray(headerEnd + TWO_CRLF.length);
62
+ }
63
+
64
+ if (this.buffer.length < this.contentLength) return;
65
+
66
+ const body = this.buffer.subarray(0, this.contentLength).toString("utf-8");
67
+ this.buffer = this.buffer.subarray(this.contentLength);
68
+ this.contentLength = -1;
69
+
70
+ try {
71
+ const msg = JSON.parse(body) as DebugProtocol.ProtocolMessage;
72
+ this.onMessage(msg);
73
+ } catch {
74
+ // Skip malformed JSON
75
+ }
76
+ }
77
+ }
78
+ }
@@ -0,0 +1,82 @@
1
+ /**
2
+ * Copyright (c) 2026 Ivan Iraci <ivan.iraci@professioneit.com>
3
+ *
4
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
5
+ * of this software and associated documentation files (the "Software"), to deal
6
+ * in the Software without restriction, including without limitation the rights
7
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8
+ * copies of the Software, and to permit persons to whom the Software is
9
+ * furnished to do so, subject to the following conditions:
10
+ *
11
+ * The above copyright notice and this permission notice shall be included in
12
+ * all copies or substantial portions of the Software.
13
+ *
14
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20
+ * SOFTWARE.
21
+ */
22
+
23
+ import { describe, it, expect } from "vitest";
24
+ import { RingBuffer } from "./ring-buffer.js";
25
+
26
+ describe("RingBuffer", () => {
27
+ it("stores and retrieves entries", () => {
28
+ const buf = new RingBuffer<string>(100);
29
+ buf.push("a");
30
+ buf.push("b");
31
+ expect(buf.getAll()).toEqual(["a", "b"]);
32
+ expect(buf.truncated).toBe(false);
33
+ });
34
+
35
+ it("drops oldest entries when full", () => {
36
+ const buf = new RingBuffer<string>(3);
37
+ buf.push("a");
38
+ buf.push("b");
39
+ buf.push("c");
40
+ buf.push("d");
41
+ expect(buf.getAll()).toEqual(["b", "c", "d"]);
42
+ expect(buf.truncated).toBe(true);
43
+ });
44
+
45
+ it("clears entries", () => {
46
+ const buf = new RingBuffer<string>(10);
47
+ buf.push("a");
48
+ buf.push("b");
49
+ buf.clear();
50
+ expect(buf.getAll()).toEqual([]);
51
+ expect(buf.truncated).toBe(false);
52
+ });
53
+
54
+ it("filters entries", () => {
55
+ const buf = new RingBuffer<{ cat: string; text: string }>(10);
56
+ buf.push({ cat: "stdout", text: "hello" });
57
+ buf.push({ cat: "stderr", text: "error" });
58
+ buf.push({ cat: "stdout", text: "world" });
59
+ const filtered = buf.getFiltered((e) => e.cat === "stdout");
60
+ expect(filtered).toEqual([
61
+ { cat: "stdout", text: "hello" },
62
+ { cat: "stdout", text: "world" },
63
+ ]);
64
+ });
65
+
66
+ it("reports size correctly", () => {
67
+ const buf = new RingBuffer<string>(5);
68
+ expect(buf.size).toBe(0);
69
+ buf.push("a");
70
+ buf.push("b");
71
+ expect(buf.size).toBe(2);
72
+ });
73
+
74
+ it("removes only matching entries", () => {
75
+ const buf = new RingBuffer<{ cat: string; text: string }>(10);
76
+ buf.push({ cat: "stdout", text: "hello" });
77
+ buf.push({ cat: "stderr", text: "error" });
78
+ buf.push({ cat: "stdout", text: "world" });
79
+ buf.removeMatching((e) => e.cat === "stdout");
80
+ expect(buf.getAll()).toEqual([{ cat: "stderr", text: "error" }]);
81
+ });
82
+ });
@@ -0,0 +1,64 @@
1
+ /**
2
+ * Copyright (c) 2026 Ivan Iraci <ivan.iraci@professioneit.com>
3
+ *
4
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
5
+ * of this software and associated documentation files (the "Software"), to deal
6
+ * in the Software without restriction, including without limitation the rights
7
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8
+ * copies of the Software, and to permit persons to whom the Software is
9
+ * furnished to do so, subject to the following conditions:
10
+ *
11
+ * The above copyright notice and this permission notice shall be included in
12
+ * all copies or substantial portions of the Software.
13
+ *
14
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20
+ * SOFTWARE.
21
+ */
22
+
23
+ export class RingBuffer<T> {
24
+ private buffer: T[] = [];
25
+ private maxSize: number;
26
+ private _truncated = false;
27
+
28
+ constructor(maxSize: number) {
29
+ this.maxSize = maxSize;
30
+ }
31
+
32
+ push(item: T): void {
33
+ if (this.buffer.length >= this.maxSize) {
34
+ this.buffer.shift();
35
+ this._truncated = true;
36
+ }
37
+ this.buffer.push(item);
38
+ }
39
+
40
+ getAll(): T[] {
41
+ return [...this.buffer];
42
+ }
43
+
44
+ getFiltered(predicate: (item: T) => boolean): T[] {
45
+ return this.buffer.filter(predicate);
46
+ }
47
+
48
+ clear(): void {
49
+ this.buffer = [];
50
+ this._truncated = false;
51
+ }
52
+
53
+ removeMatching(predicate: (item: T) => boolean): void {
54
+ this.buffer = this.buffer.filter((item) => !predicate(item));
55
+ }
56
+
57
+ get size(): number {
58
+ return this.buffer.length;
59
+ }
60
+
61
+ get truncated(): boolean {
62
+ return this._truncated;
63
+ }
64
+ }
@@ -0,0 +1,92 @@
1
+ /**
2
+ * Copyright (c) 2026 Ivan Iraci <ivan.iraci@professioneit.com>
3
+ *
4
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
5
+ * of this software and associated documentation files (the "Software"), to deal
6
+ * in the Software without restriction, including without limitation the rights
7
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8
+ * copies of the Software, and to permit persons to whom the Software is
9
+ * furnished to do so, subject to the following conditions:
10
+ *
11
+ * The above copyright notice and this permission notice shall be included in
12
+ * all copies or substantial portions of the Software.
13
+ *
14
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20
+ * SOFTWARE.
21
+ */
22
+
23
+ // src/dap/types.ts
24
+
25
+ export type SessionState = "idle" | "configuring" | "running" | "stopped" | "terminated";
26
+
27
+ export interface StoppedState {
28
+ status: "stopped";
29
+ reason: string;
30
+ location: {
31
+ file: string;
32
+ line: number;
33
+ column?: number;
34
+ functionName?: string;
35
+ };
36
+ threads: Array<{ id: number; name: string; stopped: boolean }>;
37
+ stackTrace: Array<{
38
+ id: number;
39
+ name: string;
40
+ file: string;
41
+ line: number;
42
+ column?: number;
43
+ }>;
44
+ scopes: Array<{
45
+ name: string;
46
+ variables: Array<{
47
+ name: string;
48
+ value: string;
49
+ type?: string;
50
+ hasChildren: boolean;
51
+ variablesReference: number;
52
+ }>;
53
+ }>;
54
+ output?: string[];
55
+ }
56
+
57
+ export interface RunningState {
58
+ status: "running";
59
+ output?: string[];
60
+ elapsedMs?: number;
61
+ }
62
+
63
+ export type DebugState = StoppedState | RunningState;
64
+
65
+ export interface AdapterConfig {
66
+ type: string;
67
+ runtime?: string;
68
+ program: string;
69
+ args?: string[];
70
+ launchDefaults?: Record<string, unknown>;
71
+ installHint?: string;
72
+ }
73
+
74
+ export interface OutputEntry {
75
+ category: string;
76
+ text: string;
77
+ timestamp: string;
78
+ }
79
+
80
+ export interface BreakpointConfig {
81
+ line: number;
82
+ condition?: string;
83
+ hitCondition?: string;
84
+ logMessage?: string;
85
+ }
86
+
87
+ export interface BreakpointResult {
88
+ id?: number;
89
+ verified: boolean;
90
+ line: number;
91
+ message?: string;
92
+ }
package/src/index.ts ADDED
@@ -0,0 +1,30 @@
1
+ #!/usr/bin/env node
2
+ /**
3
+ * Copyright (c) 2026 Ivan Iraci <ivan.iraci@professioneit.com>
4
+ *
5
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ * of this software and associated documentation files (the "Software"), to deal
7
+ * in the Software without restriction, including without limitation the rights
8
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ * copies of the Software, and to permit persons to whom the Software is
10
+ * furnished to do so, subject to the following conditions:
11
+ *
12
+ * The above copyright notice and this permission notice shall be included in
13
+ * all copies or substantial portions of the Software.
14
+ *
15
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ * SOFTWARE.
22
+ */
23
+
24
+ import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
25
+ import { createServer } from "./server.js";
26
+
27
+ const server = createServer();
28
+ const transport = new StdioServerTransport();
29
+ await server.connect(transport);
30
+ console.error("dap-mcp-server running on stdio");
@@ -0,0 +1,107 @@
1
+ /**
2
+ * Copyright (c) 2026 Ivan Iraci <ivan.iraci@professioneit.com>
3
+ *
4
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
5
+ * of this software and associated documentation files (the "Software"), to deal
6
+ * in the Software without restriction, including without limitation the rights
7
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8
+ * copies of the Software, and to permit persons to whom the Software is
9
+ * furnished to do so, subject to the following conditions:
10
+ *
11
+ * The above copyright notice and this permission notice shall be included in
12
+ * all copies or substantial portions of the Software.
13
+ *
14
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20
+ * SOFTWARE.
21
+ */
22
+
23
+ import { describe, it, expect, afterEach } from "vitest";
24
+ import { DebugSession } from "../session/session.js";
25
+ import path from "node:path";
26
+ import { fileURLToPath } from "node:url";
27
+
28
+ const __dirname = path.dirname(fileURLToPath(import.meta.url));
29
+ const MOCK_ADAPTER = path.join(__dirname, "../test-fixtures/mock-adapter.js");
30
+
31
+ describe("Debug Lifecycle Integration", () => {
32
+ let session: DebugSession | undefined;
33
+
34
+ afterEach(async () => {
35
+ if (session && session.state !== "terminated") {
36
+ try { await session.disconnect(); } catch { /* ignore */ }
37
+ }
38
+ session = undefined;
39
+ });
40
+
41
+ it("full debug lifecycle: launch, stop, inspect, step, evaluate, disconnect", async () => {
42
+ session = new DebugSession("integ-1", { type: "mock", runtime: "node", program: MOCK_ADAPTER });
43
+
44
+ // Initialize
45
+ const caps = await session.initialize();
46
+ expect(caps.supportsConfigurationDoneRequest).toBe(true);
47
+ expect(session.state).toBe("configuring");
48
+
49
+ // Set breakpoints
50
+ const bps = await session.setBreakpoints("/test/app.py", [{ line: 5 }, { line: 10 }]);
51
+ expect(bps).toHaveLength(2);
52
+ expect(bps[0].verified).toBe(true);
53
+
54
+ // Configuration done + launch
55
+ await session.configurationDone();
56
+ await session.launch({ program: "/test/app.py", stopOnEntry: true });
57
+
58
+ // Wait for stopped on entry
59
+ const stoppedEvent = await session.waitForStopped(5000);
60
+ expect(stoppedEvent.body.reason).toBe("entry");
61
+ expect(session.state).toBe("stopped");
62
+
63
+ // Collect composite state
64
+ const state = await session.collectState(1, 10, 1);
65
+ expect(state.status).toBe("stopped");
66
+ expect(state.reason).toBe("entry");
67
+ expect(state.threads).toHaveLength(1);
68
+ expect(state.threads[0].name).toBe("MainThread");
69
+ expect(state.stackTrace.length).toBeGreaterThan(0);
70
+ expect(state.scopes.length).toBeGreaterThan(0);
71
+
72
+ // Check variables
73
+ const locals = state.scopes.find(s => s.name === "Locals");
74
+ expect(locals).toBeDefined();
75
+ expect(locals!.variables.some(v => v.name === "x" && v.value === "42")).toBe(true);
76
+ expect(locals!.variables.some(v => v.name === "name")).toBe(true);
77
+
78
+ // Step next
79
+ await session.next(1);
80
+ const stoppedAfterStep = await session.waitForStopped(5000);
81
+ expect(stoppedAfterStep.body.reason).toBe("step");
82
+
83
+ // Evaluate expression
84
+ const evalResult = await session.evaluate("x + 1", 1);
85
+ expect(evalResult.result).toBe("42");
86
+
87
+ // Continue
88
+ await session.continueExecution(1);
89
+ const stoppedAtBp = await session.waitForStopped(5000);
90
+ expect(stoppedAtBp.body.reason).toBe("breakpoint");
91
+
92
+ // Check output
93
+ const output = session.getOutput();
94
+ expect(output.output.some(o => o.text.includes("Program started"))).toBe(true);
95
+
96
+ // Disconnect
97
+ await session.disconnect();
98
+ expect(session.state).toBe("terminated");
99
+ });
100
+
101
+ it("handles adapter crash gracefully", async () => {
102
+ const crashAdapter = path.join(__dirname, "../test-fixtures/crash-adapter.js");
103
+ session = new DebugSession("integ-crash", { type: "mock", runtime: "node", program: crashAdapter });
104
+ await expect(session.initialize()).rejects.toThrow();
105
+ expect(session.state).toBe("terminated");
106
+ });
107
+ });