zcodeDraftSkillInvalidation.test.ts 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. import { beforeEach, describe, expect, it, vi } from "vitest";
  2. import type { IZCodeSessionService } from "@zcode/services";
  3. import { invalidateDeferredDraftSessionForSkillChange } from "../src/lib/zcodeDraftSkillInvalidation.js";
  4. import { useZCodeSessionStore } from "../src/store/zcodeSessionStore.js";
  5. function createSessionService(
  6. closeSession: IZCodeSessionService["closeSession"] = vi.fn(async () => {}),
  7. ): Pick<IZCodeSessionService, "closeSession"> {
  8. return { closeSession };
  9. }
  10. beforeEach(() => {
  11. useZCodeSessionStore.setState((state) => ({
  12. ...state,
  13. workspaces: {},
  14. }));
  15. });
  16. describe("invalidateDeferredDraftSessionForSkillChange", () => {
  17. it("closes and clears the current workspace draft session", async () => {
  18. const closeSession = vi.fn(async () => {});
  19. const workspacePath = "/workspace/app";
  20. const workspaceIdentity = "ssh:dev";
  21. useZCodeSessionStore.getState().setDraftSessionId(workspacePath, "draft-1", workspaceIdentity);
  22. await invalidateDeferredDraftSessionForSkillChange({
  23. reason: "test",
  24. workspacePath,
  25. workspaceIdentity,
  26. zcodeSessionService: createSessionService(closeSession),
  27. });
  28. expect(closeSession).toHaveBeenCalledWith({
  29. workspacePath,
  30. workspaceIdentity,
  31. sessionId: "draft-1",
  32. });
  33. expect(
  34. useZCodeSessionStore.getState().getWorkspaceState(workspacePath, workspaceIdentity)
  35. .draftSessionId,
  36. ).toBeNull();
  37. expect(
  38. useZCodeSessionStore.getState().getWorkspaceState(workspacePath, workspaceIdentity)
  39. .draftRuntimeInvalidationVersion,
  40. ).toBe(1);
  41. });
  42. it("invalidates a protocol-v4 prewarm even when there is no legacy draft session", async () => {
  43. const closeSession = vi.fn(async () => {});
  44. const workspacePath = "/workspace/app";
  45. await invalidateDeferredDraftSessionForSkillChange({
  46. reason: "test",
  47. workspacePath,
  48. zcodeSessionService: createSessionService(closeSession),
  49. });
  50. expect(closeSession).not.toHaveBeenCalled();
  51. expect(
  52. useZCodeSessionStore.getState().getWorkspaceState(workspacePath)
  53. .draftRuntimeInvalidationVersion,
  54. ).toBe(1);
  55. });
  56. it("still clears the draft when closing the stale session fails", async () => {
  57. const closeSession = vi.fn(async () => {
  58. throw new Error("stale draft");
  59. });
  60. const workspacePath = "/workspace/app";
  61. useZCodeSessionStore.getState().setDraftSessionId(workspacePath, "draft-1");
  62. await invalidateDeferredDraftSessionForSkillChange({
  63. reason: "test",
  64. workspacePath,
  65. zcodeSessionService: createSessionService(closeSession),
  66. });
  67. expect(closeSession).toHaveBeenCalledWith({
  68. workspacePath,
  69. sessionId: "draft-1",
  70. });
  71. expect(
  72. useZCodeSessionStore.getState().getWorkspaceState(workspacePath).draftSessionId,
  73. ).toBeNull();
  74. expect(
  75. useZCodeSessionStore.getState().getWorkspaceState(workspacePath)
  76. .draftRuntimeInvalidationVersion,
  77. ).toBe(1);
  78. });
  79. });