Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 35 additions & 18 deletions e2e/node-sandbox/test/client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<JsonObject, BaseUserMeta>;

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<string> {
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<void> {
return new Promise((resolve) => setTimeout(resolve, ms));
Expand Down Expand Up @@ -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:
Expand All @@ -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" },
});

Expand All @@ -88,13 +101,17 @@ 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;
}
});

roomB.subscribe("others", (others) => {
if (others.length === 0) return; // Ignore [] case

callbackBCalled = true;
if (others.some((user: OpaqueUser) => user.presence?.name === "A")) {
roomBSawA = true;
Expand Down
60 changes: 30 additions & 30 deletions e2e/node-sandbox/test/node.test.ts
Original file line number Diff line number Diff line change
@@ -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<string> {
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]));
Expand All @@ -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();
Expand Down
Loading