this repo has no description
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

Better tests.

alice 7afc35fb 45270f0c

+110 -40
+110 -40
test.js
··· 1 1 // Minimal test file for transform.js 2 2 const { parseInput, resolveHandle } = require("./transform.js"); 3 + const assert = require("assert").strict; 3 4 4 - function deepEqual(a, b) { 5 - if (a === b) return true; 6 - if (typeof a !== typeof b) return false; 7 - if (a && b && typeof a === "object") { 8 - const aKeys = Object.keys(a); 9 - const bKeys = Object.keys(b); 10 - if (aKeys.length !== bKeys.length) return false; 11 - for (const k of aKeys) { 12 - if (!deepEqual(a[k], b[k])) return false; 13 - } 14 - return true; 15 - } 16 - return false; 5 + function mockFetchResponse(data = {}, isOk = true, httpStatus = 200) { 6 + return () => Promise.resolve({ 7 + ok: isOk, 8 + status: httpStatus, 9 + json: () => Promise.resolve(data), 10 + }); 17 11 } 18 12 13 + const fetchMockConfigs = [ 14 + { 15 + condition: (url) => url.includes("resolveHandle?handle=why.bsky.team"), 16 + response: mockFetchResponse({ did: "did:plc:vpkhqolt662uhesyj6nxm7ys" }), 17 + }, 18 + { 19 + condition: (url) => url.includes("resolveHandle?handle=alice.mosphere.at"), 20 + response: mockFetchResponse({ did: "did:plc:by3jhwdqgbtrcc7q4tkkv3cf" }), 21 + }, 22 + { 23 + condition: (url) => url === "https://didweb.watch/.well-known/did.json", 24 + response: mockFetchResponse({ 25 + "@context": [ 26 + "https://www.w3.org/ns/did/v1", 27 + "https://w3id.org/security/multikey/v1", 28 + "https://w3id.org/security/suites/secp256k1-2019/v1" 29 + ], 30 + "id": "did:web:didweb.watch", 31 + "alsoKnownAs": [ 32 + "at://didweb.watch" 33 + ], 34 + "verificationMethod": [ 35 + { 36 + "id": "did:web:didweb.watch#atproto", 37 + "type": "Multikey", 38 + "controller": "did:web:didweb.watch", 39 + "publicKeyMultibase": "zQ3shPLyZu2EbgJ75P61bMZP4yvBwmtd22ph5sEnY6oLz4YLo" 40 + } 41 + ], 42 + "service": [ 43 + { 44 + "id": "#atproto_pds", 45 + "type": "AtprotoPersonalDataServer", 46 + "serviceEndpoint": "https://zio.blue" 47 + } 48 + ] 49 + }), 50 + }, 51 + { 52 + condition: (url) => url === "https://fail-did-web.com/.well-known/did.json", 53 + response: mockFetchResponse({}, false, 404), 54 + }, 55 + { 56 + condition: (url) => url.includes("resolveHandle?handle=bob.test"), 57 + response: mockFetchResponse({ did: "did:plc:bobtestdid" }), 58 + }, 59 + ]; 60 + 61 + global.fetch = (url) => { 62 + const mock = fetchMockConfigs.find(m => m.condition(url)); 63 + if (mock) { 64 + return mock.response(); 65 + } 66 + console.warn(`Unhandled fetch mock for URL: ${url}`); 67 + return Promise.resolve({ ok: false, status: 500, json: () => Promise.resolve({}) }); 68 + }; 69 + 19 70 async function run() { 20 71 let passed = 0, 21 72 failed = 0; 22 73 function check(name, actual, expected) { 23 - if (deepEqual(actual, expected)) { 74 + try { 75 + assert.deepStrictEqual(actual, expected); 24 76 console.log(`PASS: ${name}`); 25 77 passed++; 26 - } else { 78 + } catch (err) { 27 79 console.log(`FAIL: ${name}\nExpected:`, expected, "\nGot: ", actual); 80 + // Log the full error for debugging if needed, especially for assert failures 81 + // console.error(err); 28 82 failed++; 29 83 } 30 84 } 31 85 32 86 // Test cases for parseInput 33 - const tests = [ 87 + const parseInputTests = [ 34 88 { 35 89 name: "parseInput/feed/cozy", 36 90 input: "https://deer.social/profile/why.bsky.team/feed/cozy", 37 91 expected: { 38 - atUri: 39 - "at://did:plc:vpkhqolt662uhesyj6nxm7ys/app.bsky.feed.generator/cozy", 92 + atUri: "at://did:plc:vpkhqolt662uhesyj6nxm7ys/app.bsky.feed.generator/cozy", 40 93 did: "did:plc:vpkhqolt662uhesyj6nxm7ys", 41 94 handle: "why.bsky.team", 42 95 rkey: "cozy", ··· 46 99 }, 47 100 { 48 101 name: "parseInput/feed.post", 49 - input: 50 - "https://deer.social/profile/did:plc:kkkcb7sys7623hcf7oefcffg/post/3lpe6ek6xhs2n", 102 + input: "https://deer.social/profile/did:plc:kkkcb7sys7623hcf7oefcffg/post/3lpe6ek6xhs2n", 51 103 expected: { 52 - atUri: 53 - "at://did:plc:kkkcb7sys7623hcf7oefcffg/app.bsky.feed.post/3lpe6ek6xhs2n", 104 + atUri: "at://did:plc:kkkcb7sys7623hcf7oefcffg/app.bsky.feed.post/3lpe6ek6xhs2n", 54 105 did: "did:plc:kkkcb7sys7623hcf7oefcffg", 55 106 handle: null, 56 107 rkey: "3lpe6ek6xhs2n", 57 108 nsid: "app.bsky.feed.post", 58 - bskyAppPath: 59 - "/profile/did:plc:kkkcb7sys7623hcf7oefcffg/post/3lpe6ek6xhs2n", 109 + bskyAppPath: "/profile/did:plc:kkkcb7sys7623hcf7oefcffg/post/3lpe6ek6xhs2n", 60 110 }, 61 111 }, 62 112 { 63 113 name: "parseInput/lists", 64 - input: 65 - "https://deer.social/profile/alice.mosphere.at/lists/3l7vfhhfqcz2u", 114 + input: "https://deer.social/profile/alice.mosphere.at/lists/3l7vfhhfqcz2u", 66 115 expected: { 67 - atUri: 68 - "at://did:plc:by3jhwdqgbtrcc7q4tkkv3cf/app.bsky.graph.list/3l7vfhhfqcz2u", 116 + atUri: "at://did:plc:by3jhwdqgbtrcc7q4tkkv3cf/app.bsky.graph.list/3l7vfhhfqcz2u", 69 117 did: "did:plc:by3jhwdqgbtrcc7q4tkkv3cf", 70 118 handle: "alice.mosphere.at", 71 119 rkey: "3l7vfhhfqcz2u", ··· 87 135 }, 88 136 { 89 137 name: "parseInput/did:web/post", 90 - input: 91 - "https://deer.social/profile/did:web:didweb.watch/post/3lpaioe62qk2j", 138 + input: "https://deer.social/profile/did:web:didweb.watch/post/3lpaioe62qk2j", 92 139 expected: { 93 140 atUri: "at://did:web:didweb.watch/app.bsky.feed.post/3lpaioe62qk2j", 94 141 did: "did:web:didweb.watch", ··· 99 146 }, 100 147 }, 101 148 ]; 102 - for (const test of tests) { 149 + for (const test of parseInputTests) { 103 150 try { 104 151 const out = await parseInput(test.input); 105 152 check(test.name, out, test.expected); ··· 109 156 } 110 157 } 111 158 112 - // Test case for resolveHandle 113 - try { 114 - const handle = "alice.mosphere.at"; 115 - const expectedDid = "did:plc:by3jhwdqgbtrcc7q4tkkv3cf"; 116 - const out = await resolveHandle(handle); 117 - check("resolveHandle", out, expectedDid); 118 - } catch (e) { 119 - console.log("FAIL: resolveHandle (exception)", e); 120 - failed++; 159 + // Test cases for resolveHandle 160 + const resolveHandleTests = [ 161 + { 162 + name: "resolveHandle/plc/alice", 163 + input: "alice.mosphere.at", 164 + expected: "did:plc:by3jhwdqgbtrcc7q4tkkv3cf", 165 + }, 166 + { 167 + name: "resolveHandle/plc/bob", 168 + input: "bob.test", 169 + expected: "did:plc:bobtestdid", 170 + }, 171 + { 172 + name: "resolveHandle/did-web/success", 173 + input: "did:web:didweb.watch", 174 + expected: "did:web:didweb.watch", 175 + }, 176 + { 177 + name: "resolveHandle/did-web/failure", 178 + input: "did:web:fail-did-web.com", 179 + expected: "did:web:fail-did-web.com", 180 + }, 181 + ]; 182 + 183 + for (const test of resolveHandleTests) { 184 + try { 185 + const out = await resolveHandle(test.input); 186 + check(test.name, out, test.expected); 187 + } catch (e) { 188 + console.log(`FAIL: ${test.name} (exception)`, e); 189 + failed++; 190 + } 121 191 } 122 192 123 193 console.log(`\nTest results: ${passed} passed, ${failed} failed.`);