|
1 | 1 | // Nostr tests cover nostr profile plugin behavior. |
2 | | -import { verifyEvent, getPublicKey } from "nostr-tools"; |
| 2 | +import { verifyEvent, getPublicKey, type SimplePool } from "nostr-tools"; |
3 | 3 | import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; |
4 | 4 | import type { NostrProfile } from "./config-schema.js"; |
5 | 5 | import { |
|
8 | 8 | contentToProfile, |
9 | 9 | validateProfile, |
10 | 10 | sanitizeProfileForDisplay, |
| 11 | + publishProfile, |
11 | 12 | type ProfileContent, |
12 | 13 | } from "./nostr-profile.js"; |
13 | 14 | import { TEST_HEX_PRIVATE_KEY_BYTES } from "./test-fixtures.js"; |
@@ -414,3 +415,74 @@ describe("edge cases", () => { |
414 | 415 | expect(verifyEvent(event)).toBe(true); |
415 | 416 | }); |
416 | 417 | }); |
| 418 | + |
| 419 | +// ============================================================================ |
| 420 | +// Profile Publishing Tests |
| 421 | +// ============================================================================ |
| 422 | + |
| 423 | +describe("publishProfile", () => { |
| 424 | + beforeEach(() => { |
| 425 | + vi.useFakeTimers(); |
| 426 | + }); |
| 427 | + |
| 428 | + afterEach(() => { |
| 429 | + vi.useRealTimers(); |
| 430 | + }); |
| 431 | + |
| 432 | + function createFakePool(publishResult: unknown): SimplePool { |
| 433 | + return { |
| 434 | + publish: vi.fn(() => [publishResult]), |
| 435 | + } as unknown as SimplePool; |
| 436 | + } |
| 437 | + |
| 438 | + it("clears the per-relay timeout timer after a successful publish", async () => { |
| 439 | + const clearTimeoutSpy = vi.spyOn(globalThis, "clearTimeout"); |
| 440 | + const profile: NostrProfile = { name: "test" }; |
| 441 | + const pool = createFakePool(Promise.resolve()); |
| 442 | + |
| 443 | + const result = await publishProfile( |
| 444 | + pool, |
| 445 | + TEST_HEX_PRIVATE_KEY_BYTES, |
| 446 | + ["wss://relay.example"], |
| 447 | + profile, |
| 448 | + ); |
| 449 | + |
| 450 | + expect(result.successes).toEqual(["wss://relay.example"]); |
| 451 | + expect(clearTimeoutSpy).toHaveBeenCalledTimes(1); |
| 452 | + }); |
| 453 | + |
| 454 | + it("clears the per-relay timeout timer after a publish timeout", async () => { |
| 455 | + const clearTimeoutSpy = vi.spyOn(globalThis, "clearTimeout"); |
| 456 | + const profile: NostrProfile = { name: "test" }; |
| 457 | + const pool = createFakePool(new Promise(() => {})); |
| 458 | + |
| 459 | + const promise = publishProfile( |
| 460 | + pool, |
| 461 | + TEST_HEX_PRIVATE_KEY_BYTES, |
| 462 | + ["wss://relay.example"], |
| 463 | + profile, |
| 464 | + ); |
| 465 | + vi.advanceTimersByTime(6_000); |
| 466 | + const result = await promise; |
| 467 | + |
| 468 | + expect(result.failures).toHaveLength(1); |
| 469 | + expect(result.failures[0]?.error).toContain("timeout"); |
| 470 | + expect(clearTimeoutSpy).toHaveBeenCalledTimes(1); |
| 471 | + }); |
| 472 | + |
| 473 | + it("does not add dangling timers when publishing to multiple relays", async () => { |
| 474 | + vi.spyOn(globalThis, "setTimeout").mockClear(); |
| 475 | + const clearTimeoutSpy = vi.spyOn(globalThis, "clearTimeout"); |
| 476 | + const profile: NostrProfile = { name: "test" }; |
| 477 | + const pool = createFakePool(Promise.resolve()); |
| 478 | + |
| 479 | + await publishProfile( |
| 480 | + pool, |
| 481 | + TEST_HEX_PRIVATE_KEY_BYTES, |
| 482 | + ["wss://relay.a", "wss://relay.b"], |
| 483 | + profile, |
| 484 | + ); |
| 485 | + |
| 486 | + expect(clearTimeoutSpy).toHaveBeenCalledTimes(2); |
| 487 | + }); |
| 488 | +}); |
0 commit comments