Skip to content

Conversation

@digitalbase
Copy link
Collaborator

Went down a rabbit hole in NDK 3.x with a bug around fetching user profile data (and zap methods). See #363

To reproduce i decided to improve the user tests and create a case that failed/showed the problem.

This PR has:

The last item i think proves the bug

it("duplicate fetching of profile", async () => {
            const event = EventGenerator.createEvent(
                0,
                JSON.stringify({
                    name: "Jeff",
                    picture: "https://image.url",
                }),
                pubkey,
            );
            event.created_at = NOW_SEC - 7200;

            ndk.fetchEvent = vi.fn().mockResolvedValueOnce(event);

            const profile = await user.fetchProfile();
            expect(profile?.name).toEqual("Jeff");
            expect(profile?.picture).toEqual("https://image.url");

            const profile2 = await user.fetchProfile();
            expect(profile2?.name).toEqual("Jeff");
            expect(profile2?.picture).toEqual("https://image.url");
        });

- only check if NDK instace is set once
- extract pubkey code in method
- use nostr-tool kinds (instead of numbers)
- improve types for pubkeys
 (no Date.now() in assertions/setup)
Using Math.floor(Date.now()/1000) introduces nondeterminism and can produce flaky tests (especially if logic compares timestamps or caches results). Use a fixed timestamp per suite/test:
@digitalbase digitalbase changed the title Improve user tests Fix src/user tests Dec 15, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants