@openclaw/tlon 2026.2.21 → 2026.5.2-beta.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/api.ts +16 -0
- package/channel-plugin-api.ts +1 -0
- package/doctor-contract-api.ts +1 -0
- package/index.ts +177 -11
- package/openclaw.plugin.json +194 -1
- package/package.json +57 -4
- package/runtime-api.ts +17 -0
- package/setup-api.ts +2 -0
- package/setup-entry.ts +9 -0
- package/src/account-fields.ts +9 -3
- package/src/channel.runtime.ts +248 -0
- package/src/channel.ts +153 -364
- package/src/config-schema.ts +22 -16
- package/src/core.test.ts +278 -0
- package/src/doctor-contract.ts +9 -0
- package/src/doctor.test.ts +40 -0
- package/src/doctor.ts +10 -0
- package/src/logger-runtime.ts +1 -0
- package/src/monitor/approval-runtime.ts +363 -0
- package/src/monitor/approval.test.ts +33 -0
- package/src/monitor/approval.ts +286 -0
- package/src/monitor/authorization.ts +30 -0
- package/src/monitor/cites.ts +54 -0
- package/src/monitor/discovery.ts +44 -54
- package/src/monitor/history.ts +155 -21
- package/src/monitor/index.ts +1290 -369
- package/src/monitor/media.test.ts +91 -0
- package/src/monitor/media.ts +172 -0
- package/src/monitor/processed-messages.test.ts +36 -1
- package/src/monitor/processed-messages.ts +62 -19
- package/src/monitor/settings-helpers.test.ts +113 -0
- package/src/monitor/settings-helpers.ts +158 -0
- package/src/monitor/utils.ts +311 -45
- package/src/runtime.ts +8 -13
- package/src/security.test.ts +622 -0
- package/src/session-route.ts +40 -0
- package/src/settings.ts +391 -0
- package/src/setup-core.ts +228 -0
- package/src/setup-surface.ts +103 -0
- package/src/targets.ts +29 -16
- package/src/tlon-api.test.ts +589 -0
- package/src/tlon-api.ts +402 -0
- package/src/types.ts +106 -45
- package/src/urbit/auth.ssrf.test.ts +3 -2
- package/src/urbit/auth.ts +2 -2
- package/src/urbit/base-url.test.ts +18 -11
- package/src/urbit/base-url.ts +9 -5
- package/src/urbit/channel-ops.ts +40 -59
- package/src/urbit/context.ts +13 -10
- package/src/urbit/errors.ts +2 -2
- package/src/urbit/fetch.ts +6 -3
- package/src/urbit/foreigns.ts +49 -0
- package/src/urbit/send.ts +53 -8
- package/src/urbit/sse-client.test.ts +184 -25
- package/src/urbit/sse-client.ts +123 -69
- package/src/urbit/story.ts +332 -0
- package/src/urbit/upload.test.ts +154 -0
- package/src/urbit/upload.ts +60 -0
- package/test-api.ts +1 -0
- package/tsconfig.json +16 -0
- package/src/config-schema.test.ts +0 -31
- package/src/onboarding.ts +0 -229
- package/src/urbit/channel-client.ts +0 -157
package/src/urbit/base-url.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { isBlockedHostnameOrIp } from "openclaw/plugin-sdk";
|
|
1
|
+
import { isBlockedHostnameOrIp } from "openclaw/plugin-sdk/ssrf-runtime";
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
type UrbitBaseUrlValidation =
|
|
4
4
|
| { ok: true; baseUrl: string; hostname: string }
|
|
5
5
|
| { ok: false; error: string };
|
|
6
6
|
|
|
@@ -8,8 +8,12 @@ function hasScheme(value: string): boolean {
|
|
|
8
8
|
return /^[a-zA-Z][a-zA-Z0-9+.-]*:\/\//.test(value);
|
|
9
9
|
}
|
|
10
10
|
|
|
11
|
+
export function normalizeUrbitHostname(hostname: string | undefined): string {
|
|
12
|
+
return (hostname ?? "").trim().toLowerCase().replace(/\.$/, "");
|
|
13
|
+
}
|
|
14
|
+
|
|
11
15
|
export function validateUrbitBaseUrl(raw: string): UrbitBaseUrlValidation {
|
|
12
|
-
const trimmed =
|
|
16
|
+
const trimmed = raw.trim();
|
|
13
17
|
if (!trimmed) {
|
|
14
18
|
return { ok: false, error: "Required" };
|
|
15
19
|
}
|
|
@@ -31,7 +35,7 @@ export function validateUrbitBaseUrl(raw: string): UrbitBaseUrlValidation {
|
|
|
31
35
|
return { ok: false, error: "URL must not include credentials" };
|
|
32
36
|
}
|
|
33
37
|
|
|
34
|
-
const hostname = parsed.hostname
|
|
38
|
+
const hostname = normalizeUrbitHostname(parsed.hostname);
|
|
35
39
|
if (!hostname) {
|
|
36
40
|
return { ok: false, error: "Invalid hostname" };
|
|
37
41
|
}
|
|
@@ -49,7 +53,7 @@ export function validateUrbitBaseUrl(raw: string): UrbitBaseUrlValidation {
|
|
|
49
53
|
}
|
|
50
54
|
|
|
51
55
|
export function isBlockedUrbitHostname(hostname: string): boolean {
|
|
52
|
-
const normalized = hostname
|
|
56
|
+
const normalized = normalizeUrbitHostname(hostname);
|
|
53
57
|
if (!normalized) {
|
|
54
58
|
return false;
|
|
55
59
|
}
|
package/src/urbit/channel-ops.ts
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import type { LookupFn, SsrFPolicy } from "openclaw/plugin-sdk";
|
|
1
|
+
import type { LookupFn, SsrFPolicy } from "openclaw/plugin-sdk/ssrf-runtime";
|
|
2
2
|
import { UrbitHttpError } from "./errors.js";
|
|
3
3
|
import { urbitFetch } from "./fetch.js";
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
type UrbitChannelDeps = {
|
|
6
6
|
baseUrl: string;
|
|
7
7
|
cookie: string;
|
|
8
8
|
ship: string;
|
|
@@ -12,21 +12,11 @@ export type UrbitChannelDeps = {
|
|
|
12
12
|
fetchImpl?: (input: RequestInfo | URL, init?: RequestInit) => Promise<Response>;
|
|
13
13
|
};
|
|
14
14
|
|
|
15
|
-
|
|
15
|
+
async function putUrbitChannel(
|
|
16
16
|
deps: UrbitChannelDeps,
|
|
17
|
-
params: {
|
|
18
|
-
)
|
|
19
|
-
|
|
20
|
-
const pokeData = {
|
|
21
|
-
id: pokeId,
|
|
22
|
-
action: "poke",
|
|
23
|
-
ship: deps.ship,
|
|
24
|
-
app: params.app,
|
|
25
|
-
mark: params.mark,
|
|
26
|
-
json: params.json,
|
|
27
|
-
};
|
|
28
|
-
|
|
29
|
-
const { response, release } = await urbitFetch({
|
|
17
|
+
params: { body: unknown; auditContext: string },
|
|
18
|
+
) {
|
|
19
|
+
return await urbitFetch({
|
|
30
20
|
baseUrl: deps.baseUrl,
|
|
31
21
|
path: `/~/channel/${deps.channelId}`,
|
|
32
22
|
init: {
|
|
@@ -35,7 +25,7 @@ export async function pokeUrbitChannel(
|
|
|
35
25
|
"Content-Type": "application/json",
|
|
36
26
|
Cookie: deps.cookie,
|
|
37
27
|
},
|
|
38
|
-
body: JSON.stringify(
|
|
28
|
+
body: JSON.stringify(params.body),
|
|
39
29
|
},
|
|
40
30
|
ssrfPolicy: deps.ssrfPolicy,
|
|
41
31
|
lookupFn: deps.lookupFn,
|
|
@@ -43,6 +33,26 @@ export async function pokeUrbitChannel(
|
|
|
43
33
|
timeoutMs: 30_000,
|
|
44
34
|
auditContext: params.auditContext,
|
|
45
35
|
});
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export async function pokeUrbitChannel(
|
|
39
|
+
deps: UrbitChannelDeps,
|
|
40
|
+
params: { app: string; mark: string; json: unknown; auditContext: string },
|
|
41
|
+
): Promise<number> {
|
|
42
|
+
const pokeId = Date.now();
|
|
43
|
+
const pokeData = {
|
|
44
|
+
id: pokeId,
|
|
45
|
+
action: "poke",
|
|
46
|
+
ship: deps.ship,
|
|
47
|
+
app: params.app,
|
|
48
|
+
mark: params.mark,
|
|
49
|
+
json: params.json,
|
|
50
|
+
};
|
|
51
|
+
|
|
52
|
+
const { response, release } = await putUrbitChannel(deps, {
|
|
53
|
+
body: [pokeData],
|
|
54
|
+
auditContext: params.auditContext,
|
|
55
|
+
});
|
|
46
56
|
|
|
47
57
|
try {
|
|
48
58
|
if (!response.ok && response.status !== 204) {
|
|
@@ -84,27 +94,11 @@ export async function scryUrbitPath(
|
|
|
84
94
|
}
|
|
85
95
|
}
|
|
86
96
|
|
|
87
|
-
|
|
97
|
+
async function createUrbitChannel(
|
|
88
98
|
deps: UrbitChannelDeps,
|
|
89
99
|
params: { body: unknown; auditContext: string },
|
|
90
100
|
): Promise<void> {
|
|
91
|
-
const { response, release } = await
|
|
92
|
-
baseUrl: deps.baseUrl,
|
|
93
|
-
path: `/~/channel/${deps.channelId}`,
|
|
94
|
-
init: {
|
|
95
|
-
method: "PUT",
|
|
96
|
-
headers: {
|
|
97
|
-
"Content-Type": "application/json",
|
|
98
|
-
Cookie: deps.cookie,
|
|
99
|
-
},
|
|
100
|
-
body: JSON.stringify(params.body),
|
|
101
|
-
},
|
|
102
|
-
ssrfPolicy: deps.ssrfPolicy,
|
|
103
|
-
lookupFn: deps.lookupFn,
|
|
104
|
-
fetchImpl: deps.fetchImpl,
|
|
105
|
-
timeoutMs: 30_000,
|
|
106
|
-
auditContext: params.auditContext,
|
|
107
|
-
});
|
|
101
|
+
const { response, release } = await putUrbitChannel(deps, params);
|
|
108
102
|
|
|
109
103
|
try {
|
|
110
104
|
if (!response.ok && response.status !== 204) {
|
|
@@ -115,31 +109,18 @@ export async function createUrbitChannel(
|
|
|
115
109
|
}
|
|
116
110
|
}
|
|
117
111
|
|
|
118
|
-
|
|
119
|
-
const { response, release } = await
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
112
|
+
async function wakeUrbitChannel(deps: UrbitChannelDeps): Promise<void> {
|
|
113
|
+
const { response, release } = await putUrbitChannel(deps, {
|
|
114
|
+
body: [
|
|
115
|
+
{
|
|
116
|
+
id: Date.now(),
|
|
117
|
+
action: "poke",
|
|
118
|
+
ship: deps.ship,
|
|
119
|
+
app: "hood",
|
|
120
|
+
mark: "helm-hi",
|
|
121
|
+
json: "Opening API channel",
|
|
127
122
|
},
|
|
128
|
-
|
|
129
|
-
{
|
|
130
|
-
id: Date.now(),
|
|
131
|
-
action: "poke",
|
|
132
|
-
ship: deps.ship,
|
|
133
|
-
app: "hood",
|
|
134
|
-
mark: "helm-hi",
|
|
135
|
-
json: "Opening API channel",
|
|
136
|
-
},
|
|
137
|
-
]),
|
|
138
|
-
},
|
|
139
|
-
ssrfPolicy: deps.ssrfPolicy,
|
|
140
|
-
lookupFn: deps.lookupFn,
|
|
141
|
-
fetchImpl: deps.fetchImpl,
|
|
142
|
-
timeoutMs: 30_000,
|
|
123
|
+
],
|
|
143
124
|
auditContext: "tlon-urbit-channel-wake",
|
|
144
125
|
});
|
|
145
126
|
|
package/src/urbit/context.ts
CHANGED
|
@@ -1,15 +1,15 @@
|
|
|
1
|
-
|
|
2
|
-
import { validateUrbitBaseUrl } from "./base-url.js";
|
|
1
|
+
export { ssrfPolicyFromDangerouslyAllowPrivateNetwork } from "openclaw/plugin-sdk/ssrf-runtime";
|
|
2
|
+
import { normalizeUrbitHostname, validateUrbitBaseUrl } from "./base-url.js";
|
|
3
3
|
import { UrbitUrlError } from "./errors.js";
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
type UrbitContext = {
|
|
6
6
|
baseUrl: string;
|
|
7
7
|
hostname: string;
|
|
8
8
|
ship: string;
|
|
9
9
|
};
|
|
10
10
|
|
|
11
|
-
|
|
12
|
-
const trimmed = hostname
|
|
11
|
+
function resolveShipFromHostname(hostname: string): string {
|
|
12
|
+
const trimmed = normalizeUrbitHostname(hostname);
|
|
13
13
|
if (!trimmed) {
|
|
14
14
|
return "";
|
|
15
15
|
}
|
|
@@ -19,7 +19,7 @@ export function resolveShipFromHostname(hostname: string): string {
|
|
|
19
19
|
return trimmed;
|
|
20
20
|
}
|
|
21
21
|
|
|
22
|
-
|
|
22
|
+
function normalizeUrbitShip(ship: string | undefined, hostname: string): string {
|
|
23
23
|
const raw = ship?.replace(/^~/, "") ?? resolveShipFromHostname(hostname);
|
|
24
24
|
return raw.trim();
|
|
25
25
|
}
|
|
@@ -40,8 +40,11 @@ export function getUrbitContext(url: string, ship?: string): UrbitContext {
|
|
|
40
40
|
};
|
|
41
41
|
}
|
|
42
42
|
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
43
|
+
/**
|
|
44
|
+
* Get the default SSRF policy for image uploads.
|
|
45
|
+
* Uses a restrictive policy that blocks private networks by default.
|
|
46
|
+
*/
|
|
47
|
+
export function getDefaultSsrFPolicy(): undefined {
|
|
48
|
+
// Default: block private networks for image uploads (safer default)
|
|
49
|
+
return undefined;
|
|
47
50
|
}
|
package/src/urbit/errors.ts
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
|
-
|
|
1
|
+
type UrbitErrorCode =
|
|
2
2
|
| "invalid_url"
|
|
3
3
|
| "http_error"
|
|
4
4
|
| "auth_failed"
|
|
5
5
|
| "missing_cookie"
|
|
6
6
|
| "channel_not_open";
|
|
7
7
|
|
|
8
|
-
|
|
8
|
+
class UrbitError extends Error {
|
|
9
9
|
readonly code: UrbitErrorCode;
|
|
10
10
|
|
|
11
11
|
constructor(code: UrbitErrorCode, message: string, options?: { cause?: unknown }) {
|
package/src/urbit/fetch.ts
CHANGED
|
@@ -1,9 +1,12 @@
|
|
|
1
|
-
import
|
|
2
|
-
|
|
1
|
+
import {
|
|
2
|
+
fetchWithSsrFGuard,
|
|
3
|
+
type LookupFn,
|
|
4
|
+
type SsrFPolicy,
|
|
5
|
+
} from "openclaw/plugin-sdk/ssrf-runtime";
|
|
3
6
|
import { validateUrbitBaseUrl } from "./base-url.js";
|
|
4
7
|
import { UrbitUrlError } from "./errors.js";
|
|
5
8
|
|
|
6
|
-
|
|
9
|
+
type UrbitFetchOptions = {
|
|
7
10
|
baseUrl: string;
|
|
8
11
|
path: string;
|
|
9
12
|
init?: RequestInit;
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Types for Urbit groups foreigns (group invites)
|
|
3
|
+
* Based on packages/shared/src/urbit/groups.ts from homestead
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
interface GroupPreviewV7 {
|
|
7
|
+
meta: {
|
|
8
|
+
title: string;
|
|
9
|
+
description: string;
|
|
10
|
+
image: string;
|
|
11
|
+
cover: string;
|
|
12
|
+
};
|
|
13
|
+
"channel-count": number;
|
|
14
|
+
"member-count": number;
|
|
15
|
+
admissions: {
|
|
16
|
+
privacy: "public" | "private" | "secret";
|
|
17
|
+
};
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
interface ForeignInvite {
|
|
21
|
+
flag: string; // group flag e.g. "~host/group-name"
|
|
22
|
+
time: number; // timestamp
|
|
23
|
+
from: string; // ship that sent invite
|
|
24
|
+
token: string | null;
|
|
25
|
+
note: string | null;
|
|
26
|
+
preview: GroupPreviewV7;
|
|
27
|
+
valid: boolean; // tracks if invite has been revoked
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
type Lookup = "preview" | "done" | "error";
|
|
31
|
+
type Progress = "ask" | "join" | "watch" | "done" | "error";
|
|
32
|
+
|
|
33
|
+
interface Foreign {
|
|
34
|
+
invites: ForeignInvite[];
|
|
35
|
+
lookup: Lookup | null;
|
|
36
|
+
preview: GroupPreviewV7 | null;
|
|
37
|
+
progress: Progress | null;
|
|
38
|
+
token: string | null;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export interface Foreigns {
|
|
42
|
+
[flag: string]: Foreign;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
// DM invite structure from chat /v3 firehose
|
|
46
|
+
export interface DmInvite {
|
|
47
|
+
ship: string;
|
|
48
|
+
// Additional fields may be present
|
|
49
|
+
}
|
package/src/urbit/send.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { scot, da } from "@urbit/aura";
|
|
2
|
+
import { markdownToStory, createImageBlock, isImageUrl, type Story } from "./story.js";
|
|
2
3
|
|
|
3
4
|
export type TlonPokeApi = {
|
|
4
5
|
poke: (params: { app: string; mark: string; json: unknown }) => Promise<unknown>;
|
|
@@ -11,8 +12,19 @@ type SendTextParams = {
|
|
|
11
12
|
text: string;
|
|
12
13
|
};
|
|
13
14
|
|
|
15
|
+
type SendStoryParams = {
|
|
16
|
+
api: TlonPokeApi;
|
|
17
|
+
fromShip: string;
|
|
18
|
+
toShip: string;
|
|
19
|
+
story: Story;
|
|
20
|
+
};
|
|
21
|
+
|
|
14
22
|
export async function sendDm({ api, fromShip, toShip, text }: SendTextParams) {
|
|
15
|
-
const story =
|
|
23
|
+
const story: Story = markdownToStory(text);
|
|
24
|
+
return sendDmWithStory({ api, fromShip, toShip, story });
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export async function sendDmWithStory({ api, fromShip, toShip, story }: SendStoryParams) {
|
|
16
28
|
const sentAt = Date.now();
|
|
17
29
|
const idUd = scot("ud", da.fromUnix(sentAt));
|
|
18
30
|
const id = `${fromShip}/${idUd}`;
|
|
@@ -52,6 +64,15 @@ type SendGroupParams = {
|
|
|
52
64
|
replyToId?: string | null;
|
|
53
65
|
};
|
|
54
66
|
|
|
67
|
+
type SendGroupStoryParams = {
|
|
68
|
+
api: TlonPokeApi;
|
|
69
|
+
fromShip: string;
|
|
70
|
+
hostShip: string;
|
|
71
|
+
channelName: string;
|
|
72
|
+
story: Story;
|
|
73
|
+
replyToId?: string | null;
|
|
74
|
+
};
|
|
75
|
+
|
|
55
76
|
export async function sendGroupMessage({
|
|
56
77
|
api,
|
|
57
78
|
fromShip,
|
|
@@ -60,13 +81,25 @@ export async function sendGroupMessage({
|
|
|
60
81
|
text,
|
|
61
82
|
replyToId,
|
|
62
83
|
}: SendGroupParams) {
|
|
63
|
-
const story =
|
|
84
|
+
const story: Story = markdownToStory(text);
|
|
85
|
+
return sendGroupMessageWithStory({ api, fromShip, hostShip, channelName, story, replyToId });
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
export async function sendGroupMessageWithStory({
|
|
89
|
+
api,
|
|
90
|
+
fromShip,
|
|
91
|
+
hostShip,
|
|
92
|
+
channelName,
|
|
93
|
+
story,
|
|
94
|
+
replyToId,
|
|
95
|
+
}: SendGroupStoryParams) {
|
|
64
96
|
const sentAt = Date.now();
|
|
65
97
|
|
|
66
98
|
// Format reply ID as @ud (with dots) - required for Tlon to recognize thread replies
|
|
67
99
|
let formattedReplyId = replyToId;
|
|
68
100
|
if (replyToId && /^\d+$/.test(replyToId)) {
|
|
69
101
|
try {
|
|
102
|
+
// scot('ud', n) formats a number as @ud with dots
|
|
70
103
|
formattedReplyId = scot("ud", BigInt(replyToId));
|
|
71
104
|
} catch {
|
|
72
105
|
// Fall back to raw ID if formatting fails
|
|
@@ -118,14 +151,26 @@ export async function sendGroupMessage({
|
|
|
118
151
|
return { channel: "tlon", messageId: `${fromShip}/${sentAt}` };
|
|
119
152
|
}
|
|
120
153
|
|
|
121
|
-
|
|
154
|
+
/**
|
|
155
|
+
* Build a story with text and optional media (image)
|
|
156
|
+
*/
|
|
157
|
+
export function buildMediaStory(text: string | undefined, mediaUrl: string | undefined): Story {
|
|
158
|
+
const story: Story = [];
|
|
122
159
|
const cleanText = text?.trim() ?? "";
|
|
123
160
|
const cleanUrl = mediaUrl?.trim() ?? "";
|
|
124
|
-
|
|
125
|
-
|
|
161
|
+
|
|
162
|
+
// Add text content if present
|
|
163
|
+
if (cleanText) {
|
|
164
|
+
story.push(...markdownToStory(cleanText));
|
|
126
165
|
}
|
|
127
|
-
|
|
128
|
-
|
|
166
|
+
|
|
167
|
+
// Add image block if URL looks like an image
|
|
168
|
+
if (cleanUrl && isImageUrl(cleanUrl)) {
|
|
169
|
+
story.push(createImageBlock(cleanUrl, ""));
|
|
170
|
+
} else if (cleanUrl) {
|
|
171
|
+
// For non-image URLs, add as a link
|
|
172
|
+
story.push({ inline: [{ link: { href: cleanUrl, content: cleanUrl } }] });
|
|
129
173
|
}
|
|
130
|
-
|
|
174
|
+
|
|
175
|
+
return story.length > 0 ? story : [{ inline: [""] }];
|
|
131
176
|
}
|
|
@@ -1,44 +1,203 @@
|
|
|
1
|
-
import type { LookupFn } from "openclaw/plugin-sdk";
|
|
2
1
|
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
|
2
|
+
import { urbitFetch } from "./fetch.js";
|
|
3
3
|
import { UrbitSSEClient } from "./sse-client.js";
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
// Mock urbitFetch to avoid real network calls
|
|
6
|
+
vi.mock("./fetch.js", () => ({
|
|
7
|
+
urbitFetch: vi.fn(),
|
|
8
|
+
}));
|
|
9
|
+
|
|
10
|
+
// Mock channel-ops to avoid real channel operations
|
|
11
|
+
vi.mock("./channel-ops.js", () => ({
|
|
12
|
+
ensureUrbitChannelOpen: vi.fn().mockResolvedValue(undefined),
|
|
13
|
+
pokeUrbitChannel: vi.fn().mockResolvedValue(undefined),
|
|
14
|
+
scryUrbitPath: vi.fn().mockResolvedValue({}),
|
|
15
|
+
}));
|
|
6
16
|
|
|
7
17
|
describe("UrbitSSEClient", () => {
|
|
8
18
|
beforeEach(() => {
|
|
9
|
-
vi.
|
|
10
|
-
mockFetch.mockReset();
|
|
19
|
+
vi.clearAllMocks();
|
|
11
20
|
});
|
|
12
21
|
|
|
13
22
|
afterEach(() => {
|
|
14
|
-
vi.
|
|
23
|
+
vi.restoreAllMocks();
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
describe("subscribe", () => {
|
|
27
|
+
it("sends subscriptions added after connect", async () => {
|
|
28
|
+
const mockUrbitFetch = vi.mocked(urbitFetch);
|
|
29
|
+
mockUrbitFetch.mockResolvedValue({
|
|
30
|
+
response: { ok: true, status: 200 } as unknown as Response,
|
|
31
|
+
finalUrl: "https://example.com",
|
|
32
|
+
release: vi.fn().mockResolvedValue(undefined),
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
const client = new UrbitSSEClient("https://example.com", "urbauth-~zod=123");
|
|
36
|
+
// Simulate connected state
|
|
37
|
+
(client as { isConnected: boolean }).isConnected = true;
|
|
38
|
+
|
|
39
|
+
await client.subscribe({
|
|
40
|
+
app: "chat",
|
|
41
|
+
path: "/dm/~zod",
|
|
42
|
+
event: () => {},
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
expect(mockUrbitFetch).toHaveBeenCalledTimes(1);
|
|
46
|
+
const callArgs = mockUrbitFetch.mock.calls[0][0];
|
|
47
|
+
expect(callArgs.path).toContain("/~/channel/");
|
|
48
|
+
expect(callArgs.init?.method).toBe("PUT");
|
|
49
|
+
|
|
50
|
+
const body = JSON.parse(callArgs.init?.body as string);
|
|
51
|
+
expect(body).toHaveLength(1);
|
|
52
|
+
expect(body[0]).toMatchObject({
|
|
53
|
+
action: "subscribe",
|
|
54
|
+
app: "chat",
|
|
55
|
+
path: "/dm/~zod",
|
|
56
|
+
});
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
it("queues subscriptions before connect", async () => {
|
|
60
|
+
const mockUrbitFetch = vi.mocked(urbitFetch);
|
|
61
|
+
|
|
62
|
+
const client = new UrbitSSEClient("https://example.com", "urbauth-~zod=123");
|
|
63
|
+
// Not connected yet
|
|
64
|
+
|
|
65
|
+
await client.subscribe({
|
|
66
|
+
app: "chat",
|
|
67
|
+
path: "/dm/~zod",
|
|
68
|
+
event: () => {},
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
// Should not call urbitFetch since not connected
|
|
72
|
+
expect(mockUrbitFetch).not.toHaveBeenCalled();
|
|
73
|
+
// But subscription should be queued
|
|
74
|
+
expect(client.subscriptions).toHaveLength(1);
|
|
75
|
+
expect(client.subscriptions[0]).toMatchObject({
|
|
76
|
+
app: "chat",
|
|
77
|
+
path: "/dm/~zod",
|
|
78
|
+
});
|
|
79
|
+
});
|
|
80
|
+
});
|
|
81
|
+
|
|
82
|
+
describe("updateCookie", () => {
|
|
83
|
+
it("normalizes cookie when updating", () => {
|
|
84
|
+
const client = new UrbitSSEClient("https://example.com", "urbauth-~zod=123");
|
|
85
|
+
|
|
86
|
+
// Cookie with extra parts that should be stripped
|
|
87
|
+
client.updateCookie("urbauth-~zod=456; Path=/; HttpOnly");
|
|
88
|
+
|
|
89
|
+
expect(client.cookie).toBe("urbauth-~zod=456");
|
|
90
|
+
});
|
|
91
|
+
|
|
92
|
+
it("handles simple cookie values", () => {
|
|
93
|
+
const client = new UrbitSSEClient("https://example.com", "urbauth-~zod=123");
|
|
94
|
+
|
|
95
|
+
client.updateCookie("urbauth-~zod=newvalue");
|
|
96
|
+
|
|
97
|
+
expect(client.cookie).toBe("urbauth-~zod=newvalue");
|
|
98
|
+
});
|
|
99
|
+
});
|
|
100
|
+
|
|
101
|
+
describe("reconnection", () => {
|
|
102
|
+
it("has autoReconnect enabled by default", () => {
|
|
103
|
+
const client = new UrbitSSEClient("https://example.com", "urbauth-~zod=123");
|
|
104
|
+
expect(client.autoReconnect).toBe(true);
|
|
105
|
+
});
|
|
106
|
+
|
|
107
|
+
it("can disable autoReconnect via options", () => {
|
|
108
|
+
const client = new UrbitSSEClient("https://example.com", "urbauth-~zod=123", {
|
|
109
|
+
autoReconnect: false,
|
|
110
|
+
});
|
|
111
|
+
expect(client.autoReconnect).toBe(false);
|
|
112
|
+
});
|
|
113
|
+
|
|
114
|
+
it("stores onReconnect callback", () => {
|
|
115
|
+
const onReconnect = vi.fn();
|
|
116
|
+
const client = new UrbitSSEClient("https://example.com", "urbauth-~zod=123", {
|
|
117
|
+
onReconnect,
|
|
118
|
+
});
|
|
119
|
+
expect(client.onReconnect).toBe(onReconnect);
|
|
120
|
+
});
|
|
121
|
+
|
|
122
|
+
it("resets reconnect attempts on successful connect", async () => {
|
|
123
|
+
const mockUrbitFetch = vi.mocked(urbitFetch);
|
|
124
|
+
|
|
125
|
+
// Mock a response that returns a readable stream
|
|
126
|
+
const mockStream = new ReadableStream({
|
|
127
|
+
start(controller) {
|
|
128
|
+
controller.close();
|
|
129
|
+
},
|
|
130
|
+
});
|
|
131
|
+
|
|
132
|
+
mockUrbitFetch.mockResolvedValue({
|
|
133
|
+
response: {
|
|
134
|
+
ok: true,
|
|
135
|
+
status: 200,
|
|
136
|
+
body: mockStream,
|
|
137
|
+
} as unknown as Response,
|
|
138
|
+
finalUrl: "https://example.com",
|
|
139
|
+
release: vi.fn().mockResolvedValue(undefined),
|
|
140
|
+
});
|
|
141
|
+
|
|
142
|
+
const client = new UrbitSSEClient("https://example.com", "urbauth-~zod=123", {
|
|
143
|
+
autoReconnect: false, // Disable to prevent reconnect loop
|
|
144
|
+
});
|
|
145
|
+
client.reconnectAttempts = 5;
|
|
146
|
+
|
|
147
|
+
await client.connect();
|
|
148
|
+
|
|
149
|
+
expect(client.reconnectAttempts).toBe(0);
|
|
150
|
+
});
|
|
15
151
|
});
|
|
16
152
|
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
153
|
+
describe("event acking", () => {
|
|
154
|
+
it("tracks lastHeardEventId and ackThreshold", () => {
|
|
155
|
+
const client = new UrbitSSEClient("https://example.com", "urbauth-~zod=123");
|
|
156
|
+
|
|
157
|
+
// Access private properties for testing
|
|
158
|
+
const lastHeardEventId = (client as unknown as { lastHeardEventId: number }).lastHeardEventId;
|
|
159
|
+
const ackThreshold = (client as unknown as { ackThreshold: number }).ackThreshold;
|
|
20
160
|
|
|
21
|
-
|
|
22
|
-
|
|
161
|
+
expect(lastHeardEventId).toBe(-1);
|
|
162
|
+
expect(ackThreshold).toBeGreaterThan(0);
|
|
23
163
|
});
|
|
24
|
-
|
|
164
|
+
});
|
|
25
165
|
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
166
|
+
describe("constructor", () => {
|
|
167
|
+
it("generates unique channel ID", () => {
|
|
168
|
+
const client1 = new UrbitSSEClient("https://example.com", "urbauth-~zod=123");
|
|
169
|
+
const client2 = new UrbitSSEClient("https://example.com", "urbauth-~zod=123");
|
|
170
|
+
|
|
171
|
+
expect(client1.channelId).not.toBe(client2.channelId);
|
|
30
172
|
});
|
|
31
173
|
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
174
|
+
it("normalizes cookie in constructor", () => {
|
|
175
|
+
const client = new UrbitSSEClient(
|
|
176
|
+
"https://example.com",
|
|
177
|
+
"urbauth-~zod=123; Path=/; HttpOnly",
|
|
178
|
+
);
|
|
179
|
+
|
|
180
|
+
expect(client.cookie).toBe("urbauth-~zod=123");
|
|
181
|
+
});
|
|
182
|
+
|
|
183
|
+
it("sets default reconnection parameters", () => {
|
|
184
|
+
const client = new UrbitSSEClient("https://example.com", "urbauth-~zod=123");
|
|
185
|
+
|
|
186
|
+
expect(client.maxReconnectAttempts).toBe(10);
|
|
187
|
+
expect(client.reconnectDelay).toBe(1000);
|
|
188
|
+
expect(client.maxReconnectDelay).toBe(30000);
|
|
189
|
+
});
|
|
190
|
+
|
|
191
|
+
it("allows overriding reconnection parameters", () => {
|
|
192
|
+
const client = new UrbitSSEClient("https://example.com", "urbauth-~zod=123", {
|
|
193
|
+
maxReconnectAttempts: 5,
|
|
194
|
+
reconnectDelay: 500,
|
|
195
|
+
maxReconnectDelay: 10000,
|
|
196
|
+
});
|
|
197
|
+
|
|
198
|
+
expect(client.maxReconnectAttempts).toBe(5);
|
|
199
|
+
expect(client.reconnectDelay).toBe(500);
|
|
200
|
+
expect(client.maxReconnectDelay).toBe(10000);
|
|
42
201
|
});
|
|
43
202
|
});
|
|
44
203
|
});
|