codeCommentPreviewStore.test.ts 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. import { beforeEach, describe, expect, it } from "vitest";
  2. import {
  3. buildCodeCommentPreviewBucketKey,
  4. useCodeCommentPreviewStore,
  5. } from "@/store/codeCommentPreviewStore.js";
  6. import type { CodeCommentPreview } from "@/lib/codeCommentContext.js";
  7. const baseComment: CodeCommentPreview = {
  8. id: "comment-1",
  9. sourcePath: "/workspace/src/app.ts",
  10. sourceTitle: "app.ts",
  11. startLine: 10,
  12. endLine: 10,
  13. selectedText: "const value = 1;",
  14. comment: "检查这里。",
  15. };
  16. describe("codeCommentPreviewStore", () => {
  17. beforeEach(() => {
  18. useCodeCommentPreviewStore.setState({ commentsByBucketKey: {} });
  19. });
  20. it("keeps preview comments by workspace and file after the pane unmounts", () => {
  21. const params = {
  22. workspacePath: "/workspace",
  23. sourcePath: "/workspace/src/app.ts",
  24. };
  25. useCodeCommentPreviewStore.getState().addComment({
  26. ...params,
  27. comment: baseComment,
  28. });
  29. expect(useCodeCommentPreviewStore.getState().getComments(params)).toEqual([
  30. baseComment,
  31. ]);
  32. expect(
  33. useCodeCommentPreviewStore.getState().commentsByBucketKey[
  34. buildCodeCommentPreviewBucketKey(params)
  35. ],
  36. ).toHaveLength(1);
  37. });
  38. it("removes a source comment without requiring the original file pane to be mounted", () => {
  39. useCodeCommentPreviewStore.getState().addComment({
  40. workspacePath: "/workspace",
  41. workspaceIdentity: "remote:ssh:demo:/workspace",
  42. sourcePath: "/workspace/src/app.ts",
  43. comment: baseComment,
  44. });
  45. useCodeCommentPreviewStore.getState().removeCommentBySource({
  46. id: baseComment.id,
  47. workspacePath: "/workspace",
  48. workspaceIdentity: "remote:ssh:demo:/workspace",
  49. });
  50. expect(
  51. useCodeCommentPreviewStore.getState().getComments({
  52. workspacePath: "/workspace",
  53. workspaceIdentity: "remote:ssh:demo:/workspace",
  54. sourcePath: "/workspace/src/app.ts",
  55. }),
  56. ).toEqual([]);
  57. });
  58. it("can restore a preview comment when sending is rejected", () => {
  59. useCodeCommentPreviewStore.getState().restoreCommentFromAttachment({
  60. ...baseComment,
  61. id: baseComment.id,
  62. workspacePath: "/workspace",
  63. sourcePath: "/workspace/src/app.ts",
  64. });
  65. expect(
  66. useCodeCommentPreviewStore.getState().getComments({
  67. workspacePath: "/workspace",
  68. sourcePath: "/workspace/src/app.ts",
  69. }),
  70. ).toEqual([baseComment]);
  71. });
  72. });