|
1 | | -/** Tests context report command output and generated report files. */ |
| 1 | +/** Tests context command behavior, token reporting, and generated report files. */ |
2 | 2 | import { mkdtemp, readFile, rm, unlink, writeFile } from "node:fs/promises"; |
3 | 3 | import { tmpdir } from "node:os"; |
4 | 4 | import { join } from "node:path"; |
5 | 5 | import { describe, expect, it } from "vitest"; |
| 6 | +import type { OpenClawConfig } from "../../config/config.js"; |
6 | 7 | import type { SessionEntry } from "../../config/sessions.js"; |
7 | 8 | import { buildContextReply } from "./commands-context-report.js"; |
| 9 | +import { buildCommandContext } from "./commands-context.js"; |
8 | 10 | import type { HandleCommandsParams } from "./commands-types.js"; |
| 11 | +import { stripStructuralPrefixes } from "./mentions.js"; |
| 12 | +import { buildTestCtx } from "./test-ctx.js"; |
| 13 | + |
| 14 | +/** Tests context report command output and generated report files. */ |
9 | 15 |
|
10 | 16 | function makeParams( |
11 | 17 | commandBodyNormalized: string, |
@@ -412,3 +418,106 @@ describe("buildContextReply", () => { |
412 | 418 | expect(result.mediaUrl).toBeUndefined(); |
413 | 419 | }); |
414 | 420 | }); |
| 421 | + |
| 422 | +/** Tests context command behavior and token reporting. */ |
| 423 | + |
| 424 | +describe("buildCommandContext", () => { |
| 425 | + it("canonicalizes registered aliases like /id to their primary command", () => { |
| 426 | + const ctx = buildTestCtx({ |
| 427 | + Provider: "webchat", |
| 428 | + Surface: "webchat", |
| 429 | + From: "user", |
| 430 | + To: "bot", |
| 431 | + Body: "/id", |
| 432 | + RawBody: "/id", |
| 433 | + CommandBody: "/id", |
| 434 | + BodyForCommands: "/id", |
| 435 | + }); |
| 436 | + |
| 437 | + const result = buildCommandContext({ |
| 438 | + ctx, |
| 439 | + cfg: {} as OpenClawConfig, |
| 440 | + isGroup: false, |
| 441 | + triggerBodyNormalized: "/id", |
| 442 | + commandAuthorized: true, |
| 443 | + }); |
| 444 | + |
| 445 | + expect(result.commandBodyNormalized).toBe("/whoami"); |
| 446 | + }); |
| 447 | + |
| 448 | + it("preserves multiline soft reset tails after structural normalization", () => { |
| 449 | + const ctx = buildTestCtx({ |
| 450 | + Provider: "whatsapp", |
| 451 | + Surface: "whatsapp", |
| 452 | + From: "user", |
| 453 | + To: "bot", |
| 454 | + Body: "/reset soft\nre-read persona files", |
| 455 | + RawBody: "/reset soft\nre-read persona files", |
| 456 | + CommandBody: "/reset soft\nre-read persona files", |
| 457 | + BodyForCommands: "/reset soft\nre-read persona files", |
| 458 | + }); |
| 459 | + |
| 460 | + const result = buildCommandContext({ |
| 461 | + ctx, |
| 462 | + cfg: {} as OpenClawConfig, |
| 463 | + isGroup: false, |
| 464 | + triggerBodyNormalized: stripStructuralPrefixes("/reset soft\nre-read persona files"), |
| 465 | + commandAuthorized: true, |
| 466 | + }); |
| 467 | + |
| 468 | + expect(result.commandBodyNormalized).toBe("/reset soft re-read persona files"); |
| 469 | + }); |
| 470 | + |
| 471 | + it("preserves multiline slash skill payloads after structural normalization", () => { |
| 472 | + const body = "/skill demo_skill first line\nsecond line"; |
| 473 | + const ctx = buildTestCtx({ |
| 474 | + Provider: "whatsapp", |
| 475 | + Surface: "whatsapp", |
| 476 | + From: "user", |
| 477 | + To: "bot", |
| 478 | + Body: body, |
| 479 | + RawBody: body, |
| 480 | + CommandBody: body, |
| 481 | + BodyForCommands: body, |
| 482 | + }); |
| 483 | + |
| 484 | + const result = buildCommandContext({ |
| 485 | + ctx, |
| 486 | + cfg: {} as OpenClawConfig, |
| 487 | + isGroup: false, |
| 488 | + triggerBodyNormalized: stripStructuralPrefixes(body), |
| 489 | + commandAuthorized: true, |
| 490 | + }); |
| 491 | + |
| 492 | + expect(result.commandBodyNormalized).toBe("/skill demo_skill first line\nsecond line"); |
| 493 | + }); |
| 494 | + |
| 495 | + it("maps explicit gateway origin into command context", () => { |
| 496 | + const ctx = buildTestCtx({ |
| 497 | + Provider: "internal", |
| 498 | + Surface: "internal", |
| 499 | + OriginatingChannel: "slack", |
| 500 | + OriginatingTo: "user:U123", |
| 501 | + SenderId: "gateway-client", |
| 502 | + From: undefined, |
| 503 | + To: undefined, |
| 504 | + Body: "/codex bind", |
| 505 | + RawBody: "/codex bind", |
| 506 | + CommandBody: "/codex bind", |
| 507 | + BodyForCommands: "/codex bind", |
| 508 | + }); |
| 509 | + |
| 510 | + const result = buildCommandContext({ |
| 511 | + ctx, |
| 512 | + cfg: {} as OpenClawConfig, |
| 513 | + isGroup: false, |
| 514 | + triggerBodyNormalized: "/codex bind", |
| 515 | + commandAuthorized: true, |
| 516 | + }); |
| 517 | + |
| 518 | + expect(result.channel).toBe("slack"); |
| 519 | + expect(result.channelId).toBe("slack"); |
| 520 | + expect(result.from).toBe("gateway-client"); |
| 521 | + expect(result.to).toBe("user:U123"); |
| 522 | + }); |
| 523 | +}); |
0 commit comments