assistantPreviewCards.test.ts 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471
  1. import { readFileSync } from "node:fs";
  2. import { describe, expect, it } from "vitest";
  3. import {
  4. buildAssistantPreviewCardFileSource,
  5. buildAssistantPreviewPptxAutoOpenRequest,
  6. } from "@/AssistantPreviewCards.js";
  7. import {
  8. buildAssistantPreviewCards,
  9. requiresAssistantPreviewCardFileStat,
  10. type AssistantPreviewCard,
  11. } from "@/lib/assistantPreviewCards.js";
  12. import {
  13. resolveAssistantPreviewCardsWithoutFileStat,
  14. resolveValidatedAssistantPreviewCards,
  15. } from "@/lib/assistantPreviewCardValidation.js";
  16. const assistantPreviewCardsSource = readFileSync(
  17. new URL("../src/AssistantPreviewCards.tsx", import.meta.url),
  18. "utf8",
  19. );
  20. describe("AssistantPreviewCards presentation", () => {
  21. it("统一使用 24px 的网址、文件和兜底图标", () => {
  22. expect(assistantPreviewCardsSource).toContain('<GlobeIcon className="size-6" />');
  23. expect(assistantPreviewCardsSource).toContain(
  24. "<FileDisplayIcon src={descriptor.fileIconSrc} size={24} />",
  25. );
  26. expect(assistantPreviewCardsSource).toContain('<FileTextIcon className="size-6" />');
  27. });
  28. });
  29. describe("assistant PPTX auto-open projection", () => {
  30. it("只投影最终可见卡片中的 PPTX,并把卡片签名并入一次性 key", () => {
  31. const cards = [
  32. {
  33. id: "file:pdf:/workspace/report.pdf",
  34. type: "file" as const,
  35. kind: "pdf" as const,
  36. title: "report.pdf",
  37. subtitleId: "chat.previewCards.pdf" as const,
  38. path: "/workspace/report.pdf",
  39. },
  40. {
  41. id: "file:pptx:/workspace/first.pptx",
  42. type: "file" as const,
  43. kind: "pptx" as const,
  44. title: "first.pptx",
  45. subtitleId: "chat.previewCards.pptx" as const,
  46. path: "/workspace/first.pptx",
  47. },
  48. {
  49. id: "file:pptx:/workspace/second.pptx",
  50. type: "file" as const,
  51. kind: "pptx" as const,
  52. title: "second.pptx",
  53. subtitleId: "chat.previewCards.pptx" as const,
  54. path: "/workspace/second.pptx",
  55. },
  56. ];
  57. const request = buildAssistantPreviewPptxAutoOpenRequest(cards, "turn-key", {
  58. workspacePath: "/workspace",
  59. workspaceIdentity: "remote:ssh:host:/workspace",
  60. workspaceRemoteSessionId: "remote-1",
  61. });
  62. expect(request?.sources).toEqual([
  63. expect.objectContaining({ path: "/workspace/first.pptx" }),
  64. expect.objectContaining({ path: "/workspace/second.pptx" }),
  65. ]);
  66. expect(request?.key).toContain("turn-key");
  67. expect(request?.key).toContain("first.pptx");
  68. expect(request?.key).toContain("second.pptx");
  69. expect(request?.key).not.toContain("report.pdf");
  70. });
  71. });
  72. describe("buildAssistantPreviewCards", () => {
  73. it("为远程 Markdown 卡片构建完整 workspace scope", () => {
  74. expect(
  75. buildAssistantPreviewCardFileSource(
  76. {
  77. id: "markdown:/workspace/README.md",
  78. type: "markdown",
  79. kind: "markdown",
  80. title: "README.md",
  81. path: "/workspace/README.md",
  82. subtitleId: "chat.previewCards.markdown",
  83. },
  84. {
  85. workspacePath: "/workspace",
  86. workspaceIdentity: "remote:ssh:host-a:/workspace",
  87. workspaceRemoteSessionId: "session-a",
  88. },
  89. ),
  90. ).toEqual({
  91. type: "file",
  92. title: "README.md",
  93. path: "/workspace/README.md",
  94. workspacePath: "/workspace",
  95. workspaceIdentity: "remote:ssh:host-a:/workspace",
  96. workspaceRemoteSessionId: "session-a",
  97. });
  98. });
  99. it("builds website cards for localhost links with markdown labels", () => {
  100. const cards = buildAssistantPreviewCards(
  101. "Open [Neon Snake](http://localhost:5173/play) when ready.",
  102. "/workspace",
  103. );
  104. expect(cards).toEqual([
  105. {
  106. id: "website:http://localhost:5173/play",
  107. type: "website",
  108. title: "Neon Snake",
  109. subtitleId: "chat.previewCards.website",
  110. url: "http://localhost:5173/play",
  111. },
  112. ]);
  113. });
  114. it("builds website cards for 127.0.0.1 links", () => {
  115. const cards = buildAssistantPreviewCards("Preview: http://127.0.0.1:3000.", "/workspace");
  116. expect(cards).toMatchObject([
  117. {
  118. type: "website",
  119. title: "127.0.0.1:3000",
  120. url: "http://127.0.0.1:3000",
  121. },
  122. ]);
  123. });
  124. it("builds markdown cards from links and raw paths without duplicates", () => {
  125. const cards = buildAssistantPreviewCards(
  126. [
  127. "Updated [summary](./docs/ui/task-file-change-summary.md).",
  128. "Also see docs/ui/task-file-change-summary.md and CHANGELOG.md.",
  129. ].join("\n"),
  130. "/workspace",
  131. {
  132. changedFilePaths: [
  133. "/workspace/docs/ui/task-file-change-summary.md",
  134. "/workspace/CHANGELOG.md",
  135. ],
  136. },
  137. );
  138. expect(cards).toMatchObject([
  139. {
  140. type: "markdown",
  141. title: "CHANGELOG.md",
  142. path: "/workspace/CHANGELOG.md",
  143. },
  144. {
  145. type: "markdown",
  146. title: "task-file-change-summary.md",
  147. path: "/workspace/docs/ui/task-file-change-summary.md",
  148. },
  149. ]);
  150. });
  151. it("builds html website cards from generated html file paths", () => {
  152. const cards = buildAssistantPreviewCards(
  153. "Generated index.html and ./public/demo.htm.",
  154. "/workspace",
  155. {
  156. changedFilePaths: ["/workspace/index.html", "/workspace/public/demo.htm"],
  157. },
  158. );
  159. expect(cards).toMatchObject([
  160. {
  161. type: "website",
  162. title: "demo.htm",
  163. subtitleId: "chat.previewCards.htmlWebsite",
  164. url: "file:///workspace/public/demo.htm",
  165. },
  166. {
  167. type: "website",
  168. title: "index.html",
  169. subtitleId: "chat.previewCards.htmlWebsite",
  170. url: "file:///workspace/index.html",
  171. },
  172. ]);
  173. });
  174. it("builds html website cards from file URLs", () => {
  175. const cards = buildAssistantPreviewCards(
  176. "Preview file:///workspace/demo.html when ready.",
  177. "/workspace",
  178. { changedFilePaths: ["/workspace/demo.html"] },
  179. );
  180. expect(cards).toMatchObject([
  181. {
  182. type: "website",
  183. title: "demo.html",
  184. subtitleId: "chat.previewCards.htmlWebsite",
  185. url: "file:///workspace/demo.html",
  186. },
  187. ]);
  188. });
  189. it("resolves bare html names from changed file paths first", () => {
  190. const cards = buildAssistantPreviewCards("Generated index.html.", "/workspace", {
  191. changedFilePaths: ["/workspace/game/index.html"],
  192. });
  193. expect(cards).toMatchObject([
  194. {
  195. type: "website",
  196. title: "index.html",
  197. subtitleId: "chat.previewCards.htmlWebsite",
  198. url: "file:///workspace/game/index.html",
  199. },
  200. ]);
  201. expect(cards).toHaveLength(1);
  202. });
  203. it("does not build html file cards when a local http preview exists", () => {
  204. const cards = buildAssistantPreviewCards(
  205. [
  206. "Preview http://localhost:5173.",
  207. "Generated index.html and file:///workspace/public/demo.html.",
  208. "See [notes](./docs/notes.md).",
  209. ].join("\n"),
  210. "/workspace",
  211. {
  212. changedFilePaths: ["/workspace/game/index.html", "/workspace/docs/notes.md"],
  213. },
  214. );
  215. expect(cards).toMatchObject([
  216. {
  217. type: "markdown",
  218. path: "/workspace/docs/notes.md",
  219. },
  220. {
  221. type: "website",
  222. url: "http://localhost:5173",
  223. },
  224. ]);
  225. expect(cards).toHaveLength(2);
  226. });
  227. it("filters invalid website candidates after regex extraction", () => {
  228. expect(buildAssistantPreviewCards("Preview http://localhost:70000.", "/workspace")).toEqual([]);
  229. expect(
  230. buildAssistantPreviewCards(
  231. "Preview file://demo.html and file:///workspace/demo.html.",
  232. "/workspace",
  233. { changedFilePaths: ["/workspace/demo.html"] },
  234. ),
  235. ).toMatchObject([
  236. {
  237. type: "website",
  238. url: "file:///workspace/demo.html",
  239. },
  240. ]);
  241. });
  242. it("trims markdown emphasis and CJK punctuation from localhost URLs", () => {
  243. const cards = buildAssistantPreviewCards(
  244. "服务地址是 **http://localhost:8765/dashboard/index.html**,你现在可以在浏览器打开试试。",
  245. "/workspace",
  246. {
  247. changedFilePaths: ["/workspace/dashboard/index.html"],
  248. },
  249. );
  250. expect(cards).toEqual([
  251. {
  252. id: "website:http://localhost:8765/dashboard/index.html",
  253. type: "website",
  254. title: "index.html",
  255. subtitleId: "chat.previewCards.website",
  256. url: "http://localhost:8765/dashboard/index.html",
  257. filePath: "/workspace/dashboard/index.html",
  258. },
  259. ]);
  260. expect(cards.map((card) => requiresAssistantPreviewCardFileStat(card))).toEqual([true]);
  261. });
  262. it("treats localhost html URL paths as workspace files before rendering", () => {
  263. const cards = buildAssistantPreviewCards(
  264. "服务地址是 http://localhost:8765/dashboard/index.html。",
  265. "/workspace",
  266. { changedFilePaths: ["/workspace/dashboard/index.html"] },
  267. );
  268. expect(cards).toEqual([
  269. {
  270. id: "website:http://localhost:8765/dashboard/index.html",
  271. type: "website",
  272. title: "index.html",
  273. subtitleId: "chat.previewCards.website",
  274. url: "http://localhost:8765/dashboard/index.html",
  275. filePath: "/workspace/dashboard/index.html",
  276. },
  277. ]);
  278. expect(cards.map((card) => requiresAssistantPreviewCardFileStat(card))).toEqual([true]);
  279. });
  280. it("deduplicates localhost cards and ignores polluted markdown link labels", () => {
  281. const cards = buildAssistantPreviewCards(
  282. [
  283. "[** (kernel](http://127.0.0.1:8781)",
  284. "[) 。完整](http://127.0.0.1:8781)",
  285. "也可以直接打开 http://127.0.0.1:8781。",
  286. ].join("\n"),
  287. "/workspace",
  288. );
  289. expect(cards).toEqual([
  290. {
  291. id: "website:http://127.0.0.1:8781",
  292. type: "website",
  293. title: "127.0.0.1:8781",
  294. subtitleId: "chat.previewCards.website",
  295. url: "http://127.0.0.1:8781",
  296. },
  297. ]);
  298. });
  299. it("classifies cards that require file stat", () => {
  300. const cards: AssistantPreviewCard[] = [
  301. {
  302. id: "website:http://localhost:5173",
  303. type: "website",
  304. title: "localhost:5173",
  305. subtitleId: "chat.previewCards.website",
  306. url: "http://localhost:5173",
  307. },
  308. {
  309. id: "website:http://localhost:70000",
  310. type: "website",
  311. title: "localhost:70000",
  312. subtitleId: "chat.previewCards.website",
  313. url: "http://localhost:70000",
  314. },
  315. {
  316. id: "website:http://localhost:8765/dashboard/index.html",
  317. type: "website",
  318. title: "index.html",
  319. subtitleId: "chat.previewCards.website",
  320. url: "http://localhost:8765/dashboard/index.html",
  321. filePath: "/workspace/dashboard/index.html",
  322. },
  323. {
  324. id: "website:file:///workspace/index.html",
  325. type: "website",
  326. title: "index.html",
  327. subtitleId: "chat.previewCards.htmlWebsite",
  328. url: "file:///workspace/index.html",
  329. },
  330. {
  331. id: "website:file:///workspace/missing.html",
  332. type: "website",
  333. title: "missing.html",
  334. subtitleId: "chat.previewCards.htmlWebsite",
  335. url: "file:///workspace/missing.html",
  336. },
  337. {
  338. id: "markdown:/workspace/exists.md",
  339. type: "markdown",
  340. kind: "markdown",
  341. title: "exists.md",
  342. subtitleId: "chat.previewCards.markdown",
  343. path: "/workspace/exists.md",
  344. },
  345. {
  346. id: "markdown:/workspace/dir.md",
  347. type: "markdown",
  348. kind: "markdown",
  349. title: "dir.md",
  350. subtitleId: "chat.previewCards.markdown",
  351. path: "/workspace/dir.md",
  352. },
  353. {
  354. id: "markdown:/workspace/missing.md",
  355. type: "markdown",
  356. kind: "markdown",
  357. title: "missing.md",
  358. subtitleId: "chat.previewCards.markdown",
  359. path: "/workspace/missing.md",
  360. },
  361. ];
  362. expect(cards.map((card) => requiresAssistantPreviewCardFileStat(card))).toEqual([
  363. false,
  364. false,
  365. true,
  366. true,
  367. true,
  368. true,
  369. true,
  370. true,
  371. ]);
  372. });
  373. it("returns stat-free preview cards synchronously", () => {
  374. const cards: AssistantPreviewCard[] = [
  375. {
  376. id: "website:http://localhost:5173",
  377. type: "website",
  378. title: "localhost:5173",
  379. subtitleId: "chat.previewCards.website",
  380. url: "http://localhost:5173",
  381. },
  382. {
  383. id: "website:http://localhost:70000",
  384. type: "website",
  385. title: "bad localhost",
  386. subtitleId: "chat.previewCards.website",
  387. url: "http://localhost:70000",
  388. },
  389. ];
  390. expect(resolveAssistantPreviewCardsWithoutFileStat(cards)).toEqual([cards[0]]);
  391. });
  392. it("waits for all file-backed preview checks before returning renderable cards", async () => {
  393. const cards: AssistantPreviewCard[] = [
  394. {
  395. id: "markdown:/workspace/README.md",
  396. type: "markdown",
  397. kind: "markdown",
  398. title: "README.md",
  399. subtitleId: "chat.previewCards.markdown",
  400. path: "/workspace/README.md",
  401. },
  402. {
  403. id: "markdown:/workspace/missing.md",
  404. type: "markdown",
  405. kind: "markdown",
  406. title: "missing.md",
  407. subtitleId: "chat.previewCards.markdown",
  408. path: "/workspace/missing.md",
  409. },
  410. {
  411. id: "website:file:///workspace/index.html",
  412. type: "website",
  413. title: "index.html",
  414. subtitleId: "chat.previewCards.htmlWebsite",
  415. url: "file:///workspace/index.html",
  416. },
  417. ];
  418. const checkedPaths: string[] = [];
  419. const fileService = {
  420. checkFilesExist: async ({ paths }: { paths: string[] }) =>
  421. paths.map((path) => {
  422. checkedPaths.push(path);
  423. return {
  424. path,
  425. exists: path.endsWith("README.md") || path.endsWith("index.html"),
  426. };
  427. }),
  428. };
  429. await expect(resolveValidatedAssistantPreviewCards(cards, fileService)).resolves.toEqual([
  430. cards[0],
  431. cards[2],
  432. ]);
  433. expect(checkedPaths).toEqual([
  434. "/workspace/README.md",
  435. "/workspace/missing.md",
  436. "/workspace/index.html",
  437. ]);
  438. });
  439. });