From ded668890fbccba2fb0ddc37850e291848ebd2db Mon Sep 17 00:00:00 2001 From: Jon Staab Date: Mon, 14 Apr 2025 13:18:49 -0700 Subject: [PATCH] Fix docs/tests --- packages/app/__tests__/thunk.test.ts | 54 ++++++++------------------ packages/lib/src/Tools.ts | 2 +- packages/net/__tests__/publish.test.ts | 4 +- 3 files changed, 19 insertions(+), 41 deletions(-) diff --git a/packages/app/__tests__/thunk.test.ts b/packages/app/__tests__/thunk.test.ts index 41b818a..95d83ef 100644 --- a/packages/app/__tests__/thunk.test.ts +++ b/packages/app/__tests__/thunk.test.ts @@ -4,14 +4,13 @@ import {LOCAL_RELAY_URL} from "@welshman/relay" import {getPubkey, makeSecret} from "@welshman/signer" import {afterEach, beforeEach, describe, expect, it, vi} from "vitest" import {repository, tracker} from "../src/core" -import {addSession, dropSession} from "../src/session" +import {addSession, dropSession, makeNip01Session} from "../src/session" import { abortThunk, - makeThunk, - mergeThunks, + Thunk, + MergedThunk, prepEvent, publishThunk, - publishThunks, thunkQueue, walkThunks, } from "../src/thunk" @@ -28,7 +27,7 @@ const mockRequest = { describe("thunk", () => { beforeEach(() => { vi.useFakeTimers() - addSession({method: "nip01", secret, pubkey}) + addSession(makeNip01Session(secret)) }) afterEach(async () => { @@ -41,11 +40,11 @@ describe("thunk", () => { dropSession(pubkey) }) - describe("mergeThunks", () => { + describe("MergedThunk", () => { it("should abort all thunks when merged controller aborts", () => { - const thunk1 = makeThunk(mockRequest) - const thunk2 = makeThunk(mockRequest) - const merged = mergeThunks([thunk1, thunk2]) + const thunk1 = new Thunk(mockRequest) + const thunk2 = new Thunk(mockRequest) + const merged = new MergedThunk([thunk1, thunk2]) merged.controller.abort() @@ -56,9 +55,9 @@ describe("thunk", () => { describe("walkThunks", () => { it("should iterate through nested thunks", () => { - const thunk1 = makeThunk(mockRequest) - const thunk2 = makeThunk(mockRequest) - const merged = mergeThunks([thunk1, thunk2]) + const thunk1 = new Thunk(mockRequest) + const thunk2 = new Thunk(mockRequest) + const merged = new MergedThunk([thunk1, thunk2]) const thunks = Array.from(walkThunks([merged, thunk1])) expect(thunks).toHaveLength(3) @@ -72,7 +71,7 @@ describe("thunk", () => { expect(publishSpy).toHaveBeenCalled() expect(result).toHaveProperty("event") - expect(result).toHaveProperty("request") + expect(result).toHaveProperty("options") }) it("should handle abort", () => { @@ -85,19 +84,9 @@ describe("thunk", () => { }) }) - describe("publishThunks", () => { - it("should publish multiple thunks", () => { - const requests = [mockRequest, mockRequest] - const result = publishThunks(requests) - - expect(repository.publish).toHaveBeenCalledTimes(2) - expect(result.thunks).toHaveLength(2) - }) - }) - describe("abortThunk", () => { it("should abort a thunk and clean up", () => { - const thunk = makeThunk(mockRequest) + const thunk = new Thunk(mockRequest) abortThunk(thunk) @@ -107,13 +96,7 @@ describe("thunk", () => { it("should update status during publishing", async () => { const track = vi.spyOn(tracker, "track") - const thunk = makeThunk(mockRequest) - let status: Record = {} - - // Subscribe to status updates - thunk.status.subscribe(_status => { - status = _status - }) + const thunk = new Thunk(mockRequest) // Start the publish process thunkQueue.push(thunk) @@ -121,10 +104,7 @@ describe("thunk", () => { // Wait for initial async operations await vi.runAllTimersAsync() - expect(status[LOCAL_RELAY_URL]).toEqual({ - status: PublishStatus.Success, - message: "", - }) + expect(thunk.status[LOCAL_RELAY_URL]).toEqual(PublishStatus.Success) // Verify tracker was called on success expect(track).toHaveBeenCalledWith(thunk.event.id, LOCAL_RELAY_URL) @@ -132,8 +112,6 @@ describe("thunk", () => { await vi.runAllTimersAsync() const finalStatus = await thunk.result - expect(finalStatus).toEqual({ - [LOCAL_RELAY_URL]: {status: PublishStatus.Success, message: ""}, - }) + expect(finalStatus).toEqual({[LOCAL_RELAY_URL]: PublishStatus.Success}) }) }) diff --git a/packages/lib/src/Tools.ts b/packages/lib/src/Tools.ts index 0eea6fb..a6685c5 100644 --- a/packages/lib/src/Tools.ts +++ b/packages/lib/src/Tools.ts @@ -899,7 +899,7 @@ export type PollOptions = { /** * Creates a promise that resolves after the condition completes or timeout * @param options - PollOptions - * @returns Promise + * @returns void Promise */ export const poll = ({interval = 300, condition, signal}: PollOptions) => new Promise(resolve => { diff --git a/packages/net/__tests__/publish.test.ts b/packages/net/__tests__/publish.test.ts index 6ee88ff..ccb665d 100644 --- a/packages/net/__tests__/publish.test.ts +++ b/packages/net/__tests__/publish.test.ts @@ -40,7 +40,7 @@ describe("publishOne", () => { await vi.runAllTimers() - expect(successSpy).toHaveBeenCalledWith("hi", "1") + expect(successSpy).toHaveBeenCalledWith("hi") expect(failureSpy).not.toHaveBeenCalled() expect(completeSpy).toHaveBeenCalled() }) @@ -72,7 +72,7 @@ describe("publishOne", () => { await vi.runAllTimers() expect(successSpy).not.toHaveBeenCalled() - expect(failureSpy).toHaveBeenCalledWith("hi", "1") + expect(failureSpy).toHaveBeenCalledWith("hi") expect(completeSpy).toHaveBeenCalled() })