Skip to content

armgit / kaneo

Public Imported from GitHub
0

⚡ Project management that doesn't become the project. Boards, Gantt charts, and GitHub integration. Self-host it or use Kaneo Cloud.

fix(ws): reconcile workspace revocation with current board tests

Tin95fe19dcd7f9
diff --cc apps/web/src/hooks/use-project-websocket.ts
index 022434f78,fabd85c6f..f367cf192
--- a/apps/web/src/hooks/use-project-websocket.ts
+++ b/apps/web/src/hooks/use-project-websocket.ts
@@@ -1,6 -1,10 +1,11 @@@
+ import { applyBoardReorder } from "@/components/kanban-board/apply-reorder";
+ import {
+ getBoardCacheVersion,
+ markBoardCacheChanged,
+ } from "@/lib/board-cache-version";
import { windowId } from "@kaneo/libs";
import { useQueryClient } from "@tanstack/react-query";
+import { useNavigate } from "@tanstack/react-router";
import { useEffect } from "react";
import { getApiUrl } from "@/fetchers/get-api-url";
import { authClient } from "@/lib/auth-client";
@@@ -149,22 -419,15 +421,26 @@@ export function useProjectWebSocket(pro
}
};
- ws.onclose = () => {
+ ws.onclose = (event) => {
if (disposed || activeSocket !== ws) return;
clearPing();
+ if (healthyTimeout !== null) {
+ clearTimeout(healthyTimeout);
+ healthyTimeout = null;
+ }
activeSocket = null;
+ if (
+ event?.code === 1008 &&
+ event.reason === "Workspace access revoked"
+ ) {
+ disposed = true;
+ void queryClient.cancelQueries();
+ queryClient.clear();
+ void navigate({ to: "/dashboard" });
+ return;
+ }
+
if (retries < MAX_RETRIES) {
const delay = BASE_DELAY * 2 ** retries; // 1s, 2s, 4s, 8s, 16s
retries += 1;
diff --cc tests/api/ws/reorder-delivery.test.ts
index 000000000,570adcae0..ae4f7cf52
mode 000000,100644..100644
--- a/tests/api/ws/reorder-delivery.test.ts
+++ b/tests/api/ws/reorder-delivery.test.ts
@@@ -1,0 -1,62 +1,65 @@@
+ import type { WSContext } from "hono/ws";
+ import { afterEach, expect, it, vi } from "vite-plus/test";
+ import { publishEvent } from "../../../apps/api/src/events";
+ import {
+ addConnection,
+ initializeWebSocketAdapter,
+ removeConnection,
+ shutdownWebSocketAdapter,
+ } from "../../../apps/api/src/ws";
+ vi.mock("../../../apps/api/src/database", () => ({
+ default: {
- select: () => ({
++ select: (fields: Record<string, unknown>) => ({
+ from: () => ({
- where: () => ({ limit: async () => [{ workspaceId: "workspace" }] }),
++ where: () =>
++ fields.userId
++ ? Promise.resolve([{ userId: "observer" }])
++ : { limit: async () => [{ workspaceId: "workspace" }] },
+ }),
+ }),
+ },
+ }));
+ vi.mock("../../../apps/api/src/redis", () => ({
+ isRedisConfigured: () => false,
+ closeRedis: vi.fn(),
+ }));
+ afterEach(async () => {
+ await shutdownWebSocketAdapter();
+ vi.useRealTimers();
+ });
+ it.each([1, 2])(
+ "delivers %i committed reorder batch through the real subscriber and local adapter",
+ async (batches) => {
+ vi.useFakeTimers();
+ await initializeWebSocketAdapter();
+ const ws = { send: vi.fn(), close: vi.fn() };
+ const connection = addConnection(
+ "project",
+ ws as unknown as WSContext,
+ "observer",
+ "window",
+ "workspace",
+ );
+ const tasks = [
+ { id: "one", position: 1, status: "to-do" },
+ { id: "two", position: 0, status: "to-do" },
+ ];
+ try {
+ for (let index = 0; index < batches; index++)
+ await publishEvent(
+ "tasks.reordered",
+ { projectId: "project", userId: "actor", tasks },
+ { waitForHandlers: true },
+ );
+ await vi.advanceTimersByTimeAsync(100);
+ expect(ws.send).toHaveBeenCalledTimes(batches);
+ expect(JSON.parse(ws.send.mock.calls[0][0])).toEqual({
+ type: "TASKS_REORDERED",
+ projectId: "project",
+ tasks,
+ });
+ } finally {
+ removeConnection("project", connection);
+ }
+ },
+ );