Fix docs/tests

This commit is contained in:
Jon Staab
2025-04-14 13:18:49 -07:00
parent e54cb0a62d
commit ded668890f
3 changed files with 19 additions and 41 deletions
+16 -38
View File
@@ -4,14 +4,13 @@ import {LOCAL_RELAY_URL} from "@welshman/relay"
import {getPubkey, makeSecret} from "@welshman/signer" import {getPubkey, makeSecret} from "@welshman/signer"
import {afterEach, beforeEach, describe, expect, it, vi} from "vitest" import {afterEach, beforeEach, describe, expect, it, vi} from "vitest"
import {repository, tracker} from "../src/core" import {repository, tracker} from "../src/core"
import {addSession, dropSession} from "../src/session" import {addSession, dropSession, makeNip01Session} from "../src/session"
import { import {
abortThunk, abortThunk,
makeThunk, Thunk,
mergeThunks, MergedThunk,
prepEvent, prepEvent,
publishThunk, publishThunk,
publishThunks,
thunkQueue, thunkQueue,
walkThunks, walkThunks,
} from "../src/thunk" } from "../src/thunk"
@@ -28,7 +27,7 @@ const mockRequest = {
describe("thunk", () => { describe("thunk", () => {
beforeEach(() => { beforeEach(() => {
vi.useFakeTimers() vi.useFakeTimers()
addSession({method: "nip01", secret, pubkey}) addSession(makeNip01Session(secret))
}) })
afterEach(async () => { afterEach(async () => {
@@ -41,11 +40,11 @@ describe("thunk", () => {
dropSession(pubkey) dropSession(pubkey)
}) })
describe("mergeThunks", () => { describe("MergedThunk", () => {
it("should abort all thunks when merged controller aborts", () => { it("should abort all thunks when merged controller aborts", () => {
const thunk1 = makeThunk(mockRequest) const thunk1 = new Thunk(mockRequest)
const thunk2 = makeThunk(mockRequest) const thunk2 = new Thunk(mockRequest)
const merged = mergeThunks([thunk1, thunk2]) const merged = new MergedThunk([thunk1, thunk2])
merged.controller.abort() merged.controller.abort()
@@ -56,9 +55,9 @@ describe("thunk", () => {
describe("walkThunks", () => { describe("walkThunks", () => {
it("should iterate through nested thunks", () => { it("should iterate through nested thunks", () => {
const thunk1 = makeThunk(mockRequest) const thunk1 = new Thunk(mockRequest)
const thunk2 = makeThunk(mockRequest) const thunk2 = new Thunk(mockRequest)
const merged = mergeThunks([thunk1, thunk2]) const merged = new MergedThunk([thunk1, thunk2])
const thunks = Array.from(walkThunks([merged, thunk1])) const thunks = Array.from(walkThunks([merged, thunk1]))
expect(thunks).toHaveLength(3) expect(thunks).toHaveLength(3)
@@ -72,7 +71,7 @@ describe("thunk", () => {
expect(publishSpy).toHaveBeenCalled() expect(publishSpy).toHaveBeenCalled()
expect(result).toHaveProperty("event") expect(result).toHaveProperty("event")
expect(result).toHaveProperty("request") expect(result).toHaveProperty("options")
}) })
it("should handle abort", () => { 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", () => { describe("abortThunk", () => {
it("should abort a thunk and clean up", () => { it("should abort a thunk and clean up", () => {
const thunk = makeThunk(mockRequest) const thunk = new Thunk(mockRequest)
abortThunk(thunk) abortThunk(thunk)
@@ -107,13 +96,7 @@ describe("thunk", () => {
it("should update status during publishing", async () => { it("should update status during publishing", async () => {
const track = vi.spyOn(tracker, "track") const track = vi.spyOn(tracker, "track")
const thunk = makeThunk(mockRequest) const thunk = new Thunk(mockRequest)
let status: Record<string, any> = {}
// Subscribe to status updates
thunk.status.subscribe(_status => {
status = _status
})
// Start the publish process // Start the publish process
thunkQueue.push(thunk) thunkQueue.push(thunk)
@@ -121,10 +104,7 @@ describe("thunk", () => {
// Wait for initial async operations // Wait for initial async operations
await vi.runAllTimersAsync() await vi.runAllTimersAsync()
expect(status[LOCAL_RELAY_URL]).toEqual({ expect(thunk.status[LOCAL_RELAY_URL]).toEqual(PublishStatus.Success)
status: PublishStatus.Success,
message: "",
})
// Verify tracker was called on success // Verify tracker was called on success
expect(track).toHaveBeenCalledWith(thunk.event.id, LOCAL_RELAY_URL) expect(track).toHaveBeenCalledWith(thunk.event.id, LOCAL_RELAY_URL)
@@ -132,8 +112,6 @@ describe("thunk", () => {
await vi.runAllTimersAsync() await vi.runAllTimersAsync()
const finalStatus = await thunk.result const finalStatus = await thunk.result
expect(finalStatus).toEqual({ expect(finalStatus).toEqual({[LOCAL_RELAY_URL]: PublishStatus.Success})
[LOCAL_RELAY_URL]: {status: PublishStatus.Success, message: ""},
})
}) })
}) })
+1 -1
View File
@@ -899,7 +899,7 @@ export type PollOptions = {
/** /**
* Creates a promise that resolves after the condition completes or timeout * Creates a promise that resolves after the condition completes or timeout
* @param options - PollOptions * @param options - PollOptions
* @returns Promise<void> * @returns void Promise
*/ */
export const poll = ({interval = 300, condition, signal}: PollOptions) => export const poll = ({interval = 300, condition, signal}: PollOptions) =>
new Promise<void>(resolve => { new Promise<void>(resolve => {
+2 -2
View File
@@ -40,7 +40,7 @@ describe("publishOne", () => {
await vi.runAllTimers() await vi.runAllTimers()
expect(successSpy).toHaveBeenCalledWith("hi", "1") expect(successSpy).toHaveBeenCalledWith("hi")
expect(failureSpy).not.toHaveBeenCalled() expect(failureSpy).not.toHaveBeenCalled()
expect(completeSpy).toHaveBeenCalled() expect(completeSpy).toHaveBeenCalled()
}) })
@@ -72,7 +72,7 @@ describe("publishOne", () => {
await vi.runAllTimers() await vi.runAllTimers()
expect(successSpy).not.toHaveBeenCalled() expect(successSpy).not.toHaveBeenCalled()
expect(failureSpy).toHaveBeenCalledWith("hi", "1") expect(failureSpy).toHaveBeenCalledWith("hi")
expect(completeSpy).toHaveBeenCalled() expect(completeSpy).toHaveBeenCalled()
}) })