assistantPreviewCards.test.ts 13 KB

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