From 08da9ad88de3164479de86ab78701d0425a10757 Mon Sep 17 00:00:00 2001 From: Vincent Driessen Date: Mon, 11 Aug 2025 14:18:47 +0200 Subject: [PATCH] Use randomized room names in e2e tests (#2583) --- e2e/node-sandbox/test/client.test.ts | 53 +++++++++++++++--------- e2e/node-sandbox/test/node.test.ts | 60 ++++++++++++++-------------- 2 files changed, 65 insertions(+), 48 deletions(-) diff --git a/e2e/node-sandbox/test/client.test.ts b/e2e/node-sandbox/test/client.test.ts index ae8a3668a5c..91c73d3fc12 100644 --- a/e2e/node-sandbox/test/client.test.ts +++ b/e2e/node-sandbox/test/client.test.ts @@ -3,12 +3,39 @@ import type { BaseUserMeta, JsonObject, User } from "@liveblocks/client"; import { Liveblocks } from "@liveblocks/node"; import { config } from "dotenv"; import WebSocket from "ws"; -import { describe, test, expect, vi } from "vitest"; +import { describe, test, expect, onTestFinished } from "vitest"; type OpaqueUser = User; config(); +// First, create the room with proper permissions +const nodeClient = new Liveblocks({ + secret: process.env.LIVEBLOCKS_SECRET_KEY!, + // @ts-expect-error hidden config + baseUrl: + process.env.LIVEBLOCKS_BASE_URL ?? + process.env.NEXT_PUBLIC_LIVEBLOCKS_BASE_URL ?? + "https://api.liveblocks.io", +}); + +async function createRandomTestRoom(): Promise { + const randomRoomId = `node-e2e-${Math.random().toString(36).substring(2, 15)}`; + + // Register cleanup + onTestFinished(async () => { + await nodeClient.deleteRoom(randomRoomId); + }); + + await nodeClient.createRoom( + randomRoomId, + { defaultAccesses: ["room:write"] }, + { idempotent: true } + ); + + return randomRoomId; +} + // Utility functions for client tests function wait(ms: number): Promise { return new Promise((resolve) => setTimeout(resolve, ms)); @@ -40,21 +67,7 @@ describe("@liveblocks/client package e2e", () => { "presence should work in node environment", { timeout: 15000 }, async () => { - // First, create the room with proper permissions - const serverClient = new Liveblocks({ - secret: process.env.LIVEBLOCKS_SECRET_KEY!, - // @ts-expect-error hidden config - baseUrl: - process.env.LIVEBLOCKS_BASE_URL ?? - process.env.NEXT_PUBLIC_LIVEBLOCKS_BASE_URL ?? - "https://api.liveblocks.io", - }); - - await serverClient.createRoom( - "node-e2e", - { defaultAccesses: ["room:write"] }, - { idempotent: true } - ); + const roomId = await createRandomTestRoom(); const clientA = createClient({ publicApiKey: @@ -74,10 +87,10 @@ describe("@liveblocks/client package e2e", () => { baseUrl: process.env.NEXT_PUBLIC_LIVEBLOCKS_BASE_URL, }); - const { room: roomA, leave: leaveA } = clientA.enterRoom("node-e2e", { + const { room: roomA, leave: leaveA } = clientA.enterRoom(roomId, { initialPresence: { name: "A" }, }); - const { room: roomB, leave: leaveB } = clientB.enterRoom("node-e2e", { + const { room: roomB, leave: leaveB } = clientB.enterRoom(roomId, { initialPresence: { name: "B" }, }); @@ -88,6 +101,8 @@ describe("@liveblocks/client package e2e", () => { let roomBSawA = false; roomA.subscribe("others", (others) => { + if (others.length === 0) return; // Ignore [] case + callbackACalled = true; if (others.some((user: OpaqueUser) => user.presence?.name === "B")) { roomASawB = true; @@ -95,6 +110,8 @@ describe("@liveblocks/client package e2e", () => { }); roomB.subscribe("others", (others) => { + if (others.length === 0) return; // Ignore [] case + callbackBCalled = true; if (others.some((user: OpaqueUser) => user.presence?.name === "A")) { roomBSawA = true; diff --git a/e2e/node-sandbox/test/node.test.ts b/e2e/node-sandbox/test/node.test.ts index 2a5b6c8355f..444fca23961 100644 --- a/e2e/node-sandbox/test/node.test.ts +++ b/e2e/node-sandbox/test/node.test.ts @@ -1,50 +1,50 @@ import { LiveList } from "@liveblocks/core"; import { Liveblocks } from "@liveblocks/node"; import { config } from "dotenv"; -import { describe, test, expect } from "vitest"; +import { describe, test, expect, onTestFinished } from "vitest"; config(); -describe("@liveblocks/node package e2e", () => { - test("create the room", async () => { - const client = new Liveblocks({ - secret: process.env.LIVEBLOCKS_SECRET_KEY!, - // @ts-expect-error hidden config - baseUrl: - process.env.LIVEBLOCKS_BASE_URL ?? - process.env.NEXT_PUBLIC_LIVEBLOCKS_BASE_URL ?? - "https://api.liveblocks.io", - }); - - expect( - await client.createRoom( - "node-package-e2e", - { defaultAccesses: ["room:write"] }, - { idempotent: true } - ) - ).toBeDefined(); +const client = new Liveblocks({ + secret: process.env.LIVEBLOCKS_SECRET_KEY!, + // @ts-expect-error hidden config + baseUrl: + process.env.LIVEBLOCKS_BASE_URL ?? + process.env.NEXT_PUBLIC_LIVEBLOCKS_BASE_URL ?? + "https://api.liveblocks.io", +}); + +async function createRandomTestRoom(): Promise { + const randomRoomId = `node-package-e2e-${Math.random().toString(36).substring(2, 15)}`; + + // Register cleanup + onTestFinished(async () => { + await client.deleteRoom(randomRoomId); }); + await client.createRoom( + randomRoomId, + { defaultAccesses: ["room:write"] }, + { idempotent: true } + ); + + return randomRoomId; +} + +describe("@liveblocks/node package e2e", () => { test("storage mutation should work in node environment", async () => { - const client = new Liveblocks({ - secret: process.env.LIVEBLOCKS_SECRET_KEY!, - // @ts-expect-error hidden config - baseUrl: - process.env.LIVEBLOCKS_BASE_URL ?? - process.env.NEXT_PUBLIC_LIVEBLOCKS_BASE_URL ?? - "https://api.liveblocks.io", - }); + const roomId = await createRandomTestRoom(); // delete existing data in the room await expect( - client.mutateStorage("node-package-e2e", ({ root }) => { + client.mutateStorage(roomId, ({ root }) => { root.delete("z"); }) ).resolves.toBeUndefined(); // add data to the room await expect( - client.mutateStorage("node-package-e2e", ({ root }) => { + client.mutateStorage(roomId, ({ root }) => { expect(root.toImmutable()).toEqual({}); // Mutate it! root.set("z", new LiveList([1, 2, 3])); @@ -53,7 +53,7 @@ describe("@liveblocks/node package e2e", () => { // add data to the room await expect( - client.mutateStorage("node-package-e2e", ({ root }) => { + client.mutateStorage(roomId, ({ root }) => { expect(root.toImmutable()).toEqual({ z: [1, 2, 3] }); }) ).resolves.toBeUndefined();