-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtestPubSub.js
More file actions
147 lines (126 loc) · 4.32 KB
/
testPubSub.js
File metadata and controls
147 lines (126 loc) · 4.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
import BosBase from "bosbase";
import * as websocket from "ws";
const baseUrl = process.env.BOSBASE_BASE_URL ?? "http://127.0.0.1:8080";
const authEmail =
process.env.BOSBASE_EMAIL ??
process.env.BOSBASE_SUPERUSER_EMAIL ??
"try@bosbase.com";
const authPassword =
process.env.BOSBASE_PASSWORD ??
process.env.BOSBASE_SUPERUSER_PASSWORD ??
"bosbasepass";
const WebSocket = websocket.WebSocket ?? websocket.default ?? websocket;
global.WebSocket = WebSocket;
function waitForMessage(buffer, label, timeoutMs = 8000) {
return new Promise((resolve, reject) => {
const timer = setTimeout(
() => reject(new Error(`${label} timed out after ${timeoutMs}ms`)),
timeoutMs
);
const poll = () => {
if (buffer.length > 0) {
clearTimeout(timer);
resolve(buffer.shift());
return;
}
setTimeout(poll, 50);
};
poll();
});
}
async function runPublishSubscribe(pb) {
console.log("\n[INFO] Test 1: publish/subscribe roundtrip...");
const topic = `test/pubsub/basic-${Date.now()}`;
const messages = [];
console.log(`[INFO] Subscribing to topic "${topic}"...`);
const unsubscribe = await pb.pubsub.subscribe(topic, (msg) => {
messages.push(msg);
});
try {
const payload = { text: "hello from pubsub", ts: Date.now() };
console.log("[INFO] Publishing basic message...");
const ack = await pb.pubsub.publish(topic, payload);
if (!ack?.id || ack.topic !== topic) {
throw new Error("Publish ack is missing expected fields");
}
console.log("[SUCCESS] Publish ack:", ack);
const received = await waitForMessage(
messages,
"basic pubsub message",
8000
);
if (!received?.data || received.data.text !== payload.text) {
throw new Error("Received pubsub message payload does not match");
}
console.log("[SUCCESS] Received pubsub message:", received);
} finally {
await unsubscribe();
console.log(`[INFO] Unsubscribed from "${topic}"`);
}
}
async function runRealtimeHelpers(pb) {
console.log("\n[INFO] Test 2: realtimeSubscribe/realtimePublish helpers...");
const topic = `test/pubsub/realtime-${Date.now()}`;
const realtimeMessages = [];
console.log(`[INFO] realtimeSubscribe on topic "${topic}"...`);
const unsubscribe = await pb.pubsub.realtimeSubscribe(topic, (msg) => {
realtimeMessages.push(msg);
});
try {
const ref = `ref-${Date.now()}`;
const payload = { user: "cli-runner", action: "join" };
console.log("[INFO] realtimePublish event \"join\"...");
const ack = await pb.pubsub.realtimePublish(topic, "join", payload, ref);
if (!ack?.id || ack.topic !== topic) {
throw new Error("Realtime publish ack is missing expected fields");
}
console.log("[SUCCESS] Realtime publish ack:", ack);
const received = await waitForMessage(
realtimeMessages,
"realtime pubsub message",
8000
);
if (received?.event !== "join") {
throw new Error("Realtime message event did not match \"join\"");
}
if (received?.ref !== ref) {
throw new Error("Realtime message ref did not match");
}
if (received?.payload?.action !== payload.action) {
throw new Error("Realtime message payload did not match");
}
console.log("[SUCCESS] Received realtime message:", received);
} finally {
await unsubscribe();
console.log(`[INFO] Unsubscribed from "${topic}"`);
}
}
async function main() {
try {
console.log("[INFO] Starting Pub/Sub tests...");
console.log(`[INFO] Base URL: ${baseUrl}`);
const pb = new BosBase(baseUrl);
console.log("[INFO] Authenticating as superuser...");
await pb
.collection("_superusers")
.authWithPassword(authEmail, authPassword);
console.log("[SUCCESS] Authenticated");
await runPublishSubscribe(pb);
await runRealtimeHelpers(pb);
await pb.pubsub.disconnect();
console.log("\n========== Pub/Sub tests completed ==========");
} catch (error) {
console.error("[ERROR] Pub/Sub test failed:");
if (error?.response) {
console.error("Status:", error.response.status);
console.error("Data:", JSON.stringify(error.response.data, null, 2));
if (error.response.data?.message) {
console.error("Message:", error.response.data.message);
}
} else {
console.error(error);
}
process.exit(1);
}
}
main();