-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathreadme-integration.spec.js
More file actions
198 lines (175 loc) · 6.36 KB
/
readme-integration.spec.js
File metadata and controls
198 lines (175 loc) · 6.36 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
/**
* Integration tests for README examples using server-as-channel pattern
*/
import { test, assert } from './test.js'
import { capability, URI, Link, Failure, provide, Schema, ok, fail } from '../src/lib.js'
import * as Server from '../src/lib.js'
import * as CAR from '@ucanto/transport/car'
import { ed25519 } from '@ucanto/principal'
import * as Client from '@ucanto/client'
import { parseLink } from '@ucanto/core'
test('README workflow integration with server-as-channel', async () => {
// 1. Define capability (from README)
/** @param {string} uri */
const ensureTrailingDelimiter = uri => (uri.endsWith('/') ? uri : `${uri}/`)
const Add = capability({
can: 'file/link',
with: URI.match({ protocol: 'file:' }),
nb: Schema.struct({
link: Link,
}),
derives: (claimed, delegated) =>
claimed.with.startsWith(ensureTrailingDelimiter(delegated.with)) ?
ok({}) :
fail(`Resource ${claimed.with} is not contained by ${delegated.with}`),
})
// 2. Define service (from README) using proper Server.provide pattern
const context = { store: new Map() }
const service = {
file: {
link: provide(Add, ({ capability, invocation }) => {
context.store.set(capability.with, capability.nb.link)
return ok({
with: capability.with,
link: capability.nb.link,
})
})
}
}
// 3. Create server (from README)
const serviceKey = await ed25519.generate()
const server = Server.create({
id: serviceKey,
service,
codec: CAR.inbound,
validateAuthorization: () => ({ ok: {} }),
canIssue: (capability, issuer) => {
if (capability.with.startsWith("file:")) {
// Extract the DID from the file URI: file:///tmp/did:key:zABC.../path
const url = new URL(capability.with)
const pathParts = url.pathname.split("/")
const did = pathParts[2] // Skip empty string and "tmp"
return did === issuer
}
return false
},
})
// 4. Create client connection using server-as-channel (RECOMMENDED PATTERN)
const connection = Client.connect({
id: serviceKey,
codec: CAR.outbound,
channel: server, // 🎯 Server directly as channel - no HTTP needed!
})
// 5. Create and execute invocation (from README)
const issuerKey = await ed25519.generate()
const testCID = parseLink('bafkreigh2akiscaildcqabsyg3dfr6chu3fgpregiymsck7e7aqa4s52zy')
const me = await Client.invoke({
issuer: issuerKey,
audience: serviceKey,
capability: {
can: 'file/link',
with: `file:///tmp/${issuerKey.did()}/me/about`,
nb: { link: testCID },
},
})
const result = await me.execute(connection)
// 6. Test that the full workflow completed successfully
assert.ok(result)
assert.ok(!result.out.error, `Expected no error, got: ${result.out.error?.message}`)
assert.ok(result.out, 'Expected successful result')
assert.ok(result.out.ok, 'Expected successful result in ok field')
assert.ok(!result.out.error, 'Expected no error in result')
assert.equal(result.out.ok.with, `file:///tmp/${issuerKey.did()}/me/about`)
assert.equal(result.out.ok.link.toString(), testCID.toString())
// 7. Verify the store was updated (proves the service handler actually ran)
const storedLink = context.store.get(`file:///tmp/${issuerKey.did()}/me/about`)
assert.ok(storedLink, 'Expected link to be stored')
assert.equal(storedLink.toString(), testCID.toString())
})
// Test delegation example with server-as-channel
test('README delegation example with server-as-channel', async () => {
// 1. Define the ensureTrailingDelimiter helper
/** @param {string} uri */
const ensureTrailingDelimiter = uri => (uri.endsWith('/') ? uri : `${uri}/`)
// Create the same service setup
const Add = capability({
can: 'file/link',
with: URI.match({ protocol: 'file:' }),
nb: Schema.struct({
link: Link,
}),
derives: (claimed, delegated) =>
claimed.with.startsWith(ensureTrailingDelimiter(delegated.with)) ?
ok({}) :
fail(`Resource ${claimed.with} is not contained by ${delegated.with}`),
})
const context = { store: new Map() }
const service = {
file: {
link: provide(Add, ({ capability, invocation }) => {
context.store.set(capability.with, capability.nb.link)
return ok({
with: capability.with,
link: capability.nb.link,
})
})
}
}
const serviceKey = await ed25519.generate()
const server = Server.create({
id: serviceKey,
service,
codec: CAR.inbound,
validateAuthorization: () => ({ ok: {} }),
canIssue: (capability, issuer) => {
if (capability.with.startsWith("file:")) {
// Extract the DID from the file URI: file:///tmp/did:key:zABC.../path
const url = new URL(capability.with)
const pathParts = url.pathname.split("/")
const did = pathParts[2] // Skip empty string and "tmp"
return did === issuer
}
return false
},
})
// Server-as-channel connection
const connection = Client.connect({
id: serviceKey,
codec: CAR.outbound,
channel: server, // 🎯 Direct server channel
})
// Generate test keys (like README)
const alice = await ed25519.generate()
const bob = await ed25519.generate()
// Alice delegates capability to Bob (like README)
const proof = await Client.delegate({
issuer: alice,
audience: bob,
capabilities: [
{
can: 'file/link',
with: `file:///tmp/${alice.did()}/friends/${bob.did()}/`,
},
],
})
// Bob uses the delegation (like README)
const testCID = parseLink('bafkreigh2akiscaildcqabsyg3dfr6chu3fgpregiymsck7e7aqa4s52zy')
const aboutBob = Client.invoke({
issuer: bob,
audience: serviceKey,
capability: {
can: 'file/link',
with: `file:///tmp/${alice.did()}/friends/${bob.did()}/about`,
nb: { link: testCID },
},
proofs: [proof],
})
const result = await aboutBob.execute(connection)
// This should succeed because Bob has delegated permission from Alice
assert.ok(result)
assert.ok(!result.out.error, `Expected no error, got: ${result.out.error?.message}`)
assert.ok(result.out, 'Expected successful result')
assert.ok(result.out.ok, 'Expected successful result in ok field')
assert.ok(!result.out.error, 'Expected no error in result')
assert.equal(result.out.ok.with, `file:///tmp/${alice.did()}/friends/${bob.did()}/about`)
})