-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest_webhook_e2e.ts
More file actions
112 lines (99 loc) · 3.03 KB
/
Copy pathtest_webhook_e2e.ts
File metadata and controls
112 lines (99 loc) · 3.03 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
// deno-lint-ignore-file no-explicit-any
import { instantAppId } from "./protocol/src/clientApi.ts";
if (typeof globalThis.window === "undefined") {
(globalThis as any).window = {
location: { search: "" },
addEventListener: () => {},
removeEventListener: () => {},
setTimeout,
clearTimeout,
};
}
if (typeof (globalThis as any).indexedDB === "undefined") {
(globalThis as any).indexedDB = {
open: () => {
const req: any = {};
setTimeout(() => {
if (req.onsuccess) {
req.onsuccess({
target: {
result: {
transaction: () => ({
objectStore: () => ({
get: () => {
const r: any = {};
setTimeout(() => r.onsuccess?.(), 0);
return r;
},
put: () => {
const r: any = {};
setTimeout(() => r.onsuccess?.(), 0);
return r;
},
}),
}),
},
},
});
}
}, 0);
return req;
},
};
}
if (typeof navigator === "undefined" || !navigator.onLine) {
Object.defineProperty(globalThis, "navigator", {
value: { onLine: true },
writable: true,
});
}
import { init } from "@instantdb/core";
import { query as adminQuery } from "./backend/src/db.ts";
const db = init({ appId: instantAppId });
async function main() {
const { conversations } = await adminQuery({
conversations: { $: { order: { updatedAt: "desc" }, limit: 1 } },
});
if (!conversations.length) {
console.log("No conversations found");
return;
}
const conversationId = conversations[0].id;
const elementId = "webhook-test-" + Date.now();
console.log("Listening for streams on conversation:", conversationId);
const room = db.joinRoom("conversations", conversationId);
let receivedCount = 0;
room.subscribeTopic("stream", (event: any) => {
if (event.elementId === elementId) {
console.log("Received via webhook:", event.text);
receivedCount++;
if (receivedCount === 3) {
console.log("Success! All webhook chunks received via InstantDB.");
Deno.exit(0);
}
}
});
console.log("Subscribed. Waiting for connection...");
await new Promise((r) => setTimeout(r, 2000)); // give some time to connect
console.log("Triggering webhook to start streaming...");
const chunks = ["Hi", " from", " webhook!"];
let currentText = "";
for (const chunk of chunks) {
currentText += chunk;
const res = await fetch("http://localhost:8000/ui-update", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({
elementId,
conversationId,
type: "stream",
text: currentText,
active: true,
authorId: "test-bot",
}),
});
console.log("Webhook response:", await res.json());
await new Promise((r) => setTimeout(r, 500));
}
}
main();