more tests
This commit is contained in:
@@ -4,6 +4,7 @@ import {afterEach, beforeEach, describe, expect, it, vi} from "vitest"
|
||||
import {follow, mute, pin, unfollow, unmute, unpin} from "../src/commands"
|
||||
import * as thunkModule from "../src/thunk"
|
||||
import {thunkWorker} from "../src/thunk"
|
||||
import {repository} from "../src/core"
|
||||
|
||||
vi.mock(import("@welshman/lib"), async importOriginal => ({
|
||||
...(await importOriginal()),
|
||||
@@ -48,6 +49,8 @@ describe("commands", () => {
|
||||
vi.resetModules()
|
||||
// Clear any cached data
|
||||
vi.clearAllMocks()
|
||||
|
||||
repository.load([])
|
||||
})
|
||||
|
||||
afterEach(() => {
|
||||
@@ -64,6 +67,7 @@ describe("commands", () => {
|
||||
it("should create new follows list if none exists", async () => {
|
||||
const publishThunkSpy = vi.spyOn(thunkModule, "publishThunk")
|
||||
await follow(["p", pubkey1])
|
||||
await vi.runAllTimersAsync()
|
||||
|
||||
expect(publishThunkSpy).toHaveBeenCalledWith(
|
||||
expect.objectContaining({
|
||||
@@ -85,6 +89,8 @@ describe("commands", () => {
|
||||
|
||||
await follow(["p", pubkey2])
|
||||
|
||||
await vi.runAllTimersAsync()
|
||||
|
||||
expect(publishThunkSpy).toHaveBeenCalledWith(
|
||||
expect.objectContaining({
|
||||
event: expect.objectContaining({
|
||||
@@ -126,6 +132,8 @@ describe("commands", () => {
|
||||
|
||||
await mute(["p", pubkey1])
|
||||
|
||||
await vi.runAllTimersAsync()
|
||||
|
||||
expect(publishThunkSpy).toHaveBeenCalledWith({
|
||||
event: expect.objectContaining({
|
||||
kind: MUTES,
|
||||
@@ -144,6 +152,8 @@ describe("commands", () => {
|
||||
|
||||
await mute(["p", pubkey2])
|
||||
|
||||
await vi.runAllTimersAsync()
|
||||
|
||||
expect(publishThunkSpy).toHaveBeenCalledWith(
|
||||
expect.objectContaining({
|
||||
event: expect.objectContaining({
|
||||
@@ -184,6 +194,8 @@ describe("commands", () => {
|
||||
const publishThunkSpy = vi.spyOn(thunkModule, "publishThunk")
|
||||
await pin(["e", event1])
|
||||
|
||||
await vi.runAllTimersAsync()
|
||||
|
||||
expect(publishThunkSpy).toHaveBeenCalledWith(
|
||||
expect.objectContaining({
|
||||
event: expect.objectContaining({
|
||||
@@ -204,6 +216,8 @@ describe("commands", () => {
|
||||
|
||||
await pin(["e", event2])
|
||||
|
||||
await vi.runAllTimersAsync()
|
||||
|
||||
expect(publishThunkSpy).toHaveBeenCalledWith(
|
||||
expect.objectContaining({
|
||||
event: expect.objectContaining({
|
||||
|
||||
@@ -4,7 +4,6 @@ import {Repository} from "@welshman/util"
|
||||
import {Tracker} from "@welshman/net"
|
||||
import {
|
||||
initStorage,
|
||||
closeStorage,
|
||||
clearStorage,
|
||||
storageAdapters,
|
||||
dead,
|
||||
@@ -25,7 +24,7 @@ describe("storage", () => {
|
||||
|
||||
afterEach(async () => {
|
||||
vi.useRealTimers()
|
||||
await closeStorage()
|
||||
await clearStorage()
|
||||
// Clean up the test database
|
||||
await new Promise((resolve, reject) => {
|
||||
const req = indexedDB.deleteDatabase(DB_NAME)
|
||||
@@ -99,9 +98,12 @@ describe("storage", () => {
|
||||
|
||||
store.update(items => items.filter(item => item.id !== "1"))
|
||||
|
||||
await vi.runAllTimersAsync()
|
||||
|
||||
const itemsPromise = getAll("items")
|
||||
await vi.runAllTimersAsync()
|
||||
const items = await itemsPromise
|
||||
await vi.runAllTimersAsync()
|
||||
|
||||
expect(items).toHaveLength(1)
|
||||
expect(items[0]).toEqual({id: "2", value: "test2"})
|
||||
|
||||
@@ -18,7 +18,7 @@ import {
|
||||
|
||||
// Mock dependencies
|
||||
vi.mock("@welshman/net", () => ({
|
||||
publish: vi.fn(),
|
||||
publish: vi.fn().mockReturnValue({emitter: {on: vi.fn()}}),
|
||||
PublishStatus: {
|
||||
Pending: "pending",
|
||||
Success: "success",
|
||||
@@ -269,8 +269,9 @@ describe("thunkWorker", async () => {
|
||||
|
||||
it("should handle publish failures", async () => {
|
||||
const mockSigner = {
|
||||
sign: vi.fn().mockRejectedValue(new Error("Signing failed")),
|
||||
sign: vi.fn().mockRejectedValue("Signing failed"),
|
||||
}
|
||||
|
||||
vi.mocked(sessionModule.getSigner).mockReturnValue(mockSigner)
|
||||
|
||||
const thunk = makeThunk(mockRequest)
|
||||
|
||||
@@ -141,6 +141,7 @@ export const closeStorage = async () => {
|
||||
export const clearStorage = async () => {
|
||||
await closeStorage()
|
||||
await deleteDB(db.name)
|
||||
db = undefined // force initStorage to run again
|
||||
}
|
||||
|
||||
const migrate = (data: any[], options: StorageAdapterOptions) =>
|
||||
|
||||
@@ -185,16 +185,30 @@ thunkWorker.addGlobalHandler((thunk: Thunk) => {
|
||||
|
||||
// Avoid making this function async so multiple publishes can run concurrently
|
||||
Promise.resolve().then(async () => {
|
||||
const fail = (message: string) => {
|
||||
const status = new Map<string, ThunkStatus>()
|
||||
|
||||
for (const url of thunk.request.relays) {
|
||||
status.set(url, {status: PublishStatus.Failed, message})
|
||||
}
|
||||
|
||||
thunk.status.set(status)
|
||||
}
|
||||
|
||||
// If the event was already signed, leave it alone. Otherwise, sign it now. This is to
|
||||
// decrease apparent latency in the UI that results from waiting for remote signers
|
||||
if (!isSignedEvent(event)) {
|
||||
const signer = getSigner(getSession(event.pubkey))
|
||||
|
||||
if (!signer) {
|
||||
return console.warn(`No signer found for ${event.pubkey}`)
|
||||
return fail(`No signer found for ${event.pubkey}`)
|
||||
}
|
||||
|
||||
event = await signer.sign(event)
|
||||
try {
|
||||
event = await signer.sign(event)
|
||||
} catch (e) {
|
||||
return fail(e.toString())
|
||||
}
|
||||
}
|
||||
|
||||
// We're guaranteed to have a signed event at this point
|
||||
|
||||
Reference in New Issue
Block a user