fix logic and add unit tests

This commit is contained in:
Sam Samskies 2023-04-20 15:01:29 -05:00
parent b068c00b7f
commit 969284e47b
No known key found for this signature in database
GPG Key ID: E3F697EE1B6EB156
3 changed files with 31 additions and 2 deletions

View File

@ -1,4 +1,4 @@
import { splitByUrl, magnetURIDecode, getRelayName } from "./Util";
import { splitByUrl, magnetURIDecode, getRelayName, validateNostrLink } from "./Util";
describe("splitByUrl", () => {
it("should split a string by URLs", () => {
@ -90,3 +90,24 @@ describe("getRelayName", () => {
expect(output).toEqual("relay.example2.com?broadcast=true");
});
});
describe("validateNostrLink", () => {
it("should return true for valid nostr links", () => {
[
"nostr:npub10elfcs4fr0l0r8af98jlmgdh9c8tcxjvz9qkw038js35mp4dma8qzvjptg",
"web+nostr:npub10elfcs4fr0l0r8af98jlmgdh9c8tcxjvz9qkw038js35mp4dma8qzvjptg",
"nostr:note15449edq4qa5wzgqvh8td0q0dp6hwtes4pknsrm7eygeenhlj99xsq94wu9",
"nostr:nprofile1qqsrhuxx8l9ex335q7he0f09aej04zpazpl0ne2cgukyawd24mayt8gpp4mhxue69uhhytnc9e3k7mgpz4mhxue69uhkg6nzv9ejuumpv34kytnrdaksjlyr9p",
"nostr:nevent1qqs226juks2sw68pyqxtn4khs8ksath9uc2smfcpalvjyvuemlezjngrd87dq",
"nostr:naddr1qqzkjurnw4ksz9thwden5te0wfjkccte9ehx7um5wghx7un8qgs2d90kkcq3nk2jry62dyf50k0h36rhpdtd594my40w9pkal876jxgrqsqqqa28pccpzu",
].forEach(link => {
expect(validateNostrLink(link)).toBe(true);
});
});
it("should return false for invalid nostr links", () => {
["nostr:npub", "web+nostr:npub", "nostr:nevent1xxx"].forEach(link => {
expect(validateNostrLink(link)).toBe(false);
});
});
});

View File

@ -499,7 +499,11 @@ export function validateNostrLink(link: string): boolean {
return false;
}
return parsedLink.id.length === 64;
if (parsedLink.type === NostrPrefix.PublicKey || parsedLink.type === NostrPrefix.Note) {
return parsedLink.id.length === 64;
}
return true;
} catch {
return false;
}

View File

@ -0,0 +1,4 @@
// @ts-expect-error - we have a folder called util so TS gets confused
import { TextEncoder, TextDecoder } from "util";
Object.assign(global, { TextDecoder, TextEncoder });