|
1 | 1 | import assert from "node:assert/strict"; |
2 | 2 | import test from "node:test"; |
3 | 3 |
|
| 4 | +import { formatTimelineMessages } from "../../messages/lib/formatTimelineMessages.ts"; |
| 5 | +import { getConfigNudgeAuthorPubkey } from "../../messages/ui/configNudgeAuthPubkey.ts"; |
4 | 6 | import { |
5 | 7 | getContextMessageDepth, |
6 | 8 | getReactionTargetId, |
7 | 9 | isInboxThreadContextEvent, |
8 | 10 | matchesInboxFilter, |
| 11 | + toInboxContextMessage, |
| 12 | + toTimelineMessage, |
9 | 13 | } from "./inboxViewHelpers.ts"; |
10 | 14 |
|
11 | 15 | // --- matchesInboxFilter --- |
@@ -256,3 +260,91 @@ test("isInboxThreadContextEvent keeps selected thread root, parent, selected eve |
256 | 260 | true, |
257 | 261 | ); |
258 | 262 | }); |
| 263 | + |
| 264 | +// --- config-nudge pipeline: toInboxContextMessage → toTimelineMessage --- |
| 265 | +// |
| 266 | +// The inbox detail pane renders messages through two mappings that a real |
| 267 | +// event's `kind` and `signerPubkey` must survive: HomeView's |
| 268 | +// toInboxContextMessage and InboxMessageRow's toTimelineMessage. An earlier |
| 269 | +// version of the HomeView mapping dropped both fields, structurally |
| 270 | +// disabling the config-nudge card on the inbox surface (the raw |
| 271 | +// ```buzz:config-nudge fence rendered instead). These tests run a real event |
| 272 | +// through formatTimelineMessages and both mappings, then assert against the |
| 273 | +// same gate helper InboxMessageRow calls. |
| 274 | + |
| 275 | +const NUDGE_CHANNEL_ID = "aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa"; |
| 276 | +const NUDGE_AGENT_SIGNER = |
| 277 | + "2222222222222222222222222222222222222222222222222222222222222222"; |
| 278 | +const NUDGE_HUMAN_SIGNER = |
| 279 | + "1111111111111111111111111111111111111111111111111111111111111111"; |
| 280 | + |
| 281 | +function makeNudgeEvent(overrides = {}) { |
| 282 | + return { |
| 283 | + id: "eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee", |
| 284 | + pubkey: NUDGE_AGENT_SIGNER, |
| 285 | + kind: 9, |
| 286 | + created_at: 1_700_000_000, |
| 287 | + content: "**Fizz** needs configuration.\n\n```buzz:config-nudge\n{}\n```", |
| 288 | + tags: [["h", NUDGE_CHANNEL_ID]], |
| 289 | + sig: "sig", |
| 290 | + ...overrides, |
| 291 | + }; |
| 292 | +} |
| 293 | + |
| 294 | +function mapThroughInboxPipeline(event) { |
| 295 | + const [formatted] = formatTimelineMessages([event], null, undefined, null); |
| 296 | + const contextMessage = toInboxContextMessage(formatted, { |
| 297 | + eventById: new Map([[event.id, event]]), |
| 298 | + fallbackAuthorPubkey: event.pubkey, |
| 299 | + profiles: undefined, |
| 300 | + selectedItemId: event.id, |
| 301 | + }); |
| 302 | + return toTimelineMessage(contextMessage); |
| 303 | +} |
| 304 | + |
| 305 | +test("inbox mappings preserve kind and signerPubkey so the nudge gate can pass", () => { |
| 306 | + const message = mapThroughInboxPipeline(makeNudgeEvent()); |
| 307 | + |
| 308 | + assert.equal(message.kind, 9); |
| 309 | + assert.equal(message.signerPubkey, NUDGE_AGENT_SIGNER); |
| 310 | + assert.equal( |
| 311 | + getConfigNudgeAuthorPubkey( |
| 312 | + message, |
| 313 | + (pubkey) => pubkey === NUDGE_AGENT_SIGNER, |
| 314 | + ), |
| 315 | + NUDGE_AGENT_SIGNER, |
| 316 | + ); |
| 317 | +}); |
| 318 | + |
| 319 | +test("inbox mappings keep the raw signer distinct from a spoofed actor tag", () => { |
| 320 | + const message = mapThroughInboxPipeline( |
| 321 | + makeNudgeEvent({ |
| 322 | + pubkey: NUDGE_HUMAN_SIGNER, |
| 323 | + tags: [ |
| 324 | + ["h", NUDGE_CHANNEL_ID], |
| 325 | + ["actor", NUDGE_AGENT_SIGNER], |
| 326 | + ], |
| 327 | + }), |
| 328 | + ); |
| 329 | + |
| 330 | + // The attributed display author resolves to the agent (actor tag), but the |
| 331 | + // signer stays human — the gate must reject. |
| 332 | + assert.equal(message.pubkey?.toLowerCase(), NUDGE_AGENT_SIGNER); |
| 333 | + assert.equal(message.signerPubkey, NUDGE_HUMAN_SIGNER); |
| 334 | + assert.equal( |
| 335 | + getConfigNudgeAuthorPubkey( |
| 336 | + message, |
| 337 | + (pubkey) => pubkey === NUDGE_AGENT_SIGNER, |
| 338 | + ), |
| 339 | + undefined, |
| 340 | + ); |
| 341 | +}); |
| 342 | + |
| 343 | +test("inbox mappings: unknown signer never enables the card", () => { |
| 344 | + const message = mapThroughInboxPipeline(makeNudgeEvent()); |
| 345 | + |
| 346 | + assert.equal( |
| 347 | + getConfigNudgeAuthorPubkey(message, () => false), |
| 348 | + undefined, |
| 349 | + ); |
| 350 | +}); |
0 commit comments