Skip to content

Commit

Permalink
including tests for nostr type guard
Browse files Browse the repository at this point in the history
  • Loading branch information
antonioconselheiro committed May 30, 2024
1 parent 3ad2e6b commit 177f4b2
Show file tree
Hide file tree
Showing 2 changed files with 110 additions and 23 deletions.
105 changes: 103 additions & 2 deletions core.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { test, expect } from 'bun:test'

import { sortEvents } from './core.ts'
import { NostrTypeGuard, sortEvents } from './core.ts'

test('sortEvents', () => {
const events = [
Expand All @@ -17,3 +16,105 @@ test('sortEvents', () => {
{ id: 'abc123', pubkey: 'key1', created_at: 1610000000, kind: 1, tags: [], content: 'Hello', sig: 'sig1' },
])
})

test('NostrTypeGuard isNProfile', () => {
const is = NostrTypeGuard.isNProfile('nprofile1qqsvc6ulagpn7kwrcwdqgp797xl7usumqa6s3kgcelwq6m75x8fe8yc5usxdg')

expect(is).toBeTrue()

Check failure on line 23 in core.test.ts

View workflow job for this annotation

GitHub Actions / test

error: expect(received).toBeTrue()

Received: false at /home/runner/work/nostr-tools/nostr-tools/core.test.ts:23:3
})

test('NostrTypeGuard isNProfile invalid nprofile', () => {
const is = NostrTypeGuard.isNProfile('nprofile1qqsvc6ulagpn7kwrcwdqgp797xl7usumqa6s3kgcelwq6m75x8fe8yc5usxãg')

expect(is).toBeFalse()
})

test('NostrTypeGuard isNProfile with invalid nprofile', () => {
const is = NostrTypeGuard.isNProfile('nsec1lqw6zqyanj9mz8gwhdam6tqge42vptz4zg93qsfej440xm5h5esqya0juv')

expect(is).toBeFalse()
})

test('NostrTypeGuard isNRelay', () => {
const is = NostrTypeGuard.isNRelay('nrelay1qqt8wumn8ghj7un9d3shjtnwdaehgu3wvfskueq4r295t')

expect(is).toBeTrue()
})

test('NostrTypeGuard isNRelay with invalid nrelay', () => {
const is = NostrTypeGuard.isNRelay('nrelay1qqt8wumn8ghj7un9d3shjtnwdaehgu3wvfskueã4r295t')

expect(is).toBeFalse()
})

test('NostrTypeGuard isNRelay with invalid nrelay', () => {
const is = NostrTypeGuard.isNRelay('nevent1qqst8cujky046negxgwwm5ynqwn53t8aqjr6afd8g59nfqwxpdhylpcpzamhxue69uhhyetvv9ujuetcv9khqmr99e3k7mg8arnc9')

expect(is).toBeFalse()
})

test('NostrTypeGuard isNEvent', () => {
const is = NostrTypeGuard.isNEvent('nevent1qqst8cujky046negxgwwm5ynqwn53t8aqjr6afd8g59nfqwxpdhylpcpzamhxue69uhhyetvv9ujuetcv9khqmr99e3k7mg8arnc9')

expect(is).toBeTrue()
})

test('NostrTypeGuard isNEvent with invalid nevent', () => {
const is = NostrTypeGuard.isNEvent('nevent1qqst8cujky046negxgwwm5ynqwn53t8aqjr6afd8g59nfqwxpdhylpcpzamhxue69uhhyetvv9ujuetcv9khqmr99e3k7mg8ãrnc9')

expect(is).toBeFalse()
})

test('NostrTypeGuard isNEvent with invalid nevent', () => {
const is = NostrTypeGuard.isNEvent('nprofile1qqsvc6ulagpn7kwrcwdqgp797xl7usumqa6s3kgcelwq6m75x8fe8yc5usxdg')

expect(is).toBeFalse()
})

test('NostrTypeGuard isNAddress', () => {
const is = NostrTypeGuard.isNAddress('naddr1qqxnzdesxqmnxvpexqunzvpcqyt8wumn8ghj7un9d3shjtnwdaehgu3wvfskueqzypve7elhmamff3sr5mgxxms4a0rppkmhmn7504h96pfcdkpplvl2jqcyqqq823cnmhuld')

expect(is).toBeTrue()
})

test('NostrTypeGuard isNAddress with invalid nadress', () => {
const is = NostrTypeGuard.isNAddress('nsec1lqw6zqyanj9mz8gwhdam6tqge42vptz4zg93qsfej440xm5h5esqya0juv')

expect(is).toBeFalse()
})

test('NostrTypeGuard isNSecret', () => {
const is = NostrTypeGuard.isNSecret('nsec1lqw6zqyanj9mz8gwhdam6tqge42vptz4zg93qsfej440xm5h5esqya0juv')

expect(is).toBeTrue()
})

test('NostrTypeGuard isNSecret with invalid nsec', () => {
const is = NostrTypeGuard.isNSecret('nsec1lqw6zqyanj9mz8gwhdam6tqge42vptz4zg93qsfej440xm5h5esqya0juã')

expect(is).toBeFalse()
})

test('NostrTypeGuard isNSecret with invalid nsec', () => {
const is = NostrTypeGuard.isNSecret('nprofile1qqsvc6ulagpn7kwrcwdqgp797xl7usumqa6s3kgcelwq6m75x8fe8yc5usxdg')

expect(is).toBeFalse()
})

test('NostrTypeGuard isNPublic', () => {
const is = NostrTypeGuard.isNPublic('npub1jz5mdljkmffmqjshpyjgqgrhdkuxd9ztzasv8xeh5q92fv33sjgqy4pats')

expect(is).toBeTrue()
})

test('NostrTypeGuard isNPublic with invalid npub', () => {
const is = NostrTypeGuard.isNPublic('npub1jz5mdljkmffmqjshpyjgqgrhdkuxd9ztzãsv8xeh5q92fv33sjgqy4pats')

expect(is).toBeFalse()
})

test('NostrTypeGuard isNPublic with invalid npub', () => {
const is = NostrTypeGuard.isNPublic('nsec1lqw6zqyanj9mz8gwhdam6tqge42vptz4zg93qsfej440xm5h5esqya0juv')

expect(is).toBeFalse()
})
28 changes: 7 additions & 21 deletions core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,27 +33,13 @@ export type Note = `note${string}`
export type Nip05 = `${string}@${string}`

export const NostrTypeGuard = {
isNProfile: (value: unknown): value is NProfile => {
return typeof value === 'string' && /^nprofile1[a-z\d]{58}$/.test(value)
},
isNRelay: (value: unknown): value is NRelay => {
return typeof value === 'string' && /^nrelay1[a-z\d]{58}$/.test(value)
},
isNEvent: (value: unknown): value is NEvent => {
return typeof value === 'string' && /^nevent1[a-z\d]{58}$/.test(value)
},
isNAddress: (value: unknown): value is NAddress => {
return typeof value === 'string' && /^naddr1[a-z\d]{58}$/.test(value)
},
isNSecret: (value: unknown): value is NSecret => {
return typeof value === 'string' && /^nsec1[a-z\d]{58}$/.test(value)
},
isNPublic: (value: unknown): value is NPublic => {
return typeof value === 'string' && /^npub1[a-z\d]{58}$/.test(value)
},
isNote: (value: unknown): value is Note => {
return typeof value === 'string' && /^note1[a-z\d]{58}$/.test(value)
}
isNProfile: (value?: string): value is NProfile => /^nprofile1[a-z\d]{58}$/.test(value || ''),
isNRelay: (value?: string): value is NRelay => /^nrelay1[a-z\d]{45}$/.test(value || ''),
isNEvent: (value?: string): value is NEvent => /^nevent1[a-z\d]+$/.test(value || ''),
isNAddress: (value?: string): value is NAddress => /^naddr1[a-z\d]+$/.test(value || ''),
isNSecret: (value?: string): value is NSecret => /^nsec1[a-z\d]{58}$/.test(value || ''),
isNPublic: (value?: string): value is NPublic => /^npub1[a-z\d]{58}$/.test(value || ''),
isNote: (value?: string): value is Note => /^note1[a-z\d]{58}$/.test(value || '')
}

/** An event whose signature has been verified. */
Expand Down

0 comments on commit 177f4b2

Please sign in to comment.