|
| 1 | +// Covers bounded, untrusted channel metadata construction. |
| 2 | +import { describe, expect, it } from "vitest"; |
| 3 | +import { buildUntrustedChannelMetadata } from "./channel-metadata.js"; |
| 4 | + |
| 5 | +function normalizeMarkerIds(value: string): string { |
| 6 | + return value.replace(/id="[a-f0-9]{16}"/g, 'id="<id>"'); |
| 7 | +} |
| 8 | + |
| 9 | +function wrapExpected(content: string): string { |
| 10 | + return [ |
| 11 | + "", |
| 12 | + '<<<EXTERNAL_UNTRUSTED_CONTENT id="<id>">>>', |
| 13 | + "Source: Channel metadata", |
| 14 | + "---", |
| 15 | + content, |
| 16 | + '<<<END_EXTERNAL_UNTRUSTED_CONTENT id="<id>">>>', |
| 17 | + ].join("\n"); |
| 18 | +} |
| 19 | + |
| 20 | +describe("buildUntrustedChannelMetadata", () => { |
| 21 | + it("keeps per-entry truncation UTF-16 safe", () => { |
| 22 | + const entryPrefix = "a".repeat(396); |
| 23 | + const result = buildUntrustedChannelMetadata({ |
| 24 | + source: "test", |
| 25 | + label: "Test channel", |
| 26 | + entries: [`${entryPrefix}🎉tail`], |
| 27 | + }); |
| 28 | + |
| 29 | + expect(normalizeMarkerIds(result ?? "")).toBe( |
| 30 | + wrapExpected(`UNTRUSTED channel metadata (test)\nTest channel:\n${entryPrefix}...`), |
| 31 | + ); |
| 32 | + }); |
| 33 | + |
| 34 | + it("keeps the combined metadata limit UTF-16 safe", () => { |
| 35 | + const header = "UNTRUSTED channel metadata (test)\nTest channel:\n"; |
| 36 | + const entryPrefix = "short"; |
| 37 | + const result = buildUntrustedChannelMetadata({ |
| 38 | + source: "test", |
| 39 | + label: "Test channel", |
| 40 | + entries: [`${entryPrefix}🎉tail`], |
| 41 | + maxChars: header.length + entryPrefix.length + 4, |
| 42 | + }); |
| 43 | + |
| 44 | + expect(normalizeMarkerIds(result ?? "")).toBe(wrapExpected(`${header}${entryPrefix}...`)); |
| 45 | + }); |
| 46 | +}); |
0 commit comments