upleb.uk

Public git repos — served from a NIP-34 GRASP relay at git.upleb.uk

summaryrefslogtreecommitdiff
path: root/tests/unit/test_nostr_event.c
diff options
context:
space:
mode:
authorYour Name <you@example.com>2026-05-17 01:31:49 +0530
committerYour Name <you@example.com>2026-05-17 01:31:49 +0530
commit347d29658959c7e4b368a15134c183f4ce7a25bc (patch)
tree362b3e40273e3c1435bdd0745de61006041bb803 /tests/unit/test_nostr_event.c
parent4c47ae188b288e7d24bd9566ab3e6a6805d9484f (diff)
Testing infrastructure: AGENTS.md rules + unit test framework + geohash tests (11/11 pass)
- Add AGENTS.md: full project context + mandatory testing rules for AI sessions - Add tests/unit/ with host-compiled C unit test infrastructure - Clean stubs approach: ESP-IDF type stubs in tests/unit/stubs/, no source modifications - Fix geohash.c bit extraction bug (3-byte span) found by unit tests - test_geohash: 11/11 passing with reference vectors (Munich, NYC, origin, boundaries)
Diffstat (limited to 'tests/unit/test_nostr_event.c')
-rw-r--r--tests/unit/test_nostr_event.c72
1 files changed, 72 insertions, 0 deletions
diff --git a/tests/unit/test_nostr_event.c b/tests/unit/test_nostr_event.c
new file mode 100644
index 0000000..12bdb93
--- /dev/null
+++ b/tests/unit/test_nostr_event.c
@@ -0,0 +1,72 @@
1#include "test_framework.h"
2#include "../../main/nostr_event.h"
3#include "../../main/identity.h"
4#include <string.h>
5#include <stdio.h>
6
7static const char *TEST_NSEC = "a1b2c3d4e5f6a1b2c3d4e5f6a1b2c3d4e5f6a1b2c3d4e5f6a1b2c3d4e5f6a1b2";
8
9static int time_override = 1700000000;
10
11int main(void)
12{
13 printf("=== test_nostr_event ===\n");
14
15 identity_init(TEST_NSEC);
16 const tollgate_identity_t *id = identity_get();
17
18 printf("\n--- Event ID computation (NIP-01) ---\n");
19 nostr_event_t event;
20 esp_err_t ret = nostr_event_init(&event, id->npub_hex, 1, "[]", "Hello TollGate");
21 ASSERT_EQ_INT(ESP_OK, ret, "nostr_event_init succeeds");
22
23 ASSERT_EQ_STR(id->npub_hex, event.pubkey, "Event pubkey matches npub");
24 ASSERT_EQ_INT(1, event.kind, "Event kind is 1");
25 ASSERT_EQ_INT(64, (int)strlen(event.id), "Event ID is 64 hex chars");
26
27 printf("\n--- Schnorr signing ---\n");
28 ret = nostr_event_sign(&event, id->nsec);
29 ASSERT_EQ_INT(ESP_OK, ret, "nostr_event_sign succeeds");
30 ASSERT_EQ_INT(128, (int)strlen(event.sig), "Signature is 128 hex chars");
31 ASSERT(event.sig[0] != '\0', "Signature is not empty");
32
33 printf("\n--- JSON serialization ---\n");
34 char json_buf[2048];
35 ret = nostr_event_to_json(&event, json_buf, sizeof(json_buf));
36 ASSERT_EQ_INT(ESP_OK, ret, "nostr_event_to_json succeeds");
37
38 ASSERT(strstr(json_buf, "\"id\"") != NULL, "JSON has 'id' field");
39 ASSERT(strstr(json_buf, "\"pubkey\"") != NULL, "JSON has 'pubkey' field");
40 ASSERT(strstr(json_buf, "\"created_at\"") != NULL, "JSON has 'created_at' field");
41 ASSERT(strstr(json_buf, "\"kind\"") != NULL, "JSON has 'kind' field");
42 ASSERT(strstr(json_buf, "\"tags\"") != NULL, "JSON has 'tags' field");
43 ASSERT(strstr(json_buf, "\"content\"") != NULL, "JSON has 'content' field");
44 ASSERT(strstr(json_buf, "\"sig\"") != NULL, "JSON has 'sig' field");
45 ASSERT(strstr(json_buf, "Hello TollGate") != NULL, "JSON contains content");
46
47 printf("\n--- Buffer too small ---\n");
48 char tiny_buf[10];
49 ret = nostr_event_to_json(&event, tiny_buf, sizeof(tiny_buf));
50 ASSERT(ret != ESP_OK, "Returns error when buffer too small");
51
52 printf("\n--- Kind 38787 event (wifistr) ---\n");
53 nostr_event_t ws_event;
54 const char *ws_tags = "[[\"d\",\"test-npub\"],[\"ssid\",\"TollGate-TEST\"],[\"g\",\"u281w0dfz\"]]";
55 ret = nostr_event_init(&ws_event, id->npub_hex, 38787, ws_tags,
56 "TollGate WiFi hotspot: TollGate-TEST");
57 ASSERT_EQ_INT(ESP_OK, ret, "Kind 38787 init succeeds");
58 ASSERT_EQ_INT(38787, ws_event.kind, "Event kind is 38787");
59 ASSERT_EQ_INT(64, (int)strlen(ws_event.id), "Kind 38787 event has valid ID");
60
61 ret = nostr_event_sign(&ws_event, id->nsec);
62 ASSERT_EQ_INT(ESP_OK, ret, "Kind 38787 signing succeeds");
63 ASSERT_EQ_INT(128, (int)strlen(ws_event.sig), "Kind 38787 signature is 128 hex chars");
64
65 printf("\n--- Determinism: same input → same ID ---\n");
66 nostr_event_t event2;
67 nostr_event_init(&event2, id->npub_hex, 1, "[]", "Hello TollGate");
68 ASSERT(strcmp(event.id, event2.id) == 0 || event.created_at != event2.created_at,
69 "Same input produces same ID (if timestamp matches) or differs only by time");
70
71 TEST_SUMMARY();
72}