@dexto/tools-plan 1.6.0 → 1.6.2

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 (38) hide show
  1. package/dist/errors.cjs +126 -0
  2. package/dist/errors.js +99 -64
  3. package/dist/index.cjs +36 -0
  4. package/dist/index.d.cts +224 -0
  5. package/dist/index.js +9 -12
  6. package/dist/plan-service-getter.cjs +16 -0
  7. package/dist/plan-service-getter.js +0 -1
  8. package/dist/plan-service.cjs +247 -0
  9. package/dist/plan-service.js +201 -215
  10. package/dist/plan-service.test.cjs +227 -0
  11. package/dist/plan-service.test.js +200 -222
  12. package/dist/tool-factory-config.cjs +38 -0
  13. package/dist/tool-factory-config.js +13 -30
  14. package/dist/tool-factory.cjs +71 -0
  15. package/dist/tool-factory.js +39 -35
  16. package/dist/tool-factory.test.cjs +96 -0
  17. package/dist/tool-factory.test.js +90 -95
  18. package/dist/tools/plan-create-tool.cjs +102 -0
  19. package/dist/tools/plan-create-tool.d.ts.map +1 -1
  20. package/dist/tools/plan-create-tool.js +77 -82
  21. package/dist/tools/plan-create-tool.test.cjs +174 -0
  22. package/dist/tools/plan-create-tool.test.js +142 -134
  23. package/dist/tools/plan-read-tool.cjs +65 -0
  24. package/dist/tools/plan-read-tool.d.ts.map +1 -1
  25. package/dist/tools/plan-read-tool.js +39 -41
  26. package/dist/tools/plan-read-tool.test.cjs +109 -0
  27. package/dist/tools/plan-read-tool.test.js +78 -87
  28. package/dist/tools/plan-review-tool.cjs +98 -0
  29. package/dist/tools/plan-review-tool.d.ts.map +1 -1
  30. package/dist/tools/plan-review-tool.js +73 -87
  31. package/dist/tools/plan-update-tool.cjs +92 -0
  32. package/dist/tools/plan-update-tool.d.ts.map +1 -1
  33. package/dist/tools/plan-update-tool.js +65 -73
  34. package/dist/tools/plan-update-tool.test.cjs +203 -0
  35. package/dist/tools/plan-update-tool.test.js +171 -154
  36. package/dist/types.cjs +44 -0
  37. package/dist/types.js +17 -24
  38. package/package.json +8 -7
@@ -0,0 +1,227 @@
1
+ "use strict";
2
+ var __create = Object.create;
3
+ var __defProp = Object.defineProperty;
4
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
+ var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __getProtoOf = Object.getPrototypeOf;
7
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
8
+ var __copyProps = (to, from, except, desc) => {
9
+ if (from && typeof from === "object" || typeof from === "function") {
10
+ for (let key of __getOwnPropNames(from))
11
+ if (!__hasOwnProp.call(to, key) && key !== except)
12
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
13
+ }
14
+ return to;
15
+ };
16
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
17
+ // If the importer is in node compatibility mode or this is not an ESM
18
+ // file that has been converted to a CommonJS file using a Babel-
19
+ // compatible transform (i.e. "__esModule" has not been set), then set
20
+ // "default" to the CommonJS "module.exports" for node compatibility.
21
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
22
+ mod
23
+ ));
24
+ var import_vitest = require("vitest");
25
+ var path = __toESM(require("node:path"), 1);
26
+ var fs = __toESM(require("node:fs/promises"), 1);
27
+ var os = __toESM(require("node:os"), 1);
28
+ var import_plan_service = require("./plan-service.js");
29
+ var import_errors = require("./errors.js");
30
+ var import_core = require("@dexto/core");
31
+ const createMockLogger = () => ({
32
+ debug: import_vitest.vi.fn(),
33
+ silly: import_vitest.vi.fn(),
34
+ info: import_vitest.vi.fn(),
35
+ warn: import_vitest.vi.fn(),
36
+ error: import_vitest.vi.fn(),
37
+ trackException: import_vitest.vi.fn(),
38
+ createChild: import_vitest.vi.fn().mockReturnThis(),
39
+ setLevel: import_vitest.vi.fn(),
40
+ getLevel: import_vitest.vi.fn(() => "debug"),
41
+ getLogFilePath: import_vitest.vi.fn(() => null),
42
+ destroy: import_vitest.vi.fn(async () => void 0)
43
+ });
44
+ (0, import_vitest.describe)("PlanService", () => {
45
+ let mockLogger;
46
+ let tempDir;
47
+ let planService;
48
+ (0, import_vitest.beforeEach)(async () => {
49
+ mockLogger = createMockLogger();
50
+ const rawTempDir = await fs.mkdtemp(path.join(os.tmpdir(), "dexto-plan-test-"));
51
+ tempDir = await fs.realpath(rawTempDir);
52
+ planService = new import_plan_service.PlanService({ basePath: tempDir }, mockLogger);
53
+ import_vitest.vi.clearAllMocks();
54
+ });
55
+ (0, import_vitest.afterEach)(async () => {
56
+ try {
57
+ await fs.rm(tempDir, { recursive: true, force: true });
58
+ } catch {
59
+ }
60
+ });
61
+ (0, import_vitest.describe)("exists", () => {
62
+ (0, import_vitest.it)("should return false for non-existent plan", async () => {
63
+ const exists = await planService.exists("non-existent-session");
64
+ (0, import_vitest.expect)(exists).toBe(false);
65
+ });
66
+ (0, import_vitest.it)("should return true for existing plan", async () => {
67
+ const sessionId = "test-session";
68
+ await planService.create(sessionId, "# Test Plan");
69
+ const exists = await planService.exists(sessionId);
70
+ (0, import_vitest.expect)(exists).toBe(true);
71
+ });
72
+ });
73
+ (0, import_vitest.describe)("create", () => {
74
+ (0, import_vitest.it)("should create a new plan with content and metadata", async () => {
75
+ const sessionId = "test-session";
76
+ const content = "# Implementation Plan\n\n## Steps\n1. First step";
77
+ const title = "Test Plan";
78
+ const plan = await planService.create(sessionId, content, { title });
79
+ (0, import_vitest.expect)(plan.content).toBe(content);
80
+ (0, import_vitest.expect)(plan.meta.sessionId).toBe(sessionId);
81
+ (0, import_vitest.expect)(plan.meta.status).toBe("draft");
82
+ (0, import_vitest.expect)(plan.meta.title).toBe(title);
83
+ (0, import_vitest.expect)(plan.meta.createdAt).toBeGreaterThan(0);
84
+ (0, import_vitest.expect)(plan.meta.updatedAt).toBeGreaterThan(0);
85
+ });
86
+ (0, import_vitest.it)("should throw error when plan already exists", async () => {
87
+ const sessionId = "test-session";
88
+ await planService.create(sessionId, "# First Plan");
89
+ try {
90
+ await planService.create(sessionId, "# Second Plan");
91
+ import_vitest.expect.fail("Should have thrown an error");
92
+ } catch (error) {
93
+ (0, import_vitest.expect)(error).toBeInstanceOf(import_core.DextoRuntimeError);
94
+ (0, import_vitest.expect)(error.code).toBe(import_errors.PlanErrorCode.PLAN_ALREADY_EXISTS);
95
+ }
96
+ });
97
+ (0, import_vitest.it)("should store plan files on disk", async () => {
98
+ const sessionId = "test-session";
99
+ const content = "# Test Plan";
100
+ await planService.create(sessionId, content);
101
+ const planPath = path.join(tempDir, sessionId, "plan.md");
102
+ const storedContent = await fs.readFile(planPath, "utf-8");
103
+ (0, import_vitest.expect)(storedContent).toBe(content);
104
+ const metaPath = path.join(tempDir, sessionId, "plan-meta.json");
105
+ const metaContent = await fs.readFile(metaPath, "utf-8");
106
+ const meta = JSON.parse(metaContent);
107
+ (0, import_vitest.expect)(meta.sessionId).toBe(sessionId);
108
+ });
109
+ });
110
+ (0, import_vitest.describe)("read", () => {
111
+ (0, import_vitest.it)("should return null for non-existent plan", async () => {
112
+ const plan = await planService.read("non-existent-session");
113
+ (0, import_vitest.expect)(plan).toBeNull();
114
+ });
115
+ (0, import_vitest.it)("should read existing plan with content and metadata", async () => {
116
+ const sessionId = "test-session";
117
+ const content = "# Test Plan";
118
+ const title = "My Plan";
119
+ await planService.create(sessionId, content, { title });
120
+ const plan = await planService.read(sessionId);
121
+ (0, import_vitest.expect)(plan).not.toBeNull();
122
+ (0, import_vitest.expect)(plan.content).toBe(content);
123
+ (0, import_vitest.expect)(plan.meta.sessionId).toBe(sessionId);
124
+ (0, import_vitest.expect)(plan.meta.title).toBe(title);
125
+ });
126
+ (0, import_vitest.it)("should handle invalid metadata schema gracefully", async () => {
127
+ const sessionId = "test-session";
128
+ await planService.create(sessionId, "# Test");
129
+ const metaPath = path.join(tempDir, sessionId, "plan-meta.json");
130
+ await fs.writeFile(metaPath, JSON.stringify({ invalidField: "value" }));
131
+ const plan = await planService.read(sessionId);
132
+ (0, import_vitest.expect)(plan).not.toBeNull();
133
+ (0, import_vitest.expect)(plan.meta.sessionId).toBe(sessionId);
134
+ (0, import_vitest.expect)(plan.meta.status).toBe("draft");
135
+ (0, import_vitest.expect)(mockLogger.warn).toHaveBeenCalled();
136
+ });
137
+ (0, import_vitest.it)("should return null for corrupted JSON metadata", async () => {
138
+ const sessionId = "test-session";
139
+ await planService.create(sessionId, "# Test");
140
+ const metaPath = path.join(tempDir, sessionId, "plan-meta.json");
141
+ await fs.writeFile(metaPath, "{ invalid json }");
142
+ const plan = await planService.read(sessionId);
143
+ (0, import_vitest.expect)(plan).toBeNull();
144
+ (0, import_vitest.expect)(mockLogger.error).toHaveBeenCalled();
145
+ });
146
+ });
147
+ (0, import_vitest.describe)("update", () => {
148
+ (0, import_vitest.it)("should update plan content", async () => {
149
+ const sessionId = "test-session";
150
+ await planService.create(sessionId, "# Original Content");
151
+ const result = await planService.update(sessionId, "# Updated Content");
152
+ (0, import_vitest.expect)(result.oldContent).toBe("# Original Content");
153
+ (0, import_vitest.expect)(result.newContent).toBe("# Updated Content");
154
+ (0, import_vitest.expect)(result.meta.updatedAt).toBeGreaterThan(0);
155
+ });
156
+ (0, import_vitest.it)("should preserve metadata when updating content", async () => {
157
+ const sessionId = "test-session";
158
+ const plan = await planService.create(sessionId, "# Original", { title: "My Title" });
159
+ const originalCreatedAt = plan.meta.createdAt;
160
+ await planService.update(sessionId, "# Updated");
161
+ const updatedPlan = await planService.read(sessionId);
162
+ (0, import_vitest.expect)(updatedPlan.meta.title).toBe("My Title");
163
+ (0, import_vitest.expect)(updatedPlan.meta.createdAt).toBe(originalCreatedAt);
164
+ });
165
+ (0, import_vitest.it)("should throw error when plan does not exist", async () => {
166
+ try {
167
+ await planService.update("non-existent", "# Content");
168
+ import_vitest.expect.fail("Should have thrown an error");
169
+ } catch (error) {
170
+ (0, import_vitest.expect)(error).toBeInstanceOf(import_core.DextoRuntimeError);
171
+ (0, import_vitest.expect)(error.code).toBe(import_errors.PlanErrorCode.PLAN_NOT_FOUND);
172
+ }
173
+ });
174
+ });
175
+ (0, import_vitest.describe)("updateMeta", () => {
176
+ (0, import_vitest.it)("should update plan status", async () => {
177
+ const sessionId = "test-session";
178
+ await planService.create(sessionId, "# Plan");
179
+ const meta = await planService.updateMeta(sessionId, { status: "approved" });
180
+ (0, import_vitest.expect)(meta.status).toBe("approved");
181
+ });
182
+ (0, import_vitest.it)("should update plan title", async () => {
183
+ const sessionId = "test-session";
184
+ await planService.create(sessionId, "# Plan");
185
+ const meta = await planService.updateMeta(sessionId, { title: "New Title" });
186
+ (0, import_vitest.expect)(meta.title).toBe("New Title");
187
+ });
188
+ (0, import_vitest.it)("should throw error when plan does not exist", async () => {
189
+ try {
190
+ await planService.updateMeta("non-existent", { status: "approved" });
191
+ import_vitest.expect.fail("Should have thrown an error");
192
+ } catch (error) {
193
+ (0, import_vitest.expect)(error).toBeInstanceOf(import_core.DextoRuntimeError);
194
+ (0, import_vitest.expect)(error.code).toBe(import_errors.PlanErrorCode.PLAN_NOT_FOUND);
195
+ }
196
+ });
197
+ });
198
+ (0, import_vitest.describe)("delete", () => {
199
+ (0, import_vitest.it)("should delete existing plan", async () => {
200
+ const sessionId = "test-session";
201
+ await planService.create(sessionId, "# Plan");
202
+ await planService.delete(sessionId);
203
+ const exists = await planService.exists(sessionId);
204
+ (0, import_vitest.expect)(exists).toBe(false);
205
+ });
206
+ (0, import_vitest.it)("should throw error when plan does not exist", async () => {
207
+ try {
208
+ await planService.delete("non-existent");
209
+ import_vitest.expect.fail("Should have thrown an error");
210
+ } catch (error) {
211
+ (0, import_vitest.expect)(error).toBeInstanceOf(import_core.DextoRuntimeError);
212
+ (0, import_vitest.expect)(error.code).toBe(import_errors.PlanErrorCode.PLAN_NOT_FOUND);
213
+ }
214
+ });
215
+ (0, import_vitest.it)("should remove plan directory from disk", async () => {
216
+ const sessionId = "test-session";
217
+ await planService.create(sessionId, "# Plan");
218
+ const planDir = path.join(tempDir, sessionId);
219
+ await planService.delete(sessionId);
220
+ try {
221
+ await fs.access(planDir);
222
+ import_vitest.expect.fail("Directory should not exist");
223
+ } catch {
224
+ }
225
+ });
226
+ });
227
+ });
@@ -1,226 +1,204 @@
1
- /**
2
- * Plan Service Tests
3
- *
4
- * Tests for the PlanService CRUD operations and error handling.
5
- */
6
- import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest';
7
- import * as path from 'node:path';
8
- import * as fs from 'node:fs/promises';
9
- import * as os from 'node:os';
10
- import { PlanService } from './plan-service.js';
11
- import { PlanErrorCode } from './errors.js';
12
- import { DextoRuntimeError } from '@dexto/core';
13
- // Create mock logger
1
+ import { describe, it, expect, beforeEach, afterEach, vi } from "vitest";
2
+ import * as path from "node:path";
3
+ import * as fs from "node:fs/promises";
4
+ import * as os from "node:os";
5
+ import { PlanService } from "./plan-service.js";
6
+ import { PlanErrorCode } from "./errors.js";
7
+ import { DextoRuntimeError } from "@dexto/core";
14
8
  const createMockLogger = () => ({
15
- debug: vi.fn(),
16
- silly: vi.fn(),
17
- info: vi.fn(),
18
- warn: vi.fn(),
19
- error: vi.fn(),
20
- trackException: vi.fn(),
21
- createChild: vi.fn().mockReturnThis(),
22
- setLevel: vi.fn(),
23
- getLevel: vi.fn(() => 'debug'),
24
- getLogFilePath: vi.fn(() => null),
25
- destroy: vi.fn(async () => undefined),
9
+ debug: vi.fn(),
10
+ silly: vi.fn(),
11
+ info: vi.fn(),
12
+ warn: vi.fn(),
13
+ error: vi.fn(),
14
+ trackException: vi.fn(),
15
+ createChild: vi.fn().mockReturnThis(),
16
+ setLevel: vi.fn(),
17
+ getLevel: vi.fn(() => "debug"),
18
+ getLogFilePath: vi.fn(() => null),
19
+ destroy: vi.fn(async () => void 0)
26
20
  });
27
- describe('PlanService', () => {
28
- let mockLogger;
29
- let tempDir;
30
- let planService;
31
- beforeEach(async () => {
32
- mockLogger = createMockLogger();
33
- // Create temp directory for testing
34
- const rawTempDir = await fs.mkdtemp(path.join(os.tmpdir(), 'dexto-plan-test-'));
35
- tempDir = await fs.realpath(rawTempDir);
36
- planService = new PlanService({ basePath: tempDir }, mockLogger);
37
- vi.clearAllMocks();
38
- });
39
- afterEach(async () => {
40
- // Cleanup temp directory
41
- try {
42
- await fs.rm(tempDir, { recursive: true, force: true });
43
- }
44
- catch {
45
- // Ignore cleanup errors
46
- }
47
- });
48
- describe('exists', () => {
49
- it('should return false for non-existent plan', async () => {
50
- const exists = await planService.exists('non-existent-session');
51
- expect(exists).toBe(false);
52
- });
53
- it('should return true for existing plan', async () => {
54
- const sessionId = 'test-session';
55
- await planService.create(sessionId, '# Test Plan');
56
- const exists = await planService.exists(sessionId);
57
- expect(exists).toBe(true);
58
- });
59
- });
60
- describe('create', () => {
61
- it('should create a new plan with content and metadata', async () => {
62
- const sessionId = 'test-session';
63
- const content = '# Implementation Plan\n\n## Steps\n1. First step';
64
- const title = 'Test Plan';
65
- const plan = await planService.create(sessionId, content, { title });
66
- expect(plan.content).toBe(content);
67
- expect(plan.meta.sessionId).toBe(sessionId);
68
- expect(plan.meta.status).toBe('draft');
69
- expect(plan.meta.title).toBe(title);
70
- expect(plan.meta.createdAt).toBeGreaterThan(0);
71
- expect(plan.meta.updatedAt).toBeGreaterThan(0);
72
- });
73
- it('should throw error when plan already exists', async () => {
74
- const sessionId = 'test-session';
75
- await planService.create(sessionId, '# First Plan');
76
- try {
77
- await planService.create(sessionId, '# Second Plan');
78
- expect.fail('Should have thrown an error');
79
- }
80
- catch (error) {
81
- expect(error).toBeInstanceOf(DextoRuntimeError);
82
- expect(error.code).toBe(PlanErrorCode.PLAN_ALREADY_EXISTS);
83
- }
84
- });
85
- it('should store plan files on disk', async () => {
86
- const sessionId = 'test-session';
87
- const content = '# Test Plan';
88
- await planService.create(sessionId, content);
89
- // Verify plan.md exists
90
- const planPath = path.join(tempDir, sessionId, 'plan.md');
91
- const storedContent = await fs.readFile(planPath, 'utf-8');
92
- expect(storedContent).toBe(content);
93
- // Verify plan-meta.json exists
94
- const metaPath = path.join(tempDir, sessionId, 'plan-meta.json');
95
- const metaContent = await fs.readFile(metaPath, 'utf-8');
96
- const meta = JSON.parse(metaContent);
97
- expect(meta.sessionId).toBe(sessionId);
98
- });
99
- });
100
- describe('read', () => {
101
- it('should return null for non-existent plan', async () => {
102
- const plan = await planService.read('non-existent-session');
103
- expect(plan).toBeNull();
104
- });
105
- it('should read existing plan with content and metadata', async () => {
106
- const sessionId = 'test-session';
107
- const content = '# Test Plan';
108
- const title = 'My Plan';
109
- await planService.create(sessionId, content, { title });
110
- const plan = await planService.read(sessionId);
111
- expect(plan).not.toBeNull();
112
- expect(plan.content).toBe(content);
113
- expect(plan.meta.sessionId).toBe(sessionId);
114
- expect(plan.meta.title).toBe(title);
115
- });
116
- it('should handle invalid metadata schema gracefully', async () => {
117
- const sessionId = 'test-session';
118
- await planService.create(sessionId, '# Test');
119
- // Write valid JSON but invalid schema (missing required fields)
120
- const metaPath = path.join(tempDir, sessionId, 'plan-meta.json');
121
- await fs.writeFile(metaPath, JSON.stringify({ invalidField: 'value' }));
122
- const plan = await planService.read(sessionId);
123
- // Should return with default metadata
124
- expect(plan).not.toBeNull();
125
- expect(plan.meta.sessionId).toBe(sessionId);
126
- expect(plan.meta.status).toBe('draft');
127
- expect(mockLogger.warn).toHaveBeenCalled();
128
- });
129
- it('should return null for corrupted JSON metadata', async () => {
130
- const sessionId = 'test-session';
131
- await planService.create(sessionId, '# Test');
132
- // Corrupt the metadata with invalid JSON
133
- const metaPath = path.join(tempDir, sessionId, 'plan-meta.json');
134
- await fs.writeFile(metaPath, '{ invalid json }');
135
- const plan = await planService.read(sessionId);
136
- // Should return null and log error
137
- expect(plan).toBeNull();
138
- expect(mockLogger.error).toHaveBeenCalled();
139
- });
140
- });
141
- describe('update', () => {
142
- it('should update plan content', async () => {
143
- const sessionId = 'test-session';
144
- await planService.create(sessionId, '# Original Content');
145
- const result = await planService.update(sessionId, '# Updated Content');
146
- expect(result.oldContent).toBe('# Original Content');
147
- expect(result.newContent).toBe('# Updated Content');
148
- expect(result.meta.updatedAt).toBeGreaterThan(0);
149
- });
150
- it('should preserve metadata when updating content', async () => {
151
- const sessionId = 'test-session';
152
- const plan = await planService.create(sessionId, '# Original', { title: 'My Title' });
153
- const originalCreatedAt = plan.meta.createdAt;
154
- await planService.update(sessionId, '# Updated');
155
- const updatedPlan = await planService.read(sessionId);
156
- expect(updatedPlan.meta.title).toBe('My Title');
157
- expect(updatedPlan.meta.createdAt).toBe(originalCreatedAt);
158
- });
159
- it('should throw error when plan does not exist', async () => {
160
- try {
161
- await planService.update('non-existent', '# Content');
162
- expect.fail('Should have thrown an error');
163
- }
164
- catch (error) {
165
- expect(error).toBeInstanceOf(DextoRuntimeError);
166
- expect(error.code).toBe(PlanErrorCode.PLAN_NOT_FOUND);
167
- }
168
- });
169
- });
170
- describe('updateMeta', () => {
171
- it('should update plan status', async () => {
172
- const sessionId = 'test-session';
173
- await planService.create(sessionId, '# Plan');
174
- const meta = await planService.updateMeta(sessionId, { status: 'approved' });
175
- expect(meta.status).toBe('approved');
176
- });
177
- it('should update plan title', async () => {
178
- const sessionId = 'test-session';
179
- await planService.create(sessionId, '# Plan');
180
- const meta = await planService.updateMeta(sessionId, { title: 'New Title' });
181
- expect(meta.title).toBe('New Title');
182
- });
183
- it('should throw error when plan does not exist', async () => {
184
- try {
185
- await planService.updateMeta('non-existent', { status: 'approved' });
186
- expect.fail('Should have thrown an error');
187
- }
188
- catch (error) {
189
- expect(error).toBeInstanceOf(DextoRuntimeError);
190
- expect(error.code).toBe(PlanErrorCode.PLAN_NOT_FOUND);
191
- }
192
- });
193
- });
194
- describe('delete', () => {
195
- it('should delete existing plan', async () => {
196
- const sessionId = 'test-session';
197
- await planService.create(sessionId, '# Plan');
198
- await planService.delete(sessionId);
199
- const exists = await planService.exists(sessionId);
200
- expect(exists).toBe(false);
201
- });
202
- it('should throw error when plan does not exist', async () => {
203
- try {
204
- await planService.delete('non-existent');
205
- expect.fail('Should have thrown an error');
206
- }
207
- catch (error) {
208
- expect(error).toBeInstanceOf(DextoRuntimeError);
209
- expect(error.code).toBe(PlanErrorCode.PLAN_NOT_FOUND);
210
- }
211
- });
212
- it('should remove plan directory from disk', async () => {
213
- const sessionId = 'test-session';
214
- await planService.create(sessionId, '# Plan');
215
- const planDir = path.join(tempDir, sessionId);
216
- await planService.delete(sessionId);
217
- try {
218
- await fs.access(planDir);
219
- expect.fail('Directory should not exist');
220
- }
221
- catch {
222
- // Expected - directory should not exist
223
- }
224
- });
21
+ describe("PlanService", () => {
22
+ let mockLogger;
23
+ let tempDir;
24
+ let planService;
25
+ beforeEach(async () => {
26
+ mockLogger = createMockLogger();
27
+ const rawTempDir = await fs.mkdtemp(path.join(os.tmpdir(), "dexto-plan-test-"));
28
+ tempDir = await fs.realpath(rawTempDir);
29
+ planService = new PlanService({ basePath: tempDir }, mockLogger);
30
+ vi.clearAllMocks();
31
+ });
32
+ afterEach(async () => {
33
+ try {
34
+ await fs.rm(tempDir, { recursive: true, force: true });
35
+ } catch {
36
+ }
37
+ });
38
+ describe("exists", () => {
39
+ it("should return false for non-existent plan", async () => {
40
+ const exists = await planService.exists("non-existent-session");
41
+ expect(exists).toBe(false);
225
42
  });
43
+ it("should return true for existing plan", async () => {
44
+ const sessionId = "test-session";
45
+ await planService.create(sessionId, "# Test Plan");
46
+ const exists = await planService.exists(sessionId);
47
+ expect(exists).toBe(true);
48
+ });
49
+ });
50
+ describe("create", () => {
51
+ it("should create a new plan with content and metadata", async () => {
52
+ const sessionId = "test-session";
53
+ const content = "# Implementation Plan\n\n## Steps\n1. First step";
54
+ const title = "Test Plan";
55
+ const plan = await planService.create(sessionId, content, { title });
56
+ expect(plan.content).toBe(content);
57
+ expect(plan.meta.sessionId).toBe(sessionId);
58
+ expect(plan.meta.status).toBe("draft");
59
+ expect(plan.meta.title).toBe(title);
60
+ expect(plan.meta.createdAt).toBeGreaterThan(0);
61
+ expect(plan.meta.updatedAt).toBeGreaterThan(0);
62
+ });
63
+ it("should throw error when plan already exists", async () => {
64
+ const sessionId = "test-session";
65
+ await planService.create(sessionId, "# First Plan");
66
+ try {
67
+ await planService.create(sessionId, "# Second Plan");
68
+ expect.fail("Should have thrown an error");
69
+ } catch (error) {
70
+ expect(error).toBeInstanceOf(DextoRuntimeError);
71
+ expect(error.code).toBe(PlanErrorCode.PLAN_ALREADY_EXISTS);
72
+ }
73
+ });
74
+ it("should store plan files on disk", async () => {
75
+ const sessionId = "test-session";
76
+ const content = "# Test Plan";
77
+ await planService.create(sessionId, content);
78
+ const planPath = path.join(tempDir, sessionId, "plan.md");
79
+ const storedContent = await fs.readFile(planPath, "utf-8");
80
+ expect(storedContent).toBe(content);
81
+ const metaPath = path.join(tempDir, sessionId, "plan-meta.json");
82
+ const metaContent = await fs.readFile(metaPath, "utf-8");
83
+ const meta = JSON.parse(metaContent);
84
+ expect(meta.sessionId).toBe(sessionId);
85
+ });
86
+ });
87
+ describe("read", () => {
88
+ it("should return null for non-existent plan", async () => {
89
+ const plan = await planService.read("non-existent-session");
90
+ expect(plan).toBeNull();
91
+ });
92
+ it("should read existing plan with content and metadata", async () => {
93
+ const sessionId = "test-session";
94
+ const content = "# Test Plan";
95
+ const title = "My Plan";
96
+ await planService.create(sessionId, content, { title });
97
+ const plan = await planService.read(sessionId);
98
+ expect(plan).not.toBeNull();
99
+ expect(plan.content).toBe(content);
100
+ expect(plan.meta.sessionId).toBe(sessionId);
101
+ expect(plan.meta.title).toBe(title);
102
+ });
103
+ it("should handle invalid metadata schema gracefully", async () => {
104
+ const sessionId = "test-session";
105
+ await planService.create(sessionId, "# Test");
106
+ const metaPath = path.join(tempDir, sessionId, "plan-meta.json");
107
+ await fs.writeFile(metaPath, JSON.stringify({ invalidField: "value" }));
108
+ const plan = await planService.read(sessionId);
109
+ expect(plan).not.toBeNull();
110
+ expect(plan.meta.sessionId).toBe(sessionId);
111
+ expect(plan.meta.status).toBe("draft");
112
+ expect(mockLogger.warn).toHaveBeenCalled();
113
+ });
114
+ it("should return null for corrupted JSON metadata", async () => {
115
+ const sessionId = "test-session";
116
+ await planService.create(sessionId, "# Test");
117
+ const metaPath = path.join(tempDir, sessionId, "plan-meta.json");
118
+ await fs.writeFile(metaPath, "{ invalid json }");
119
+ const plan = await planService.read(sessionId);
120
+ expect(plan).toBeNull();
121
+ expect(mockLogger.error).toHaveBeenCalled();
122
+ });
123
+ });
124
+ describe("update", () => {
125
+ it("should update plan content", async () => {
126
+ const sessionId = "test-session";
127
+ await planService.create(sessionId, "# Original Content");
128
+ const result = await planService.update(sessionId, "# Updated Content");
129
+ expect(result.oldContent).toBe("# Original Content");
130
+ expect(result.newContent).toBe("# Updated Content");
131
+ expect(result.meta.updatedAt).toBeGreaterThan(0);
132
+ });
133
+ it("should preserve metadata when updating content", async () => {
134
+ const sessionId = "test-session";
135
+ const plan = await planService.create(sessionId, "# Original", { title: "My Title" });
136
+ const originalCreatedAt = plan.meta.createdAt;
137
+ await planService.update(sessionId, "# Updated");
138
+ const updatedPlan = await planService.read(sessionId);
139
+ expect(updatedPlan.meta.title).toBe("My Title");
140
+ expect(updatedPlan.meta.createdAt).toBe(originalCreatedAt);
141
+ });
142
+ it("should throw error when plan does not exist", async () => {
143
+ try {
144
+ await planService.update("non-existent", "# Content");
145
+ expect.fail("Should have thrown an error");
146
+ } catch (error) {
147
+ expect(error).toBeInstanceOf(DextoRuntimeError);
148
+ expect(error.code).toBe(PlanErrorCode.PLAN_NOT_FOUND);
149
+ }
150
+ });
151
+ });
152
+ describe("updateMeta", () => {
153
+ it("should update plan status", async () => {
154
+ const sessionId = "test-session";
155
+ await planService.create(sessionId, "# Plan");
156
+ const meta = await planService.updateMeta(sessionId, { status: "approved" });
157
+ expect(meta.status).toBe("approved");
158
+ });
159
+ it("should update plan title", async () => {
160
+ const sessionId = "test-session";
161
+ await planService.create(sessionId, "# Plan");
162
+ const meta = await planService.updateMeta(sessionId, { title: "New Title" });
163
+ expect(meta.title).toBe("New Title");
164
+ });
165
+ it("should throw error when plan does not exist", async () => {
166
+ try {
167
+ await planService.updateMeta("non-existent", { status: "approved" });
168
+ expect.fail("Should have thrown an error");
169
+ } catch (error) {
170
+ expect(error).toBeInstanceOf(DextoRuntimeError);
171
+ expect(error.code).toBe(PlanErrorCode.PLAN_NOT_FOUND);
172
+ }
173
+ });
174
+ });
175
+ describe("delete", () => {
176
+ it("should delete existing plan", async () => {
177
+ const sessionId = "test-session";
178
+ await planService.create(sessionId, "# Plan");
179
+ await planService.delete(sessionId);
180
+ const exists = await planService.exists(sessionId);
181
+ expect(exists).toBe(false);
182
+ });
183
+ it("should throw error when plan does not exist", async () => {
184
+ try {
185
+ await planService.delete("non-existent");
186
+ expect.fail("Should have thrown an error");
187
+ } catch (error) {
188
+ expect(error).toBeInstanceOf(DextoRuntimeError);
189
+ expect(error.code).toBe(PlanErrorCode.PLAN_NOT_FOUND);
190
+ }
191
+ });
192
+ it("should remove plan directory from disk", async () => {
193
+ const sessionId = "test-session";
194
+ await planService.create(sessionId, "# Plan");
195
+ const planDir = path.join(tempDir, sessionId);
196
+ await planService.delete(sessionId);
197
+ try {
198
+ await fs.access(planDir);
199
+ expect.fail("Directory should not exist");
200
+ } catch {
201
+ }
202
+ });
203
+ });
226
204
  });