-
Notifications
You must be signed in to change notification settings - Fork 229
test: add NIP-22 created_at integration coverage #547
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
cameri
merged 2 commits into
cameri:main
from
Priyanshubhartistm:feat/integration-tests-nip-22-timestamp-limits
Apr 22, 2026
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| --- | ||
| "nostream": patch | ||
| --- | ||
|
|
||
| Improve NIP-22 `created_at` limit handling coverage and boundary reliability. | ||
|
|
||
| This adds integration coverage for accepted and rejected events across configured positive and negative `created_at` deltas, and keeps rejection semantics consistent (`rejected`) for out-of-range timestamps. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| @nip-22 | ||
| Feature: NIP-22 created_at timestamp limits | ||
| Scenario: Event with created_at at current time is accepted | ||
| Given someone called Alice | ||
| And created_at limits are set to maxPositiveDelta 900 and maxNegativeDelta 0 | ||
| When Alice drafts a text_note event with content "test event" and created_at 0 seconds from now | ||
| Then Alice sends their last draft event successfully | ||
| When Alice subscribes to author Alice | ||
| Then Alice receives a text_note event from Alice with content "test event" | ||
|
|
||
| Scenario: Event with created_at above positive delta limit is rejected | ||
| Given someone called Alice | ||
| And created_at limits are set to maxPositiveDelta 900 and maxNegativeDelta 0 | ||
| When Alice drafts a text_note event with content "test event" and created_at 910 seconds from now | ||
| Then Alice sends their last draft event unsuccessfully with reason containing "rejected" | ||
|
|
||
| Scenario: Event older than configured negative delta limit is rejected | ||
| Given someone called Alice | ||
| And created_at limits are set to maxPositiveDelta 900 and maxNegativeDelta 3600 | ||
| When Alice drafts a text_note event with content "test event" and created_at -3601 seconds from now | ||
| Then Alice sends their last draft event unsuccessfully with reason containing "rejected" | ||
|
|
||
| Scenario: Event within configured negative delta limit is accepted | ||
| Given someone called Alice | ||
| And created_at limits are set to maxPositiveDelta 900 and maxNegativeDelta 3600 | ||
| When Alice drafts a text_note event with content "test event" and created_at -3590 seconds from now | ||
| Then Alice sends their last draft event successfully | ||
| When Alice subscribes to author Alice | ||
| Then Alice receives a text_note event from Alice with content "test event" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,104 @@ | ||
| import { After, Before, Given, Then, When } from '@cucumber/cucumber' | ||
| import { assocPath, pipe } from 'ramda' | ||
|
|
||
| import { CommandResult, MessageType } from '../../../../src/@types/messages' | ||
| import { createEvent, sendEvent } from '../helpers' | ||
|
|
||
| import { Event } from '../../../../src/@types/event' | ||
| import { expect } from 'chai' | ||
| import { isDraft } from '../shared' | ||
| import { SettingsStatic } from '../../../../src/utils/settings' | ||
| import WebSocket from 'ws' | ||
|
|
||
| const previousSettingsSnapshot = Symbol('nip22PreviousSettingsSnapshot') | ||
| const draftOffsetSeconds = Symbol('nip22DraftOffsetSeconds') | ||
|
|
||
| const setCreatedAtLimits = (maxPositiveDelta: number, maxNegativeDelta: number) => { | ||
| const settings = SettingsStatic._settings ?? SettingsStatic.createSettings() | ||
|
|
||
| SettingsStatic._settings = pipe( | ||
| assocPath(['limits', 'event', 'createdAt', 'maxPositiveDelta'], maxPositiveDelta), | ||
| assocPath(['limits', 'event', 'createdAt', 'maxNegativeDelta'], maxNegativeDelta), | ||
| )(settings) as any | ||
| } | ||
|
|
||
| Before({ tags: '@nip-22' }, function(this: any) { | ||
| this[previousSettingsSnapshot] = SettingsStatic._settings | ||
| }) | ||
|
|
||
| After({ tags: '@nip-22' }, function(this: any) { | ||
| SettingsStatic._settings = this[previousSettingsSnapshot] | ||
| delete this[previousSettingsSnapshot] | ||
| }) | ||
|
|
||
| Given(/^created_at limits are set to maxPositiveDelta (\d+) and maxNegativeDelta (\d+)$/, function( | ||
| maxPositiveDelta: string, | ||
| maxNegativeDelta: string, | ||
| ) { | ||
| setCreatedAtLimits(Number(maxPositiveDelta), Number(maxNegativeDelta)) | ||
| }) | ||
|
|
||
| When(/^(\w+) drafts a text_note event with content "([^"]+)" and created_at (-?\d+) seconds from now$/, async function( | ||
| name: string, | ||
| content: string, | ||
| offsetSeconds: string, | ||
| ) { | ||
| const { pubkey, privkey } = this.parameters.identities[name] | ||
| const createdAt = Math.floor(Date.now() / 1000) + Number(offsetSeconds) | ||
|
|
||
| const event: Event = await createEvent( | ||
| { | ||
| pubkey, | ||
| kind: 1, | ||
| content, | ||
| created_at: createdAt, | ||
|
cameri marked this conversation as resolved.
|
||
| }, | ||
| privkey, | ||
| ) | ||
|
|
||
| const draftEvent = event as any | ||
| draftEvent[isDraft] = true | ||
| draftEvent[draftOffsetSeconds] = Number(offsetSeconds) | ||
|
|
||
| this.parameters.events[name].push(event) | ||
| }) | ||
|
|
||
| Then(/^(\w+) sends their last draft event unsuccessfully with reason containing "([^"]+)"$/, async function( | ||
| name: string, | ||
| expectedReason: string, | ||
| ) { | ||
| const ws = this.parameters.clients[name] as WebSocket | ||
|
|
||
| const event = this.parameters.events[name].findLast((lastEvent: Event) => (lastEvent as any)[isDraft]) | ||
| if (!event) { | ||
| throw new Error(`No draft event found for ${name}`) | ||
| } | ||
|
|
||
| const draftEvent = event as any | ||
| const offsetSeconds = draftEvent[draftOffsetSeconds] | ||
|
|
||
| let eventToSend = event | ||
| if (typeof offsetSeconds === 'number') { | ||
| const { pubkey, privkey } = this.parameters.identities[name] | ||
| const createdAt = Math.floor(Date.now() / 1000) + offsetSeconds | ||
|
|
||
| eventToSend = await createEvent( | ||
| { | ||
| pubkey, | ||
| kind: event.kind, | ||
| content: event.content, | ||
| created_at: createdAt, | ||
| }, | ||
| privkey, | ||
| ) | ||
| } | ||
|
|
||
| delete draftEvent[isDraft] | ||
| delete draftEvent[draftOffsetSeconds] | ||
|
|
||
| const command = await sendEvent(ws, eventToSend, false) as CommandResult | ||
|
|
||
| expect(command[0]).to.equal(MessageType.OK) | ||
| expect(command[2]).to.equal(false) | ||
| expect(command[3].toLowerCase()).to.contain(expectedReason.toLowerCase()) | ||
| }) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
@nip-22After hook resets created_at limits to hard-coded defaults (900/0). Integration tests globally overridelimits.event.createdAt.maxPositiveDeltato 0 intest/integration/features/shared.tsto avoid time-based flakiness; restoring to 900 here will leak settings changes into subsequent scenarios/features and can cause flaky failures. Snapshot the previous settings (or previous createdAt limits) before modifying, and restore that snapshot in After instead of hard-coding values.