|
| 1 | +# Event Generation for Testing |
| 2 | + |
| 3 | +This guide shows how to use NDK's test utilities to generate various types of events for testing purposes. |
| 4 | + |
| 5 | +## Basic Event Generation |
| 6 | + |
| 7 | +Use the EventGenerator to create different types of events: |
| 8 | + |
| 9 | +```typescript |
| 10 | +import { EventGenerator } from "@nostr-dev-kit/ndk-test-utils"; |
| 11 | +import { NDK } from "@nostr-dev-kit/ndk"; |
| 12 | + |
| 13 | +const ndk = new NDK(); |
| 14 | +EventGenerator.setNDK(ndk); // Required setup |
| 15 | +const generator = new EventGenerator(); |
| 16 | + |
| 17 | +// Generate a text note (kind 1) |
| 18 | +const textNote = await generator.textNote("Hello World"); |
| 19 | + |
| 20 | +// Generate metadata (kind 0) |
| 21 | +const metadata = await generator.metadata({ |
| 22 | + name: "Test User", |
| 23 | + about: "Testing NDK", |
| 24 | + picture: "https://example.com/avatar.jpg" |
| 25 | +}); |
| 26 | + |
| 27 | +// Generate contact list (kind 3) |
| 28 | +const contacts = await generator.contactList([ |
| 29 | + "pubkey1", |
| 30 | + "pubkey2", |
| 31 | + "pubkey3" |
| 32 | +]); |
| 33 | +``` |
| 34 | + |
| 35 | +## Advanced Event Generation |
| 36 | + |
| 37 | +### Custom Event Creation |
| 38 | + |
| 39 | +```typescript |
| 40 | +// Create a custom event with specific properties |
| 41 | +const customEvent = await generator.createEvent({ |
| 42 | + kind: 1, |
| 43 | + content: "Custom content", |
| 44 | + tags: [ |
| 45 | + ["p", "pubkey1"], |
| 46 | + ["e", "event1"] |
| 47 | + ] |
| 48 | +}); |
| 49 | + |
| 50 | +// Create an encrypted direct message |
| 51 | +const encryptedDM = await generator.encryptedDM( |
| 52 | + "recipient_pubkey", |
| 53 | + "Secret message" |
| 54 | +); |
| 55 | + |
| 56 | +// Create a reaction event |
| 57 | +const reaction = await generator.reaction( |
| 58 | + "target_event_id", |
| 59 | + "+" // Like reaction |
| 60 | +); |
| 61 | +``` |
| 62 | + |
| 63 | +### Event with Tags |
| 64 | + |
| 65 | +```typescript |
| 66 | +// Create an event with multiple tag types |
| 67 | +const taggedEvent = await generator.createEvent({ |
| 68 | + kind: 1, |
| 69 | + content: "Tagged content", |
| 70 | + tags: [ |
| 71 | + ["p", "pubkey1", "wss://relay.example"], |
| 72 | + ["e", "event1", "wss://relay.example", "reply"], |
| 73 | + ["t", "testing"], |
| 74 | + ["r", "https://example.com"] |
| 75 | + ] |
| 76 | +}); |
| 77 | +``` |
| 78 | + |
| 79 | +### Parametric Events |
| 80 | + |
| 81 | +```typescript |
| 82 | +// Generate events with specific parameters |
| 83 | +const paramEvent = await generator.createEvent({ |
| 84 | + kind: 1, |
| 85 | + content: "Parametric content", |
| 86 | + created_at: Math.floor(Date.now() / 1000), |
| 87 | + tags: [], |
| 88 | + pubkey: "custom_pubkey" // Override default pubkey |
| 89 | +}); |
| 90 | + |
| 91 | +// Generate multiple events |
| 92 | +const events = await Promise.all([ |
| 93 | + generator.textNote("First note"), |
| 94 | + generator.textNote("Second note"), |
| 95 | + generator.textNote("Third note") |
| 96 | +]); |
| 97 | +``` |
| 98 | + |
| 99 | +## Testing Scenarios |
| 100 | + |
| 101 | +### Event Verification |
| 102 | + |
| 103 | +```typescript |
| 104 | +// Test event verification |
| 105 | +const event = await generator.textNote("Test message"); |
| 106 | + |
| 107 | +expect(event.kind).toBe(1); |
| 108 | +expect(event.content).toBe("Test message"); |
| 109 | +expect(event.sig).toBeDefined(); |
| 110 | +expect(event.id).toBeDefined(); |
| 111 | +expect(event.pubkey).toBeDefined(); |
| 112 | +``` |
| 113 | + |
| 114 | +### Event Relationships |
| 115 | + |
| 116 | +```typescript |
| 117 | +// Create a thread of related events |
| 118 | +const rootEvent = await generator.textNote("Root message"); |
| 119 | +const replyEvent = await generator.createEvent({ |
| 120 | + kind: 1, |
| 121 | + content: "Reply message", |
| 122 | + tags: [ |
| 123 | + ["e", rootEvent.id, "", "root"], |
| 124 | + ["p", rootEvent.pubkey] |
| 125 | + ] |
| 126 | +}); |
| 127 | + |
| 128 | +// Create a reaction to the reply |
| 129 | +const reactionEvent = await generator.reaction(replyEvent.id, "+"); |
| 130 | +``` |
| 131 | + |
| 132 | +### Testing Event Processing |
| 133 | + |
| 134 | +```typescript |
| 135 | +// Test event processing with mock relay |
| 136 | +import { RelayMock } from "@nostr-dev-kit/ndk-test-utils"; |
| 137 | + |
| 138 | +const relay = new RelayMock("wss://test.relay"); |
| 139 | +const events = []; |
| 140 | + |
| 141 | +// Subscribe to events |
| 142 | +relay.subscribe({ |
| 143 | + subId: "test", |
| 144 | + filters: [{ kinds: [1] }], |
| 145 | + eventReceived: (event) => events.push(event) |
| 146 | +}); |
| 147 | + |
| 148 | +// Generate and simulate events |
| 149 | +const event1 = await generator.textNote("First"); |
| 150 | +const event2 = await generator.textNote("Second"); |
| 151 | + |
| 152 | +await relay.simulateEvent(event1); |
| 153 | +await relay.simulateEvent(event2); |
| 154 | + |
| 155 | +expect(events).toHaveLength(2); |
| 156 | +``` |
| 157 | + |
| 158 | +## Best Practices |
| 159 | + |
| 160 | +1. Reset generator before tests: |
| 161 | +```typescript |
| 162 | +beforeEach(() => { |
| 163 | + EventGenerator.setNDK(new NDK()); |
| 164 | +}); |
| 165 | +``` |
| 166 | + |
| 167 | +2. Use consistent timestamps for deterministic testing: |
| 168 | +```typescript |
| 169 | +const timestamp = Math.floor(Date.now() / 1000); |
| 170 | +const event = await generator.createEvent({ |
| 171 | + kind: 1, |
| 172 | + content: "Test", |
| 173 | + created_at: timestamp |
| 174 | +}); |
| 175 | +``` |
| 176 | + |
| 177 | +3. Test event validation: |
| 178 | +```typescript |
| 179 | +const event = await generator.textNote("Test"); |
| 180 | +expect(event.verify()).resolves.toBe(true); |
| 181 | +``` |
| 182 | + |
| 183 | +4. Generate related events: |
| 184 | +```typescript |
| 185 | +// Create a conversation thread |
| 186 | +const thread = []; |
| 187 | +const root = await generator.textNote("Root"); |
| 188 | +thread.push(root); |
| 189 | + |
| 190 | +for (let i = 0; i < 3; i++) { |
| 191 | + const reply = await generator.createEvent({ |
| 192 | + kind: 1, |
| 193 | + content: `Reply ${i + 1}`, |
| 194 | + tags: [ |
| 195 | + ["e", thread[i].id, "", "reply"], |
| 196 | + ["p", thread[i].pubkey] |
| 197 | + ] |
| 198 | + }); |
| 199 | + thread.push(reply); |
| 200 | +} |
| 201 | +``` |
0 commit comments