mirror of
https://github.com/nbd-wtf/nostr-tools.git
synced 2025-12-08 16:28:49 +00:00
edit filter.test.js and add more test cases (#165)
* add more test cases for event file * add another test case for event file * edit filter.test.js and add more test cases
This commit is contained in:
204
filter.test.js
204
filter.test.js
@@ -1,42 +1,176 @@
|
|||||||
/* eslint-env jest */
|
/* eslint-env jest */
|
||||||
|
|
||||||
const {matchFilters} = require('./lib/nostr.cjs')
|
const {matchFilter, matchFilters} = require('./lib/nostr.cjs.js')
|
||||||
|
|
||||||
test('test if filters match', () => {
|
describe('Filter', () => {
|
||||||
;[
|
describe('matchFilter', () => {
|
||||||
{
|
it('should return true when all filter conditions are met', () => {
|
||||||
filters: [{ids: ['i']}],
|
const filter = {
|
||||||
good: [{id: 'i'}],
|
ids: ['123', '456'],
|
||||||
bad: [{id: 'j'}]
|
kinds: [1, 2, 3],
|
||||||
},
|
authors: ['abc'],
|
||||||
{
|
since: 100,
|
||||||
filters: [{authors: ['abc']}, {kinds: [1, 3]}],
|
until: 200,
|
||||||
good: [
|
'#tag': ['value']
|
||||||
{pubkey: 'xyz', kind: 3},
|
}
|
||||||
{pubkey: 'abc', kind: 12},
|
|
||||||
{pubkey: 'abc', kind: 1}
|
const event = {
|
||||||
],
|
id: '123',
|
||||||
bad: [{pubkey: 'hhh', kind: 12}]
|
kind: 1,
|
||||||
},
|
pubkey: 'abc',
|
||||||
{
|
created_at: 150,
|
||||||
filters: [{'#e': ['yyy'], since: 444}],
|
tags: [['tag', 'value']]
|
||||||
good: [
|
}
|
||||||
{
|
|
||||||
tags: [
|
const result = matchFilter(filter, event)
|
||||||
['e', 'uuu'],
|
|
||||||
['e', 'yyy']
|
expect(result).toEqual(true)
|
||||||
],
|
|
||||||
created_at: 555
|
|
||||||
}
|
|
||||||
],
|
|
||||||
bad: [{tags: [['e', 'uuu']], created_at: 111}]
|
|
||||||
}
|
|
||||||
].forEach(({filters, good, bad}) => {
|
|
||||||
good.forEach(ev => {
|
|
||||||
expect(matchFilters(filters, ev)).toBeTruthy()
|
|
||||||
})
|
})
|
||||||
bad.forEach(ev => {
|
|
||||||
expect(matchFilters(filters, ev)).toBeFalsy()
|
it('should return false when the event id is not in the filter', () => {
|
||||||
|
const filter = {ids: ['123', '456']}
|
||||||
|
|
||||||
|
const event = {id: '789'}
|
||||||
|
|
||||||
|
const result = matchFilter(filter, event)
|
||||||
|
|
||||||
|
expect(result).toEqual(false)
|
||||||
|
})
|
||||||
|
|
||||||
|
it('should return false when the event kind is not in the filter', () => {
|
||||||
|
const filter = {kinds: [1, 2, 3]}
|
||||||
|
|
||||||
|
const event = {kind: 4}
|
||||||
|
|
||||||
|
const result = matchFilter(filter, event)
|
||||||
|
|
||||||
|
expect(result).toEqual(false)
|
||||||
|
})
|
||||||
|
|
||||||
|
it('should return false when the event author is not in the filter', () => {
|
||||||
|
const filter = {authors: ['abc', 'def']}
|
||||||
|
|
||||||
|
const event = {pubkey: 'ghi'}
|
||||||
|
|
||||||
|
const result = matchFilter(filter, event)
|
||||||
|
|
||||||
|
expect(result).toEqual(false)
|
||||||
|
})
|
||||||
|
|
||||||
|
it('should return false when a tag is not present in the event', () => {
|
||||||
|
const filter = {'#tag': ['value1', 'value2']}
|
||||||
|
|
||||||
|
const event = {tags: [['not_tag', 'value1']]}
|
||||||
|
|
||||||
|
const result = matchFilter(filter, event)
|
||||||
|
|
||||||
|
expect(result).toEqual(false)
|
||||||
|
})
|
||||||
|
|
||||||
|
it('should return false when a tag value is not present in the event', () => {
|
||||||
|
const filter = {'#tag': ['value1', 'value2']}
|
||||||
|
|
||||||
|
const event = {tags: [['tag', 'value3']]}
|
||||||
|
|
||||||
|
const result = matchFilter(filter, event)
|
||||||
|
|
||||||
|
expect(result).toEqual(false)
|
||||||
|
})
|
||||||
|
|
||||||
|
it('should return true when filter has tags that is present in the event', () => {
|
||||||
|
const filter = {'#tag1': ['foo']}
|
||||||
|
|
||||||
|
const event = {
|
||||||
|
id: '123',
|
||||||
|
kind: 1,
|
||||||
|
pubkey: 'abc',
|
||||||
|
created_at: 150,
|
||||||
|
tags: [
|
||||||
|
['tag1', 'foo'],
|
||||||
|
['tag2', 'bar']
|
||||||
|
]
|
||||||
|
}
|
||||||
|
|
||||||
|
const result = matchFilter(filter, event)
|
||||||
|
|
||||||
|
expect(result).toEqual(true)
|
||||||
|
})
|
||||||
|
|
||||||
|
it('should return false when the event is before the filter since value', () => {
|
||||||
|
const filter = {since: 100}
|
||||||
|
|
||||||
|
const event = {created_at: 50}
|
||||||
|
|
||||||
|
const result = matchFilter(filter, event)
|
||||||
|
|
||||||
|
expect(result).toEqual(false)
|
||||||
|
})
|
||||||
|
|
||||||
|
it('should return false when the event is after the filter until value', () => {
|
||||||
|
const filter = {until: 100}
|
||||||
|
|
||||||
|
const event = {created_at: 150}
|
||||||
|
|
||||||
|
const result = matchFilter(filter, event)
|
||||||
|
|
||||||
|
expect(result).toEqual(false)
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
describe('matchFilters', () => {
|
||||||
|
it('should return true when at least one filter matches the event', () => {
|
||||||
|
const filters = [
|
||||||
|
{ids: ['123'], kinds: [1], authors: ['abc']},
|
||||||
|
{ids: ['456'], kinds: [2], authors: ['def']},
|
||||||
|
{ids: ['789'], kinds: [3], authors: ['ghi']}
|
||||||
|
]
|
||||||
|
|
||||||
|
const event = {id: '789', kind: 3, pubkey: 'ghi'}
|
||||||
|
|
||||||
|
const result = matchFilters(filters, event)
|
||||||
|
|
||||||
|
expect(result).toEqual(true)
|
||||||
|
})
|
||||||
|
|
||||||
|
it('should return true when event matches one or more filters and some have limit set', () => {
|
||||||
|
const filters = [
|
||||||
|
{ids: ['123'], limit: 1},
|
||||||
|
{kinds: [1], limit: 2},
|
||||||
|
{authors: ['abc'], limit: 3}
|
||||||
|
]
|
||||||
|
|
||||||
|
const event = {id: '123', kind: 1, pubkey: 'abc', created_at: 150}
|
||||||
|
|
||||||
|
const result = matchFilters(filters, event)
|
||||||
|
|
||||||
|
expect(result).toEqual(true)
|
||||||
|
})
|
||||||
|
|
||||||
|
it('should return false when no filters match the event', () => {
|
||||||
|
const filters = [
|
||||||
|
{ids: ['123'], kinds: [1], authors: ['abc']},
|
||||||
|
{ids: ['456'], kinds: [2], authors: ['def']},
|
||||||
|
{ids: ['789'], kinds: [3], authors: ['ghi']}
|
||||||
|
]
|
||||||
|
|
||||||
|
const event = {id: '100', kind: 4, pubkey: 'jkl'}
|
||||||
|
|
||||||
|
const result = matchFilters(filters, event)
|
||||||
|
|
||||||
|
expect(result).toEqual(false)
|
||||||
|
})
|
||||||
|
|
||||||
|
it('should return false when event matches none of the filters and some have limit set', () => {
|
||||||
|
const filters = [
|
||||||
|
{ids: ['123'], limit: 1},
|
||||||
|
{kinds: [1], limit: 2},
|
||||||
|
{authors: ['abc'], limit: 3}
|
||||||
|
]
|
||||||
|
const event = {id: '456', kind: 2, pubkey: 'def', created_at: 200}
|
||||||
|
|
||||||
|
const result = matchFilters(filters, event)
|
||||||
|
|
||||||
|
expect(result).toEqual(false)
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|||||||
Reference in New Issue
Block a user