render.spec.ts 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. import { describe, expect, it } from 'vitest'
  2. import { pathToFileURL } from 'node:url'
  3. import { join, resolve } from 'node:path'
  4. import {
  5. DEFAULT_MAX_LOCATIONS,
  6. DEFAULT_MAX_RESULT_CHARS,
  7. formatHover,
  8. formatLocations,
  9. LSP_OPERATIONS,
  10. parseLspArgs,
  11. presentLspCall,
  12. renderUri,
  13. } from '@deepseek-ai/dsh-tool-lsp'
  14. import type { LspLocation } from '@deepseek-ai/dsh-lsp'
  15. const WS = resolve('/home/u/proj')
  16. const WS_URI = pathToFileURL(WS).href
  17. function loc(uri: string, line: number, character = 0): LspLocation {
  18. return { uri, range: { start: { line, character }, end: { line, character: character + 1 } } }
  19. }
  20. describe('parseLspArgs', () => {
  21. it('accepts the four operations and converts one-based to zero-based', () => {
  22. for (const operation of LSP_OPERATIONS) {
  23. const input = parseLspArgs({ operation, file_path: 'a.ts', line: 3, character: 5 })
  24. expect(input.operation).toBe(operation)
  25. expect(input.position).toEqual({ line: 2, character: 4 })
  26. }
  27. })
  28. it('rejects an unknown operation', () => {
  29. expect(() => parseLspArgs({ operation: 'rename', file_path: 'a.ts', line: 1, character: 1 }))
  30. .toThrow(/operation must be one of/)
  31. })
  32. it('rejects a blank file_path', () => {
  33. expect(() => parseLspArgs({ operation: 'hover', file_path: ' ', line: 1, character: 1 }))
  34. .toThrow(/file_path/)
  35. })
  36. it('rejects non-positive or non-integer coordinates', () => {
  37. expect(() => parseLspArgs({ operation: 'hover', file_path: 'a.ts', line: 0, character: 1 })).toThrow(/line/)
  38. expect(() => parseLspArgs({ operation: 'hover', file_path: 'a.ts', line: 1, character: 0 })).toThrow(/character/)
  39. expect(() => parseLspArgs({ operation: 'hover', file_path: 'a.ts', line: 1.5, character: 1 })).toThrow(/line/)
  40. })
  41. })
  42. describe('renderUri', () => {
  43. it('relativizes a file: URI inside the workspace with forward slashes', () => {
  44. const uri = pathToFileURL(join(WS, 'src', 'a.ts')).href
  45. expect(renderUri(uri, WS_URI)).toBe('src/a.ts')
  46. })
  47. it('returns an absolute path for a file: URI outside the workspace', () => {
  48. const outside = resolve(WS, '..', 'other', 'lib', 'b.ts')
  49. const uri = pathToFileURL(outside).href
  50. expect(renderUri(uri, WS_URI)).toBe(outside.replaceAll('\\', '/'))
  51. })
  52. it('renders the workspace root itself as "."', () => {
  53. expect(renderUri(WS_URI, WS_URI)).toBe('.')
  54. })
  55. it('keeps an in-workspace path whose first segment starts with dots relative', () => {
  56. // `..generated` is a real in-workspace dir, not a parent escape; only a `..` segment is external.
  57. const uri = pathToFileURL(join(WS, '..generated', 'a.ts')).href
  58. expect(renderUri(uri, WS_URI)).toBe('..generated/a.ts')
  59. })
  60. it('relativizes Windows execution-world URIs on a non-Windows host', () => {
  61. expect(renderUri('file:///C:/WORKSPACE/src/a.ts', 'file:///c:/workspace')).toBe('src/a.ts')
  62. expect(renderUri('file:///D:/lib/b.ts', 'file:///C:/workspace')).toBe('D:/lib/b.ts')
  63. })
  64. it('renders remote file authorities without host path conversion', () => {
  65. expect(renderUri('file://server/share/workspace/a.ts', 'file://server/share/workspace')).toBe('a.ts')
  66. expect(renderUri('file://SERVER/share/workspace/src/A.ts', 'file://server/Share/Workspace')).toBe('src/A.ts')
  67. expect(renderUri('file://other/share/b.ts', 'file://server/share/workspace')).toBe('//other/share/b.ts')
  68. expect(renderUri('file:///D:/lib/a.ts', 'file://server/share/workspace')).toBe('D:/lib/a.ts')
  69. expect(renderUri('file:///a.ts', 'file://server/')).toBe('/a.ts')
  70. expect(renderUri('file:///a.ts', 'file:///')).toBe('a.ts')
  71. })
  72. it('preserves backslashes as ordinary POSIX filename characters', () => {
  73. expect(renderUri('file:///home/u/proj/dir%5Cname/a.ts', 'file:///home/u/proj')).toBe('dir\\name/a.ts')
  74. })
  75. it('keeps malformed or mismatched URI coordinates verbatim', () => {
  76. expect(renderUri('file://[', WS_URI)).toBe('file://[')
  77. expect(renderUri('file:///a.ts', 'https://example.com/workspace')).toBe('file:///a.ts')
  78. expect(renderUri('file:///a.ts', 'file:///bad%ZZ')).toBe('file:///a.ts')
  79. expect(renderUri('file:///C:/workspace/bad%5Cpath', 'file:///C:/workspace')).toBe('file:///C:/workspace/bad%5Cpath')
  80. expect(renderUri('file:///short', 'file:///short/deeper')).toBe('/short')
  81. expect(renderUri('file:///', 'file:///C:/workspace')).toBe('/')
  82. })
  83. it('keeps a non-file URI verbatim', () => {
  84. expect(renderUri('untitled:Untitled-1', WS_URI)).toBe('untitled:Untitled-1')
  85. expect(renderUri('jdt://contents/Foo.class', WS_URI)).toBe('jdt://contents/Foo.class')
  86. })
  87. it('keeps a malformed file: URI verbatim when it cannot be parsed to a path', () => {
  88. // An encoded path separator is invalid on every platform and must remain verbatim.
  89. expect(renderUri('file:///bad%2Fpath', WS_URI)).toBe('file:///bad%2Fpath')
  90. expect(renderUri('file:///bad%00path', WS_URI)).toBe('file:///bad%00path')
  91. })
  92. })
  93. describe('formatLocations', () => {
  94. it('renders a no-result line for an empty list', () => {
  95. expect(formatLocations([], WS_URI, DEFAULT_MAX_LOCATIONS, DEFAULT_MAX_RESULT_CHARS)).toBe('No results.')
  96. })
  97. it('renders one-based path:line:character grouped by file', () => {
  98. const a = pathToFileURL(join(WS, 'a.ts')).href
  99. const text = formatLocations([loc(a, 0, 0), loc(a, 4, 2)], WS_URI, DEFAULT_MAX_LOCATIONS, DEFAULT_MAX_RESULT_CHARS)
  100. expect(text).toBe('a.ts:1:1\na.ts:5:3')
  101. })
  102. it('caps at maxLocations and marks the omission', () => {
  103. const a = pathToFileURL(join(WS, 'a.ts')).href
  104. const many = Array.from({ length: 5 }, (_, i) => loc(a, i))
  105. const text = formatLocations(many, WS_URI, 2, DEFAULT_MAX_RESULT_CHARS)
  106. expect(text).toContain('a.ts:1:1')
  107. expect(text).toContain('3 more locations omitted (limit 2).')
  108. })
  109. it('uses the singular omission marker for exactly one extra', () => {
  110. const a = pathToFileURL(join(WS, 'a.ts')).href
  111. const text = formatLocations([loc(a, 0), loc(a, 1)], WS_URI, 1, DEFAULT_MAX_RESULT_CHARS)
  112. expect(text).toContain('1 more location omitted (limit 1).')
  113. })
  114. it('caps the complete location text even when one URI is enormous', () => {
  115. const maxResultChars = 80
  116. const text = formatLocations([loc(`custom:${'x'.repeat(1_000_000)}`, 0)], WS_URI, 1, maxResultChars)
  117. expect(text).toHaveLength(maxResultChars)
  118. expect(text).toContain('locations truncated')
  119. })
  120. })
  121. describe('formatHover', () => {
  122. it('renders a no-result line for null', () => {
  123. expect(formatHover(null, DEFAULT_MAX_RESULT_CHARS)).toBe('No hover information.')
  124. })
  125. it('returns short hover verbatim', () => {
  126. expect(formatHover({ contents: '```ts\nx: number\n```' }, DEFAULT_MAX_RESULT_CHARS)).toBe('```ts\nx: number\n```')
  127. })
  128. it('caps the complete hover text including its truncation marker', () => {
  129. const text = formatHover({ contents: 'a'.repeat(100) }, 60)
  130. expect(text).toHaveLength(60)
  131. expect(text).toContain('hover truncated (limit 60 characters).')
  132. })
  133. it('still honors a cap smaller than the truncation marker', () => {
  134. expect(formatHover({ contents: 'a'.repeat(100) }, 10)).toHaveLength(10)
  135. })
  136. })
  137. describe('presentLspCall', () => {
  138. it('is a generic search card with an operation/cursor title and a line location', () => {
  139. expect(presentLspCall({ operation: 'findReferences', file_path: 'a.ts', line: 3, character: 7 })).toEqual({
  140. card: 'generic',
  141. kind: 'search',
  142. title: 'LSP findReferences a.ts:3:7',
  143. locations: [{ path: 'a.ts', line: 3 }],
  144. })
  145. })
  146. })