@privateclaw/privateclaw 0.1.12 → 0.1.14

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.
@@ -250,6 +250,127 @@ test("privateclaw command embeds qqimg tags for QQ channel replies", async (t) =
250
250
  assert.deepEqual(qrPng.subarray(0, PNG_SIGNATURE.length), PNG_SIGNATURE);
251
251
  });
252
252
 
253
+ test("privateclaw command can override the relay per invocation", async (t) => {
254
+ const defaultRelay = createRelayServer({
255
+ host: "127.0.0.1",
256
+ port: 0,
257
+ sessionTtlMs: 60_000,
258
+ frameCacheSize: 8,
259
+ });
260
+ const overrideRelay = createRelayServer({
261
+ host: "127.0.0.1",
262
+ port: 0,
263
+ sessionTtlMs: 60_000,
264
+ frameCacheSize: 8,
265
+ });
266
+ const [{ port: defaultPort }, { port: overridePort }] = await Promise.all([
267
+ defaultRelay.start(),
268
+ overrideRelay.start(),
269
+ ]);
270
+
271
+ const stateDir = await mkdtemp(path.join(os.tmpdir(), "privateclaw-plugin-state-"));
272
+ t.after(async () => {
273
+ await rm(stateDir, { recursive: true, force: true });
274
+ await Promise.all([defaultRelay.stop(), overrideRelay.stop()]);
275
+ });
276
+
277
+ const plugin = createOpenClawCompatiblePlugin({
278
+ providerWsUrl: `ws://127.0.0.1:${defaultPort}/ws/provider`,
279
+ appWsUrl: `ws://127.0.0.1:${defaultPort}/ws/app`,
280
+ bridge: new EchoBridge("OpenClaw bridge"),
281
+ welcomeMessage: "Welcome to PrivateClaw",
282
+ });
283
+ const { api, getCli, getCommand, getService } = createMockApi();
284
+ plugin.register(api);
285
+
286
+ const service = getService();
287
+ await service.start({
288
+ config: {},
289
+ stateDir,
290
+ logger: api.logger,
291
+ });
292
+ t.after(async () => {
293
+ await service.stop?.({
294
+ config: {},
295
+ stateDir,
296
+ logger: api.logger,
297
+ });
298
+ });
299
+
300
+ const command = getCommand();
301
+ const defaultReply = await command.handler({
302
+ channel: "telegram",
303
+ senderId: "tester",
304
+ isAuthorizedSender: true,
305
+ commandBody: "/privateclaw",
306
+ config: {},
307
+ });
308
+ const defaultInviteUri =
309
+ defaultReply.text?.match(/privateclaw:\/\/connect\?payload=\S+/)?.[0];
310
+ assert.ok(defaultInviteUri, "default command should print a PrivateClaw invite URI");
311
+ const defaultInvite = decodeInviteString(defaultInviteUri);
312
+ assert.match(defaultInvite.appWsUrl, new RegExp(`:${defaultPort}/ws/app`));
313
+
314
+ const overrideReply = await command.handler({
315
+ channel: "telegram",
316
+ senderId: "tester",
317
+ isAuthorizedSender: true,
318
+ commandBody: `/privateclaw relay=ws://127.0.0.1:${overridePort}`,
319
+ args: `relay=ws://127.0.0.1:${overridePort}`,
320
+ config: {},
321
+ });
322
+ const overrideInviteUri =
323
+ overrideReply.text?.match(/privateclaw:\/\/connect\?payload=\S+/)?.[0];
324
+ assert.ok(
325
+ overrideInviteUri,
326
+ "override command should print a PrivateClaw invite URI",
327
+ );
328
+ const overrideInvite = decodeInviteString(overrideInviteUri);
329
+ assert.match(overrideInvite.appWsUrl, new RegExp(`:${overridePort}/ws/app`));
330
+ assert.equal(overrideInvite.relayLabel, `127.0.0.1:${overridePort}`);
331
+
332
+ const cli = getCli();
333
+ const root = new FakeCliCommand("root");
334
+ await cli.registrar({
335
+ program: root,
336
+ config: {},
337
+ logger: api.logger,
338
+ });
339
+ const sessions = root.children.get("privateclaw")?.children.get("sessions");
340
+ assert.ok(
341
+ sessions?.actionHandler,
342
+ "plugin should register the privateclaw sessions subcommand",
343
+ );
344
+ assert.ok(
345
+ sessions?.children.get("qr")?.actionHandler,
346
+ "plugin should register the privateclaw sessions qr subcommand",
347
+ );
348
+ assert.ok(
349
+ sessions?.children.get("kill")?.actionHandler,
350
+ "plugin should register the privateclaw sessions kill subcommand",
351
+ );
352
+
353
+ const printed: string[] = [];
354
+ const originalLog = console.log;
355
+ console.log = (message?: unknown, ...optionalParams: unknown[]) => {
356
+ printed.push([message, ...optionalParams].map(String).join(" "));
357
+ };
358
+ t.after(() => {
359
+ console.log = originalLog;
360
+ });
361
+
362
+ await sessions.actionHandler?.();
363
+
364
+ assert.ok(
365
+ printed.some((line) => line.includes(defaultInvite.sessionId)),
366
+ "sessions should include the default-relay session",
367
+ );
368
+ assert.ok(
369
+ printed.some((line) => line.includes(overrideInvite.sessionId)),
370
+ "sessions should include the override-relay session",
371
+ );
372
+ });
373
+
253
374
  test("privateclaw plugin local pair CLI defaults to the long session TTL", async (t) => {
254
375
  const relay = createRelayServer({
255
376
  host: "127.0.0.1",
@@ -347,6 +468,85 @@ test("privateclaw plugin local pair CLI defaults to the long session TTL", async
347
468
  assert.deepEqual(qrPng.subarray(0, PNG_SIGNATURE.length), PNG_SIGNATURE);
348
469
  });
349
470
 
471
+ test("privateclaw plugin local pair CLI can override the relay per command", async (t) => {
472
+ const defaultRelay = createRelayServer({
473
+ host: "127.0.0.1",
474
+ port: 0,
475
+ sessionTtlMs: 60_000,
476
+ frameCacheSize: 8,
477
+ });
478
+ const overrideRelay = createRelayServer({
479
+ host: "127.0.0.1",
480
+ port: 0,
481
+ sessionTtlMs: 60_000,
482
+ frameCacheSize: 8,
483
+ });
484
+ const [{ port: defaultPort }, { port: overridePort }] = await Promise.all([
485
+ defaultRelay.start(),
486
+ overrideRelay.start(),
487
+ ]);
488
+
489
+ t.after(async () => {
490
+ await Promise.all([defaultRelay.stop(), overrideRelay.stop()]);
491
+ });
492
+
493
+ const plugin = createOpenClawCompatiblePlugin({
494
+ providerWsUrl: `ws://127.0.0.1:${defaultPort}/ws/provider`,
495
+ appWsUrl: `ws://127.0.0.1:${defaultPort}/ws/app`,
496
+ bridge: new EchoBridge("OpenClaw bridge"),
497
+ });
498
+ const { api, getCli } = createMockApi();
499
+ plugin.register(api);
500
+
501
+ const cli = getCli();
502
+ const root = new FakeCliCommand("root");
503
+ await cli.registrar({
504
+ program: root,
505
+ config: {},
506
+ logger: api.logger,
507
+ });
508
+
509
+ const pair = root.children.get("privateclaw")?.children.get("pair");
510
+ assert.ok(pair?.actionHandler, "plugin should register the privateclaw pair subcommand");
511
+
512
+ const stateDir = await mkdtemp(path.join(os.tmpdir(), "privateclaw-cli-state-"));
513
+ const previousStateDir = process.env.OPENCLAW_STATE_DIR;
514
+ process.env.OPENCLAW_STATE_DIR = stateDir;
515
+ t.after(async () => {
516
+ if (previousStateDir == null) {
517
+ delete process.env.OPENCLAW_STATE_DIR;
518
+ } else {
519
+ process.env.OPENCLAW_STATE_DIR = previousStateDir;
520
+ }
521
+ await rm(stateDir, { recursive: true, force: true });
522
+ });
523
+
524
+ const printed: string[] = [];
525
+ const originalLog = console.log;
526
+ console.log = (message?: unknown, ...optionalParams: unknown[]) => {
527
+ printed.push([message, ...optionalParams].map(String).join(" "));
528
+ };
529
+ t.after(() => {
530
+ console.log = originalLog;
531
+ });
532
+
533
+ await pair.actionHandler({
534
+ label: "CLI pairing session",
535
+ relay: `ws://127.0.0.1:${overridePort}`,
536
+ printOnly: true,
537
+ });
538
+
539
+ const inviteUri = printed.find((line) =>
540
+ line.startsWith("邀请链接 / Invite URI: privateclaw://connect?payload="),
541
+ );
542
+ assert.ok(inviteUri, "pair command should print a PrivateClaw invite URI");
543
+ const invite = decodeInviteString(
544
+ inviteUri.replace("邀请链接 / Invite URI: ", ""),
545
+ );
546
+ assert.match(invite.appWsUrl, new RegExp(`:${overridePort}/ws/app`));
547
+ assert.equal(invite.relayLabel, `127.0.0.1:${overridePort}`);
548
+ });
549
+
350
550
  test("privateclaw CLI can list and remove active group-session participants", async (t) => {
351
551
  const relay = createRelayServer({
352
552
  host: "127.0.0.1",