microfed 0.0.11 → 0.0.13

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,139 @@
1
+ /**
2
+ * Microfed WebFinger Module Tests
3
+ * Run: node --test test/webfinger.test.js
4
+ */
5
+
6
+ import { test, describe } from 'node:test'
7
+ import assert from 'node:assert'
8
+ import { createResponse, parseResource, getActorUrl } from '../src/webfinger.js'
9
+
10
+ describe('createResponse', () => {
11
+ test('creates valid JRD response', () => {
12
+ const response = createResponse(
13
+ 'alice@example.com',
14
+ 'https://example.com/users/alice'
15
+ )
16
+
17
+ assert.strictEqual(response.subject, 'acct:alice@example.com')
18
+ assert.ok(Array.isArray(response.links))
19
+ assert.strictEqual(response.links.length, 1)
20
+ })
21
+
22
+ test('includes self link with ActivityPub type', () => {
23
+ const response = createResponse(
24
+ 'alice@example.com',
25
+ 'https://example.com/users/alice'
26
+ )
27
+
28
+ const selfLink = response.links.find(l => l.rel === 'self')
29
+ assert.ok(selfLink)
30
+ assert.strictEqual(selfLink.type, 'application/activity+json')
31
+ assert.strictEqual(selfLink.href, 'https://example.com/users/alice')
32
+ })
33
+
34
+ test('includes profile page link when provided', () => {
35
+ const response = createResponse(
36
+ 'alice@example.com',
37
+ 'https://example.com/users/alice',
38
+ { profileUrl: 'https://example.com/@alice' }
39
+ )
40
+
41
+ const profileLink = response.links.find(l => l.rel === 'http://webfinger.net/rel/profile-page')
42
+ assert.ok(profileLink)
43
+ assert.strictEqual(profileLink.type, 'text/html')
44
+ assert.strictEqual(profileLink.href, 'https://example.com/@alice')
45
+ })
46
+
47
+ test('includes aliases when provided', () => {
48
+ const response = createResponse(
49
+ 'alice@example.com',
50
+ 'https://example.com/users/alice',
51
+ { aliases: ['https://example.com/@alice'] }
52
+ )
53
+
54
+ assert.deepStrictEqual(response.aliases, ['https://example.com/@alice'])
55
+ })
56
+ })
57
+
58
+ describe('parseResource', () => {
59
+ test('parses acct: URI', () => {
60
+ const result = parseResource('acct:alice@example.com')
61
+
62
+ assert.deepStrictEqual(result, {
63
+ username: 'alice',
64
+ domain: 'example.com'
65
+ })
66
+ })
67
+
68
+ test('parses acct: URI with subdomain', () => {
69
+ const result = parseResource('acct:bob@social.example.org')
70
+
71
+ assert.deepStrictEqual(result, {
72
+ username: 'bob',
73
+ domain: 'social.example.org'
74
+ })
75
+ })
76
+
77
+ test('parses https URL with /users/ path', () => {
78
+ const result = parseResource('https://example.com/users/alice')
79
+
80
+ assert.deepStrictEqual(result, {
81
+ username: 'alice',
82
+ domain: 'example.com'
83
+ })
84
+ })
85
+
86
+ test('returns null for invalid input', () => {
87
+ assert.strictEqual(parseResource(null), null)
88
+ assert.strictEqual(parseResource(''), null)
89
+ assert.strictEqual(parseResource('invalid'), null)
90
+ assert.strictEqual(parseResource('acct:noatsign'), null)
91
+ })
92
+
93
+ test('returns null for URL without /users/ path', () => {
94
+ const result = parseResource('https://example.com/@alice')
95
+ assert.strictEqual(result, null)
96
+ })
97
+ })
98
+
99
+ describe('getActorUrl', () => {
100
+ test('extracts actor URL from webfinger response', () => {
101
+ const webfinger = {
102
+ subject: 'acct:alice@example.com',
103
+ links: [
104
+ { rel: 'self', type: 'application/activity+json', href: 'https://example.com/users/alice' }
105
+ ]
106
+ }
107
+
108
+ assert.strictEqual(getActorUrl(webfinger), 'https://example.com/users/alice')
109
+ })
110
+
111
+ test('returns null when no self link exists', () => {
112
+ const webfinger = {
113
+ subject: 'acct:alice@example.com',
114
+ links: [
115
+ { rel: 'other', href: 'https://example.com' }
116
+ ]
117
+ }
118
+
119
+ assert.strictEqual(getActorUrl(webfinger), null)
120
+ })
121
+
122
+ test('returns null when links is missing', () => {
123
+ const webfinger = { subject: 'acct:alice@example.com' }
124
+
125
+ assert.strictEqual(getActorUrl(webfinger), null)
126
+ })
127
+
128
+ test('ignores non-ActivityPub self links', () => {
129
+ const webfinger = {
130
+ subject: 'acct:alice@example.com',
131
+ links: [
132
+ { rel: 'self', type: 'text/html', href: 'https://example.com/@alice' },
133
+ { rel: 'self', type: 'application/activity+json', href: 'https://example.com/users/alice' }
134
+ ]
135
+ }
136
+
137
+ assert.strictEqual(getActorUrl(webfinger), 'https://example.com/users/alice')
138
+ })
139
+ })