-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.js
More file actions
266 lines (231 loc) · 7.66 KB
/
test.js
File metadata and controls
266 lines (231 loc) · 7.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
#!/usr/bin/env node
'use strict';
const { parseServiceEvent, buildServiceTags, calculateTrustScore, SERVICE_KIND } = require('./lib');
let passed = 0;
let failed = 0;
function assert(condition, msg) {
if (condition) {
passed++;
console.log(` ✅ ${msg}`);
} else {
failed++;
console.log(` ❌ ${msg}`);
}
}
// ─── parseServiceEvent ───
console.log('\n📦 parseServiceEvent');
const mockEvent = {
kind: SERVICE_KIND,
id: 'abc123',
pubkey: 'pk_test',
created_at: 1706832000,
content: 'A test service for translation.',
tags: [
['d', 'translation-service'],
['name', 'Test Translator'],
['c', 'translation'],
['c', 'summarization'],
['price', '21', 'sats', 'request'],
['ln', 'test@getalby.com'],
['status', 'active'],
['k', '5100'],
['t', 'ai'],
['t', 'agent'],
['t', 'service']
],
sig: 'sig_test'
};
const svc = parseServiceEvent(mockEvent);
assert(svc.pubkey === 'pk_test', 'pubkey extracted');
assert(svc.eventId === 'abc123', 'eventId extracted');
assert(svc.createdAt === 1706832000, 'createdAt extracted');
assert(svc.id === 'translation-service', 'd-tag → id');
assert(svc.name === 'Test Translator', 'name extracted');
assert(svc.capabilities.length === 2, 'two capabilities');
assert(svc.capabilities[0] === 'translation', 'first capability');
assert(svc.capabilities[1] === 'summarization', 'second capability');
assert(svc.price.amount === 21, 'price amount');
assert(svc.price.currency === 'sats', 'price currency');
assert(svc.price.per === 'request', 'price per');
assert(svc.lnAddress === 'test@getalby.com', 'Lightning address');
assert(svc.status === 'active', 'status');
assert(svc.dvmKinds[0] === '5100', 'DVM kind');
assert(svc.hashtags.includes('ai'), 'hashtag ai');
assert(svc.description === 'A test service for translation.', 'description from content');
// Minimal event
const minEvent = {
kind: SERVICE_KIND,
id: 'min1',
pubkey: 'pk_min',
created_at: 1706832000,
content: '',
tags: [['d', 'bare']],
sig: 'sig_min'
};
const minSvc = parseServiceEvent(minEvent);
assert(minSvc.id === 'bare', 'minimal: id extracted');
assert(minSvc.capabilities.length === 0, 'minimal: no capabilities');
assert(minSvc.price === null, 'minimal: no price');
assert(minSvc.lnAddress === null, 'minimal: no lightning');
assert(minSvc.status === 'active', 'minimal: default status active');
// ─── buildServiceTags ───
console.log('\n🏗️ buildServiceTags');
const tags = buildServiceTags({
id: 'text-gen',
name: 'Text Agent',
capabilities: ['text-generation', 'translation'],
price: { amount: 50, currency: 'sats', per: 'request' },
lnAddress: 'agent@domain.com',
status: 'active',
dvmKinds: ['5050'],
hashtags: ['multilingual']
});
const tagMap = {};
for (const t of tags) {
if (!tagMap[t[0]]) tagMap[t[0]] = [];
tagMap[t[0]].push(t);
}
assert(tagMap['d'][0][1] === 'text-gen', 'd-tag set');
assert(tagMap['name'][0][1] === 'Text Agent', 'name tag set');
assert(tagMap['c'].length === 2, 'two capability tags');
assert(tagMap['c'][0][1] === 'text-generation', 'first capability tag');
assert(tagMap['price'][0][1] === '50', 'price amount as string');
assert(tagMap['price'][0][2] === 'sats', 'price currency');
assert(tagMap['ln'][0][1] === 'agent@domain.com', 'ln tag');
assert(tagMap['status'][0][1] === 'active', 'status tag');
assert(tagMap['k'][0][1] === '5050', 'DVM kind tag');
assert(tagMap['t'].some(t => t[1] === 'multilingual'), 'custom hashtag');
assert(tagMap['t'].some(t => t[1] === 'agent'), 'auto agent tag');
assert(tagMap['t'].some(t => t[1] === 'service'), 'auto service tag');
// Without optional fields
const minTags = buildServiceTags({ id: 'bare' });
const minTagMap = {};
for (const t of minTags) {
if (!minTagMap[t[0]]) minTagMap[t[0]] = [];
minTagMap[t[0]].push(t);
}
assert(minTagMap['d'][0][1] === 'bare', 'minimal: d-tag');
assert(minTagMap['status'][0][1] === 'active', 'minimal: default status');
assert(!minTagMap['price'], 'minimal: no price tag');
assert(!minTagMap['ln'], 'minimal: no ln tag');
// ─── calculateTrustScore ───
console.log('\n🛡️ calculateTrustScore');
const targetPk = 'target_pk_123';
const attestations = [
{
kind: 1985,
pubkey: 'attester_1',
tags: [
['L', 'ai.wot'],
['l', 'service-quality', 'ai.wot'],
['p', targetPk]
],
created_at: 1706832000
},
{
kind: 1985,
pubkey: 'attester_2',
tags: [
['L', 'ai.wot'],
['l', 'general-trust', 'ai.wot'],
['p', targetPk]
],
created_at: 1706832000
},
{
kind: 1985,
pubkey: 'attester_3',
tags: [
['L', 'ai.wot'],
['l', 'work-completed', 'ai.wot'],
['p', targetPk]
],
created_at: 1706832000
},
// Self-attestation (should be excluded)
{
kind: 1985,
pubkey: targetPk,
tags: [
['L', 'ai.wot'],
['l', 'service-quality', 'ai.wot'],
['p', targetPk]
],
created_at: 1706832000
},
// Duplicate attester (should keep highest weight)
{
kind: 1985,
pubkey: 'attester_1',
tags: [
['L', 'ai.wot'],
['l', 'general-trust', 'ai.wot'],
['p', targetPk]
],
created_at: 1706831000
}
];
const trust = calculateTrustScore(attestations, targetPk);
assert(trust.attesters === 3, 'three unique attesters (self excluded)');
// attester_1: service-quality (1.5) > general-trust (0.8) → 15 points
// attester_2: general-trust (0.8) → 8 points
// attester_3: work-completed (1.2) → 12 points
// Total: 35
assert(trust.score === 35, `score is 35 (got ${trust.score})`);
assert(trust.details.length === 3, 'three details entries');
// No attestations
const empty = calculateTrustScore([], 'nobody');
assert(empty.score === 0, 'no attestations → score 0');
assert(empty.attesters === 0, 'no attestations → 0 attesters');
// Only self-attestation
const selfOnly = calculateTrustScore([{
kind: 1985,
pubkey: 'me',
tags: [['L', 'ai.wot'], ['l', 'service-quality', 'ai.wot'], ['p', 'me']],
created_at: 1706832000
}], 'me');
assert(selfOnly.score === 0, 'self-only → score 0');
// ─── Round-trip: build → parse ───
console.log('\n🔄 Round-trip: buildServiceTags → parseServiceEvent');
const original = {
id: 'roundtrip-test',
name: 'Roundtrip Agent',
capabilities: ['research', 'data-analysis'],
price: { amount: 100, currency: 'sats', per: 'query' },
lnAddress: 'rt@test.com',
status: 'active',
dvmKinds: ['5050', '5300'],
hashtags: ['research']
};
const rtTags = buildServiceTags(original);
const rtEvent = {
kind: SERVICE_KIND,
id: 'event_rt',
pubkey: 'pk_rt',
created_at: 1706900000,
content: 'Research and data analysis service.',
tags: rtTags,
sig: 'sig_rt'
};
const parsed = parseServiceEvent(rtEvent);
assert(parsed.id === 'roundtrip-test', 'rt: id preserved');
assert(parsed.name === 'Roundtrip Agent', 'rt: name preserved');
assert(parsed.capabilities.length === 2, 'rt: capabilities preserved');
assert(parsed.capabilities[0] === 'research', 'rt: first capability');
assert(parsed.price.amount === 100, 'rt: price amount');
assert(parsed.price.per === 'query', 'rt: price per');
assert(parsed.lnAddress === 'rt@test.com', 'rt: lightning address');
assert(parsed.dvmKinds.length === 2, 'rt: DVM kinds preserved');
assert(parsed.hashtags.includes('research'), 'rt: custom hashtag');
assert(parsed.hashtags.includes('agent'), 'rt: auto agent tag');
// ─── Constants ───
console.log('\n📐 Constants');
assert(SERVICE_KIND === 38990, 'SERVICE_KIND is 38990');
// ─── Summary ───
console.log(`\n${'─'.repeat(40)}`);
console.log(`Results: ${passed} passed, ${failed} failed`);
if (failed > 0) {
process.exit(1);
} else {
console.log('All tests passed! ✅\n');
}