|
| 1 | +import Path from "node:path"; |
| 2 | +import Fs from "node:fs/promises"; |
| 3 | + |
| 4 | +import { it, expect, describe } from "vitest"; |
| 5 | +import { analyzeDocument } from "src/analyzer.js"; |
| 6 | +import { getReferences } from "src/references.js"; |
| 7 | +import { TextDocument } from "vscode-languageserver-textdocument"; |
| 8 | + |
| 9 | +const fixturesDir = Path.resolve(import.meta.dirname, "__fixtures__"); |
| 10 | + |
| 11 | +async function setupFixture(name: string) { |
| 12 | + const filePath = Path.resolve(fixturesDir, name); |
| 13 | + const content = await Fs.readFile(filePath, "utf-8"); |
| 14 | + const analysis = analyzeDocument(content, name); |
| 15 | + const uri = `file:///${name}`; |
| 16 | + const doc = TextDocument.create(uri, "typescript", 1, content); |
| 17 | + |
| 18 | + return { doc, analysis: { ...analysis, uri } }; |
| 19 | +} |
| 20 | + |
| 21 | +describe("getReferences", () => { |
| 22 | + it("finds dependsOn refs when cursor is on a registration name", async () => { |
| 23 | + const { doc, analysis } = await setupFixture("valid.ts"); |
| 24 | + const content = doc.getText(); |
| 25 | + const idx = content.indexOf('"compile"'); |
| 26 | + const offset = idx + 1; |
| 27 | + const position = doc.positionAt(offset); |
| 28 | + const locations = getReferences([analysis], position, doc, { includeDeclaration: true }); |
| 29 | + |
| 30 | + expect(locations.length).toBe(2); |
| 31 | + |
| 32 | + const compileReg = analysis.registrations.find((r) => r.name === "compile"); |
| 33 | + expect(locations).toContainEqual({ uri: analysis.uri, range: compileReg!.nameRange }); |
| 34 | + |
| 35 | + const buildReg = analysis.registrations.find((r) => r.name === "build"); |
| 36 | + const compileDep = buildReg!.configuration!.dependsOn.find((d) => d.name === "compile"); |
| 37 | + expect(locations).toContainEqual({ uri: analysis.uri, range: compileDep!.range }); |
| 38 | + }); |
| 39 | + |
| 40 | + it("finds registration + dependsOn refs from a dependsOn cursor", async () => { |
| 41 | + const { doc, analysis } = await setupFixture("valid.ts"); |
| 42 | + const content = doc.getText(); |
| 43 | + const dependsOnIdx = content.indexOf('dependsOn: ["compile"'); |
| 44 | + const offset = dependsOnIdx + 'dependsOn: ["'.length + 1; |
| 45 | + const position = doc.positionAt(offset); |
| 46 | + const locations = getReferences([analysis], position, doc, { includeDeclaration: true }); |
| 47 | + |
| 48 | + expect(locations.length).toBe(2); |
| 49 | + |
| 50 | + const compileReg = analysis.registrations.find((r) => r.name === "compile"); |
| 51 | + expect(locations).toContainEqual({ uri: analysis.uri, range: compileReg!.nameRange }); |
| 52 | + |
| 53 | + const buildReg = analysis.registrations.find((r) => r.name === "build"); |
| 54 | + const compileDep = buildReg!.configuration!.dependsOn.find((d) => d.name === "compile"); |
| 55 | + expect(locations).toContainEqual({ uri: analysis.uri, range: compileDep!.range }); |
| 56 | + }); |
| 57 | + |
| 58 | + it("excludes declaration when includeDeclaration is false", async () => { |
| 59 | + const { doc, analysis } = await setupFixture("valid.ts"); |
| 60 | + const content = doc.getText(); |
| 61 | + const idx = content.indexOf('"compile"'); |
| 62 | + const offset = idx + 1; |
| 63 | + const position = doc.positionAt(offset); |
| 64 | + const locations = getReferences([analysis], position, doc, { includeDeclaration: false }); |
| 65 | + |
| 66 | + expect(locations.length).toBe(1); |
| 67 | + |
| 68 | + const buildReg = analysis.registrations.find((r) => r.name === "build"); |
| 69 | + const compileDep = buildReg!.configuration!.dependsOn.find((d) => d.name === "compile"); |
| 70 | + expect(locations).toContainEqual({ uri: analysis.uri, range: compileDep!.range }); |
| 71 | + }); |
| 72 | + |
| 73 | + it("returns only declaration for unreferenced task", async () => { |
| 74 | + const { doc, analysis } = await setupFixture("valid.ts"); |
| 75 | + const content = doc.getText(); |
| 76 | + const idx = content.indexOf('"clean-cache"'); |
| 77 | + const offset = idx + 1; |
| 78 | + const position = doc.positionAt(offset); |
| 79 | + const locations = getReferences([analysis], position, doc, { includeDeclaration: true }); |
| 80 | + |
| 81 | + expect(locations.length).toBe(1); |
| 82 | + |
| 83 | + const reg = analysis.registrations.find((r) => r.name === "clean-cache"); |
| 84 | + expect(locations[0]).toEqual({ uri: analysis.uri, range: reg!.nameRange }); |
| 85 | + }); |
| 86 | + |
| 87 | + it("returns empty for unreferenced task with includeDeclaration false", async () => { |
| 88 | + const { doc, analysis } = await setupFixture("valid.ts"); |
| 89 | + const content = doc.getText(); |
| 90 | + const idx = content.indexOf('"clean-cache"'); |
| 91 | + const offset = idx + 1; |
| 92 | + const position = doc.positionAt(offset); |
| 93 | + const locations = getReferences([analysis], position, doc, { includeDeclaration: false }); |
| 94 | + |
| 95 | + expect(locations).toEqual([]); |
| 96 | + }); |
| 97 | + |
| 98 | + it("returns empty for workspace-qualified references", async () => { |
| 99 | + const { doc, analysis } = await setupFixture("unresolved-deps.ts"); |
| 100 | + const content = doc.getText(); |
| 101 | + const idx = content.indexOf('"other-pkg:build"'); |
| 102 | + const offset = idx + 1; |
| 103 | + const position = doc.positionAt(offset); |
| 104 | + const locations = getReferences([analysis], position, doc, { includeDeclaration: true }); |
| 105 | + |
| 106 | + expect(locations).toEqual([]); |
| 107 | + }); |
| 108 | + |
| 109 | + it("returns empty for non-task strings", async () => { |
| 110 | + const { doc, analysis } = await setupFixture("valid.ts"); |
| 111 | + const content = doc.getText(); |
| 112 | + const idx = content.indexOf('"Compile TypeScript"'); |
| 113 | + const offset = idx + 1; |
| 114 | + const position = doc.positionAt(offset); |
| 115 | + const locations = getReferences([analysis], position, doc, { includeDeclaration: true }); |
| 116 | + |
| 117 | + expect(locations).toEqual([]); |
| 118 | + }); |
| 119 | +}); |
0 commit comments