@nexiq/cli 1.0.1

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.
@@ -0,0 +1,897 @@
1
+ import { describe, it, expect, vi, beforeEach } from "vitest";
2
+ import {
3
+ ProjectManager,
4
+ type ComponentHierarchyNode,
5
+ } from "./projectManager.js";
6
+ import fs from "node:fs";
7
+ import path from "node:path";
8
+ import * as watcher from "@parcel/watcher";
9
+ import { analyzeProject } from "@nexiq/analyser";
10
+ import "@nexiq/extension-sdk";
11
+ import type { JsonData } from "@nexiq/shared";
12
+
13
+ import Database from "better-sqlite3";
14
+
15
+ vi.mock("node:fs");
16
+ vi.mock("@parcel/watcher");
17
+
18
+ const createMockStmt = () => ({
19
+ all: vi.fn(() => []),
20
+ get: vi.fn(() => undefined),
21
+ run: vi.fn(() => ({ changes: 0 })),
22
+ });
23
+
24
+ const mockDb = {
25
+ prepare: vi.fn(() => createMockStmt() as unknown as Database.Statement),
26
+ close: vi.fn(),
27
+ pragma: vi.fn(),
28
+ exec: vi.fn(),
29
+ };
30
+
31
+ vi.mock("@nexiq/analyser", () => ({
32
+ analyzeProject: vi.fn(),
33
+ }));
34
+
35
+ vi.mock("@nexiq/analyser/db/sqlite", () => ({
36
+ SqliteDB: vi.fn().mockImplementation(() => ({
37
+ db: mockDb,
38
+ getAllData: vi.fn().mockReturnValue({
39
+ files: [],
40
+ entities: [],
41
+ scopes: [],
42
+ symbols: [],
43
+ renders: [],
44
+ exports: [],
45
+ relations: [],
46
+ }),
47
+ close: () => mockDb.close(),
48
+ })),
49
+ }));
50
+
51
+ vi.mock("better-sqlite3", () => {
52
+ return {
53
+ default: vi.fn(() => mockDb),
54
+ };
55
+ });
56
+
57
+ describe("ProjectManager", () => {
58
+ let projectManager: ProjectManager;
59
+
60
+ beforeEach(() => {
61
+ vi.clearAllMocks();
62
+ mockDb.prepare.mockReset();
63
+ mockDb.prepare.mockImplementation(
64
+ () => createMockStmt() as unknown as Database.Statement,
65
+ );
66
+ vi.spyOn(console, "error").mockImplementation(() => {});
67
+ projectManager = new ProjectManager();
68
+ vi.mocked(fs.existsSync).mockReturnValue(false);
69
+ vi.mocked(analyzeProject).mockResolvedValue({
70
+ src: "/test/project",
71
+ files: {},
72
+ edges: [],
73
+ } as unknown as JsonData);
74
+ });
75
+
76
+ it("should open a project and return project info", async () => {
77
+ const projectPath = "/test/project";
78
+ const info = await projectManager.openProject(projectPath);
79
+
80
+ expect(info.projectPath).toBe(projectPath);
81
+ expect(fs.mkdirSync).toHaveBeenCalledWith(
82
+ expect.stringContaining(".react-map/cache"),
83
+ { recursive: true },
84
+ );
85
+ expect(analyzeProject).toHaveBeenCalledWith(
86
+ projectPath,
87
+ expect.any(String),
88
+ undefined,
89
+ expect.any(String),
90
+ );
91
+ expect(watcher.subscribe).toHaveBeenCalled();
92
+ });
93
+
94
+ it("should handle subprojects correctly", async () => {
95
+ const projectPath = "/test/project";
96
+ const subProject = "packages/app";
97
+ const info = await projectManager.openProject(projectPath, subProject);
98
+
99
+ expect(info.subProject).toBe(subProject);
100
+ const expectedAnalysisPath = path.resolve(projectPath, subProject);
101
+ expect(analyzeProject).toHaveBeenCalledWith(
102
+ expectedAnalysisPath,
103
+ expect.any(String),
104
+ undefined,
105
+ expect.any(String),
106
+ );
107
+ });
108
+
109
+ it("should return cached project if already open", async () => {
110
+ const projectPath = "/test/project";
111
+ const info1 = await projectManager.openProject(projectPath);
112
+ const info2 = await projectManager.openProject(projectPath);
113
+
114
+ expect(info1).toBe(info2);
115
+ expect(analyzeProject).toHaveBeenCalledTimes(1);
116
+ });
117
+
118
+ it("should load config and extensions if present", async () => {
119
+ const projectPath = "/test/project";
120
+ const configPath = path.join(projectPath, "react.map.config.json");
121
+
122
+ vi.mocked(fs.existsSync).mockImplementation((p: string | fs.PathLike) => {
123
+ if (p === configPath) return true;
124
+ // Do not return true for extension file path so we use module import
125
+ return false;
126
+ });
127
+ vi.mocked(fs.readFileSync).mockReturnValue(
128
+ JSON.stringify({
129
+ ignorePatterns: ["*.test.ts"],
130
+ extensions: ["@nexiq/test-extension"],
131
+ }),
132
+ );
133
+
134
+ // Mock dynamic import
135
+ vi.mock("@nexiq/test-extension", () => ({
136
+ default: { id: "test-ext" },
137
+ }));
138
+ vi.mock("@nexiq/fallback", () => ({
139
+ default: { id: "fallback-ext" },
140
+ }));
141
+
142
+ const consoleSpy = vi.spyOn(console, "error").mockImplementation(() => {});
143
+ await projectManager.openProject(projectPath);
144
+
145
+ expect(analyzeProject).toHaveBeenCalledWith(
146
+ projectPath,
147
+ expect.any(String),
148
+ ["*.test.ts"],
149
+ expect.any(String),
150
+ );
151
+ expect(consoleSpy).toHaveBeenCalledWith(
152
+ expect.stringContaining("Loaded extension: test-ext"),
153
+ );
154
+ consoleSpy.mockRestore();
155
+ });
156
+
157
+ it("should handle extension load failures", async () => {
158
+ const projectPath = "/test/project";
159
+ const configPath = path.join(projectPath, "react.map.config.json");
160
+
161
+ vi.mocked(fs.existsSync).mockImplementation(
162
+ (p: string | fs.PathLike) => p === configPath,
163
+ );
164
+ vi.mocked(fs.readFileSync).mockReturnValue(
165
+ JSON.stringify({
166
+ extensions: ["@nexiq/fail"],
167
+ }),
168
+ );
169
+
170
+ const consoleSpy = vi.spyOn(console, "error").mockImplementation(() => {});
171
+ await projectManager.openProject(projectPath);
172
+ expect(consoleSpy).toHaveBeenCalledWith(
173
+ expect.stringContaining("Failed to load extension"),
174
+ expect.any(String),
175
+ );
176
+ });
177
+
178
+ it("should handle extension fallback import", async () => {
179
+ const projectPath = "/test/project";
180
+ const configPath = path.join(projectPath, "react.map.config.json");
181
+
182
+ vi.mocked(fs.existsSync).mockImplementation(
183
+ (p: string | fs.PathLike) => p === configPath,
184
+ );
185
+ vi.mocked(fs.readFileSync).mockReturnValue(
186
+ JSON.stringify({
187
+ extensions: ["@nexiq/fallback"],
188
+ }),
189
+ );
190
+
191
+ await projectManager.openProject(projectPath);
192
+ // Should hit line 94
193
+ });
194
+
195
+ it("should close all projects and unsubscribe from watchers", async () => {
196
+ const mockSubscription = { unsubscribe: vi.fn() };
197
+ vi.mocked(watcher.subscribe).mockResolvedValue(
198
+ mockSubscription as unknown as watcher.AsyncSubscription,
199
+ );
200
+
201
+ await projectManager.openProject("/p1");
202
+ await projectManager.openProject("/p2");
203
+
204
+ await projectManager.closeAll();
205
+
206
+ expect(mockSubscription.unsubscribe).toHaveBeenCalledTimes(2);
207
+ expect(mockDb.close).toHaveBeenCalledTimes(2);
208
+ expect(projectManager.getProject("/p1")).toBeUndefined();
209
+ });
210
+
211
+ it("should re-analyze when watcher detects changes", async () => {
212
+ let watcherCallback: (err: Error | null, events: watcher.Event[]) => void;
213
+ vi.mocked(watcher.subscribe).mockImplementation(
214
+ (
215
+ _path: string,
216
+ cb: (err: Error | null, events: watcher.Event[]) => void,
217
+ ) => {
218
+ watcherCallback = cb;
219
+ return Promise.resolve({
220
+ unsubscribe: vi.fn(),
221
+ } as unknown as watcher.AsyncSubscription);
222
+ },
223
+ );
224
+
225
+ await projectManager.openProject("/test/project");
226
+
227
+ // Trigger change
228
+ const events = [
229
+ { path: "/test/project/src/NewComp.tsx", type: "update" as const },
230
+ ];
231
+ await watcherCallback!(null, events);
232
+
233
+ expect(analyzeProject).toHaveBeenCalledTimes(2);
234
+ expect(mockDb.close).toHaveBeenCalled();
235
+ });
236
+
237
+ it("should handle watcher errors", async () => {
238
+ let watcherCallback: (err: Error | null, events: watcher.Event[]) => void;
239
+ vi.mocked(watcher.subscribe).mockImplementation(
240
+ (
241
+ _path: string,
242
+ cb: (err: Error | null, events: watcher.Event[]) => void,
243
+ ) => {
244
+ watcherCallback = cb;
245
+ return Promise.resolve({
246
+ unsubscribe: vi.fn(),
247
+ } as unknown as watcher.AsyncSubscription);
248
+ },
249
+ );
250
+
251
+ const consoleSpy = vi.spyOn(console, "error").mockImplementation(() => {});
252
+ await projectManager.openProject("/test/project");
253
+
254
+ await watcherCallback!(new Error("Watcher failed"), []);
255
+ expect(consoleSpy).toHaveBeenCalledWith(
256
+ expect.stringContaining("Watcher error"),
257
+ expect.any(Error),
258
+ );
259
+ });
260
+
261
+ describe("Labeling", () => {
262
+ const projectPath = "/test/project";
263
+
264
+ beforeEach(async () => {
265
+ vi.mocked(analyzeProject).mockResolvedValue({
266
+ src: projectPath,
267
+ files: {},
268
+ edges: [],
269
+ labels: {},
270
+ } as unknown as JsonData);
271
+ await projectManager.openProject(projectPath);
272
+ });
273
+
274
+ it("should add and persist labels", async () => {
275
+ const labels = await projectManager.addLabel(
276
+ projectPath,
277
+ "id1",
278
+ "important",
279
+ );
280
+ expect(labels).toEqual(["important"]);
281
+ expect(fs.writeFileSync).toHaveBeenCalled();
282
+
283
+ const allLabels = await projectManager.getLabels(projectPath);
284
+ expect(allLabels["id1"]).toEqual(["important"]);
285
+ });
286
+
287
+ it("should remove labels", async () => {
288
+ await projectManager.addLabel(projectPath, "id1", "tag1");
289
+ await projectManager.addLabel(projectPath, "id1", "tag2");
290
+
291
+ const labels = await projectManager.removeLabel(
292
+ projectPath,
293
+ "id1",
294
+ "tag1",
295
+ );
296
+ expect(labels).toEqual(["tag2"]);
297
+
298
+ const found = await projectManager.findEntitiesByLabel(
299
+ projectPath,
300
+ "tag2",
301
+ );
302
+ expect(found).toContain("id1");
303
+ });
304
+
305
+ it("should find entities by label", async () => {
306
+ await projectManager.addLabel(projectPath, "id1", "@nexiq/shared");
307
+ await projectManager.addLabel(projectPath, "id2", "@nexiq/shared");
308
+
309
+ const found = await projectManager.findEntitiesByLabel(
310
+ projectPath,
311
+ "@nexiq/shared",
312
+ );
313
+ expect(found).toEqual(["id1", "id2"]);
314
+ });
315
+ });
316
+
317
+ describe("Enhanced Navigation", () => {
318
+ const projectPath = "/test/project";
319
+
320
+ beforeEach(async () => {
321
+ const mockGraph = {
322
+ src: projectPath,
323
+ files: {
324
+ "/src/components/Button.tsx": {
325
+ var: {
326
+ "btn-id": {
327
+ id: "btn-id",
328
+ name: { type: "identifier" as const, name: "Button" },
329
+ kind: "component" as const,
330
+ type: "function" as const,
331
+ loc: { line: 10, column: 1 },
332
+ children: { r1: { tag: "div", loc: { line: 15, column: 5 } } },
333
+ var: {
334
+ s1: {
335
+ id: "s1",
336
+ name: { type: "identifier" as const, name: "count" },
337
+ kind: "state" as const,
338
+ type: "data" as const,
339
+ loc: { line: 11, column: 5 },
340
+ },
341
+ },
342
+ },
343
+ },
344
+ },
345
+ "/src/utils/math.ts": { var: {} },
346
+ },
347
+ edges: [],
348
+ };
349
+ vi.mocked(analyzeProject).mockResolvedValue(
350
+ mockGraph as unknown as JsonData,
351
+ );
352
+ await projectManager.openProject(projectPath);
353
+ });
354
+
355
+ it("should list directories and files", async () => {
356
+ vi.mocked(analyzeProject).mockResolvedValue({
357
+ src: projectPath,
358
+ files: {
359
+ "/src/components/Button.tsx": {
360
+ var: {
361
+ "btn-id": {
362
+ id: "btn-id",
363
+ name: { type: "identifier" as const, name: "Button" },
364
+ kind: "component" as const,
365
+ type: "function" as const,
366
+ loc: { line: 10, column: 1 },
367
+ children: { r1: { tag: "div", loc: { line: 15, column: 5 } } },
368
+ var: {
369
+ s1: {
370
+ id: "s1",
371
+ name: { type: "identifier" as const, name: "count" },
372
+ kind: "state" as const,
373
+ type: "data" as const,
374
+ loc: { line: 11, column: 5 },
375
+ },
376
+ },
377
+ },
378
+ },
379
+ },
380
+ "/src/utils/math.ts": { var: {} },
381
+ },
382
+ edges: [],
383
+ } as unknown as JsonData);
384
+ await projectManager.openProject(projectPath);
385
+
386
+ const result = await projectManager.listDirectory(projectPath, "src");
387
+ expect(result.directories).toEqual(["components", "utils"]);
388
+ expect(result.files).toEqual([]);
389
+
390
+ const components = await projectManager.listDirectory(
391
+ projectPath,
392
+ "src/components",
393
+ );
394
+ expect(components.files).toEqual(["Button.tsx"]);
395
+ });
396
+
397
+ it("should get file outline", async () => {
398
+ vi.mocked(analyzeProject).mockResolvedValue({
399
+ src: projectPath,
400
+ files: {
401
+ "/src/components/Button.tsx": {
402
+ var: {
403
+ "btn-id": {
404
+ id: "btn-id",
405
+ name: { type: "identifier" as const, name: "Button" },
406
+ kind: "component" as const,
407
+ type: "function" as const,
408
+ loc: { line: 10, column: 1 },
409
+ children: { r1: { tag: "div", loc: { line: 15, column: 5 } } },
410
+ var: {
411
+ s1: {
412
+ id: "s1",
413
+ name: { type: "identifier" as const, name: "count" },
414
+ kind: "state" as const,
415
+ type: "data" as const,
416
+ loc: { line: 11, column: 5 },
417
+ },
418
+ },
419
+ },
420
+ },
421
+ },
422
+ "/src/utils/math.ts": { var: {} },
423
+ },
424
+ edges: [],
425
+ } as unknown as JsonData);
426
+ await projectManager.openProject(projectPath);
427
+ const outline = await projectManager.getFileOutline(
428
+ projectPath,
429
+ "src/components/Button.tsx",
430
+ );
431
+ expect(outline).toHaveLength(1);
432
+ expect(outline[0].name).toBe("Button");
433
+ });
434
+
435
+ it("should get component hierarchy", async () => {
436
+ vi.mocked(analyzeProject).mockResolvedValue({
437
+ src: projectPath,
438
+ files: {
439
+ "/src/components/Button.tsx": {
440
+ var: {
441
+ "btn-id": {
442
+ id: "btn-id",
443
+ name: { type: "identifier" as const, name: "Button" },
444
+ kind: "component" as const,
445
+ type: "function" as const,
446
+ loc: { line: 10, column: 1 },
447
+ children: { r1: { tag: "div", loc: { line: 15, column: 5 } } },
448
+ var: {
449
+ s1: {
450
+ id: "s1",
451
+ name: { type: "identifier" as const, name: "count" },
452
+ kind: "state" as const,
453
+ type: "data" as const,
454
+ loc: { line: 11, column: 5 },
455
+ },
456
+ },
457
+ },
458
+ },
459
+ },
460
+ "/src/utils/math.ts": { var: {} },
461
+ },
462
+ edges: [],
463
+ } as unknown as JsonData);
464
+ await projectManager.openProject(projectPath);
465
+ mockDb.prepare
466
+ .mockReturnValueOnce({
467
+ all: vi.fn().mockReturnValue([{ id: "btn-id", name: "Button" }]),
468
+ } as unknown as Database.Statement)
469
+ .mockReturnValueOnce({
470
+ all: vi.fn().mockReturnValue([]), // renderedBy call
471
+ } as unknown as Database.Statement)
472
+ .mockReturnValueOnce({
473
+ get: vi.fn().mockReturnValue({ id: "btn-id", name: "Button" }), // sym call
474
+ } as unknown as Database.Statement)
475
+ .mockReturnValueOnce({
476
+ all: vi.fn().mockReturnValue([{ tag: "div", symbol_id: null }]), // children call
477
+ } as unknown as Database.Statement)
478
+ .mockReturnValueOnce({
479
+ all: vi.fn().mockReturnValue([]),
480
+ } as unknown as Database.Statement);
481
+
482
+ const result = (await projectManager.getComponentHierarchy(
483
+ projectPath,
484
+ "Button",
485
+ )) as { component: string; hierarchies: ComponentHierarchyNode[] };
486
+ expect(result.component).toBe("Button");
487
+ expect(result.hierarchies[0].name).toBe("Button");
488
+ expect(result.hierarchies[0].children[0].name).toBe("div");
489
+ });
490
+
491
+ it("should return error if component not found for hierarchy", async () => {
492
+ mockDb.prepare.mockImplementationOnce(
493
+ () =>
494
+ ({
495
+ all: vi.fn().mockReturnValue([]),
496
+ }) as unknown as Database.Statement,
497
+ );
498
+ const result = await projectManager.getComponentHierarchy(
499
+ projectPath,
500
+ "NonExistent",
501
+ );
502
+ expect((result as { error: string }).error).toBeDefined();
503
+ });
504
+
505
+ it("should throw error if file not found for outline", async () => {
506
+ await expect(
507
+ projectManager.getFileOutline(projectPath, "non-existent.tsx"),
508
+ ).rejects.toThrow("File not found");
509
+ });
510
+
511
+ it("should open project automatically for listDirectory", async () => {
512
+ const result = await projectManager.listDirectory("/new-project", "src");
513
+ expect(result).toBeDefined();
514
+ expect(analyzeProject).toHaveBeenCalledWith(
515
+ "/new-project",
516
+ expect.any(String),
517
+ undefined,
518
+ expect.any(String),
519
+ );
520
+ });
521
+ });
522
+
523
+ describe("Efficiency Tools", () => {
524
+ const projectPath = "/test/project";
525
+
526
+ beforeEach(async () => {
527
+ const mockGraph = {
528
+ src: projectPath,
529
+ files: {
530
+ "/src/App.tsx": {
531
+ import: {
532
+ useState: {
533
+ localName: "useState",
534
+ importedName: "useState",
535
+ source: "react",
536
+ type: "named",
537
+ importKind: "value",
538
+ },
539
+ Button: {
540
+ localName: "Button",
541
+ importedName: "Button",
542
+ source: "/src/components/Button.tsx",
543
+ type: "named",
544
+ importKind: "value",
545
+ },
546
+ },
547
+ var: {
548
+ "app-id": {
549
+ id: "app-id",
550
+ name: { type: "identifier" as const, name: "App" },
551
+ kind: "component" as const,
552
+ type: "function" as const,
553
+ loc: { line: 10, column: 1 },
554
+ hooks: ["useState"],
555
+ children: {
556
+ r1: { tag: "Button", loc: { line: 15, column: 5 } },
557
+ },
558
+ },
559
+ },
560
+ },
561
+ "/src/components/Button.tsx": {
562
+ import: {},
563
+ var: {
564
+ "btn-id": {
565
+ id: "btn-id",
566
+ name: { type: "identifier" as const, name: "Button" },
567
+ kind: "component" as const,
568
+ type: "function" as const,
569
+ loc: { line: 5, column: 1 },
570
+ },
571
+ },
572
+ },
573
+ },
574
+ edges: [],
575
+ };
576
+ vi.mocked(analyzeProject).mockResolvedValue(
577
+ mockGraph as unknown as JsonData,
578
+ );
579
+ await projectManager.openProject(projectPath);
580
+ });
581
+
582
+ it("should find files by pattern", async () => {
583
+ const results = await projectManager.findFiles(projectPath, "App*");
584
+ expect(results).toEqual(["/src/App.tsx"]);
585
+
586
+ const results2 = await projectManager.findFiles(projectPath, "Button");
587
+ expect(results2).toEqual(["/src/components/Button.tsx"]);
588
+ });
589
+
590
+ it("should get file imports", async () => {
591
+ const imports = await projectManager.getFileImports(
592
+ projectPath,
593
+ "/src/App.tsx",
594
+ );
595
+ expect(imports["useState"]).toBeDefined();
596
+ expect(imports["Button"]).toBeDefined();
597
+ });
598
+
599
+ it("should get project tree", async () => {
600
+ const tree = await projectManager.getProjectTree(
601
+ projectPath,
602
+ undefined,
603
+ 2,
604
+ );
605
+ expect(tree.name).toBe("/");
606
+ expect(tree.children[0].name).toBe("src");
607
+ expect(tree.children[0].children[0].name).toBe("App.tsx");
608
+ expect(tree.children[0].children[1].name).toBe("components");
609
+ });
610
+ });
611
+
612
+ describe("Symbol Location and Content", () => {
613
+ const projectPath = "/test/project";
614
+
615
+ beforeEach(async () => {
616
+ const mockGraph = {
617
+ src: projectPath,
618
+ files: {
619
+ "/src/App.tsx": {
620
+ var: {
621
+ "app-id": {
622
+ id: "app-id",
623
+ name: { type: "identifier" as const, name: "App" },
624
+ kind: "component" as const,
625
+ type: "function" as const,
626
+ loc: { line: 5, column: 1 },
627
+ scope: {
628
+ start: { line: 5, column: 20 },
629
+ end: { line: 10, column: 1 },
630
+ },
631
+ },
632
+ },
633
+ },
634
+ },
635
+ edges: [],
636
+ };
637
+ vi.mocked(analyzeProject).mockResolvedValue(
638
+ mockGraph as unknown as JsonData,
639
+ );
640
+ await projectManager.openProject(projectPath);
641
+ });
642
+
643
+ it("should get symbol location", async () => {
644
+ mockDb.prepare.mockReturnValueOnce({
645
+ all: vi.fn().mockReturnValue([
646
+ {
647
+ id: "app-id",
648
+ name: "App",
649
+ file: "/src/App.tsx",
650
+ line: 1,
651
+ column: 1,
652
+ kind: "component",
653
+ type: "function",
654
+ },
655
+ ]),
656
+ } as unknown as Database.Statement);
657
+ const loc = await projectManager.getSymbolLocation(projectPath, "App");
658
+ expect(loc).toHaveLength(1);
659
+ expect(loc[0].file).toBe("/src/App.tsx");
660
+ expect(loc[0].loc.line).toBe(1);
661
+ });
662
+
663
+ it("should get symbol content", async () => {
664
+ mockDb.prepare.mockReturnValueOnce({
665
+ all: vi.fn().mockReturnValue([
666
+ {
667
+ id: "app-id",
668
+ name: "App",
669
+ file: "/src/App.tsx",
670
+ line: 1,
671
+ column: 1,
672
+ kind: "component",
673
+ type: "function",
674
+ },
675
+ ]),
676
+ } as unknown as Database.Statement);
677
+ const fileContent =
678
+ "export const App = () => {\n return <div>App</div>\n}";
679
+ vi.mocked(fs.existsSync).mockImplementation((p: string | fs.PathLike) => {
680
+ const s = p as string;
681
+ if (s.includes(".react-map/cache")) return true;
682
+ if (s.includes("/src/App.tsx")) return true;
683
+ return false;
684
+ });
685
+ vi.mocked(fs.readFileSync).mockReturnValue(fileContent);
686
+
687
+ const content = (await projectManager.getSymbolContent(
688
+ projectPath,
689
+ "App",
690
+ )) as { content: string }[];
691
+ expect(content[0].content).toContain("App");
692
+ });
693
+
694
+ it("should get symbol content from subproject", async () => {
695
+ const subProject = "packages/app";
696
+ mockDb.prepare.mockReturnValueOnce({
697
+ all: vi.fn().mockReturnValue([
698
+ {
699
+ id: "app-id",
700
+ name: "App",
701
+ file: "/src/App.tsx",
702
+ line: 1,
703
+ column: 1,
704
+ kind: "component",
705
+ type: "function",
706
+ },
707
+ ]),
708
+ } as unknown as Database.Statement);
709
+ await projectManager.openProject(projectPath, subProject);
710
+
711
+ const fileContent = "export const App = () => {}";
712
+ vi.mocked(fs.existsSync).mockImplementation((p: string | fs.PathLike) => {
713
+ const s = p as string;
714
+ if (s.includes(".react-map/cache")) return true;
715
+ if (s.includes("/src/App.tsx")) return true;
716
+ return false;
717
+ });
718
+ vi.mocked(fs.readFileSync).mockReturnValue(fileContent);
719
+
720
+ const content = (await projectManager.getSymbolContent(
721
+ projectPath,
722
+ "App",
723
+ subProject,
724
+ )) as { content: string }[];
725
+ expect(content[0].content).toContain("App");
726
+ });
727
+
728
+ it("should handle missing file on disk for content", async () => {
729
+ mockDb.prepare.mockReturnValueOnce({
730
+ all: vi.fn().mockReturnValue([
731
+ {
732
+ id: "app-id",
733
+ name: "App",
734
+ file: "/src/App.tsx",
735
+ line: 1,
736
+ column: 1,
737
+ kind: "component",
738
+ type: "function",
739
+ },
740
+ ]),
741
+ } as unknown as Database.Statement);
742
+ vi.mocked(fs.existsSync).mockImplementation((p: string | fs.PathLike) => {
743
+ const s = p as string;
744
+ if (s.includes(".react-map/cache")) return true;
745
+ return false;
746
+ });
747
+ const content = (await projectManager.getSymbolContent(
748
+ projectPath,
749
+ "App",
750
+ )) as { error: string }[];
751
+ expect(content[0].error).toBeDefined();
752
+ });
753
+
754
+ it("should return error if symbol not found for content", async () => {
755
+ mockDb.prepare.mockReturnValueOnce({
756
+ all: vi.fn().mockReturnValue([]),
757
+ } as unknown as Database.Statement);
758
+ const result = (await projectManager.getSymbolContent(
759
+ projectPath,
760
+ "NonExistent",
761
+ )) as { error: string };
762
+ expect(result.error).toBeDefined();
763
+ });
764
+
765
+ it("should open project automatically for getSymbolLocation", async () => {
766
+ mockDb.prepare.mockReturnValueOnce({
767
+ all: vi.fn().mockReturnValue([]),
768
+ } as unknown as Database.Statement);
769
+ const loc = await projectManager.getSymbolLocation("/new-project", "App");
770
+ expect(loc).toEqual([]);
771
+ expect(analyzeProject).toHaveBeenCalledWith(
772
+ "/new-project",
773
+ expect.any(String),
774
+ undefined,
775
+ expect.any(String),
776
+ );
777
+ });
778
+ });
779
+
780
+ describe("Graph State", () => {
781
+ const projectPath = "/test/project";
782
+
783
+ beforeEach(async () => {
784
+ vi.mocked(analyzeProject).mockResolvedValue({
785
+ src: projectPath,
786
+ files: {
787
+ "/src/App.tsx": {
788
+ var: {
789
+ app: {
790
+ id: "app",
791
+ name: { type: "identifier" as const, name: "App" },
792
+ loc: { line: 1, column: 1 },
793
+ kind: "component" as const,
794
+ ui: { x: 0, y: 0, children: {} },
795
+ var: {
796
+ "app:v1": {
797
+ id: "app:v1",
798
+ name: { type: "identifier" as const, name: "v1" },
799
+ loc: { line: 2, column: 1 },
800
+ kind: "normal" as const,
801
+ },
802
+ },
803
+ },
804
+ },
805
+ },
806
+ },
807
+ edges: [],
808
+ } as unknown as JsonData);
809
+ await projectManager.openProject(projectPath);
810
+ });
811
+
812
+ it("should update graph positions with contextId and sub-items", async () => {
813
+ const positions = {
814
+ app: { x: 100, y: 200, isLayoutCalculated: true },
815
+ "app-render-1": { x: 50, y: 50 },
816
+ "app:v1": { x: 10, y: 10 },
817
+ };
818
+ await projectManager.updateGraphPosition(
819
+ projectPath,
820
+ undefined,
821
+ positions,
822
+ "app",
823
+ );
824
+ await projectManager.updateGraphPosition(
825
+ projectPath,
826
+ undefined,
827
+ positions,
828
+ "root",
829
+ );
830
+ await projectManager.updateGraphPosition(
831
+ projectPath,
832
+ undefined,
833
+ positions,
834
+ "app:v1",
835
+ ); // non-combo context
836
+ expect(fs.writeFileSync).toHaveBeenCalled();
837
+ });
838
+
839
+ it("should update positions in subproject", async () => {
840
+ const subProject = "packages/app";
841
+ await projectManager.openProject(projectPath, subProject);
842
+ const success = await projectManager.updateGraphPosition(
843
+ projectPath,
844
+ subProject,
845
+ {},
846
+ );
847
+ expect(success).toBe(true);
848
+ });
849
+
850
+ it("should open project automatically for updateGraphPosition", async () => {
851
+ const success = await projectManager.updateGraphPosition(
852
+ "/new-project",
853
+ undefined,
854
+ {},
855
+ );
856
+ expect(success).toBe(true);
857
+ });
858
+
859
+ it("should handle partial symbol matches", async () => {
860
+ mockDb.prepare.mockReturnValueOnce({
861
+ all: vi.fn().mockReturnValue([
862
+ {
863
+ id: "app",
864
+ name: "App",
865
+ file: "/src/App.tsx",
866
+ line: 1,
867
+ column: 1,
868
+ kind: "component",
869
+ type: "function",
870
+ props_json: "[]",
871
+ },
872
+ ]),
873
+ } as unknown as Database.Statement);
874
+ const results = await projectManager.findSymbol(
875
+ projectPath,
876
+ "Ap",
877
+ undefined,
878
+ false,
879
+ ); // Matches "App"
880
+ expect(results.definitions).toHaveLength(1);
881
+ expect(results.definitions[0].name).toBe("App");
882
+ expect(results.externalUsages).toHaveLength(0);
883
+ });
884
+
885
+ it("should save and read app state", async () => {
886
+ const state = { zoom: 1 };
887
+ vi.mocked(fs.existsSync).mockReturnValue(true);
888
+ vi.mocked(fs.readFileSync).mockReturnValue(JSON.stringify(state));
889
+
890
+ await projectManager.saveAppState(projectPath, state);
891
+ expect(fs.writeFileSync).toHaveBeenCalled();
892
+
893
+ const readState = await projectManager.readAppState(projectPath);
894
+ expect(readState).toEqual(state);
895
+ });
896
+ });
897
+ });