Skip to content

Commit

Permalink
test: add random topic testing
Browse files Browse the repository at this point in the history
  • Loading branch information
rygine committed Nov 29, 2023
1 parent 21f7b85 commit f3f94af
Showing 1 changed file with 23 additions and 1 deletion.
24 changes: 23 additions & 1 deletion test/utils/topic.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import { buildContentTopic, isValidTopic } from '../../src/utils/topic'
import {
buildContentTopic,
buildDirectMessageTopicV2,
isValidTopic,
} from '../../src/utils/topic'
import crypto from '../../src/crypto/crypto'

describe('topic utils', () => {
describe('isValidTopic', () => {
Expand All @@ -22,5 +27,22 @@ describe('topic utils', () => {
false
)
})

it('validates random topics correctly', () => {
const topics = Array.from({ length: 100 }).map(() =>
buildDirectMessageTopicV2(
Buffer.from(crypto.getRandomValues(new Uint8Array(32)))
.toString('base64')
.replace(/=*$/g, '')
// Replace slashes with dashes so that the topic is still easily split by /
// We do not treat this as needing to be valid Base64 anywhere
.replace('/', '-')
)
)

topics.forEach((topic) => {
expect(isValidTopic(topic)).toBe(true)
})
})
})
})

0 comments on commit f3f94af

Please sign in to comment.