Fix some tests

This commit is contained in:
Jon Staab
2025-04-08 10:17:30 -07:00
parent 1f7101daee
commit 74b20da8fb
11 changed files with 144 additions and 376 deletions
+2 -2
View File
@@ -85,7 +85,7 @@ describe("collection", () => {
})
await col.loadItem("1")
expect(mockLoad).toHaveBeenCalledWith("1")
expect(mockLoad).toHaveBeenCalledWith("1", [])
})
it("should handle concurrent loading of the same item", async () => {
@@ -212,7 +212,7 @@ describe("collection", () => {
})
col.deriveItem("1")
expect(mockLoad).toHaveBeenCalledWith("1")
expect(mockLoad).toHaveBeenCalledWith("1", [])
})
})
-200
View File
@@ -1,200 +0,0 @@
import {describe, it, expect, vi, beforeEach, afterEach} from "vitest"
import {writable, get} from "svelte/store"
import {Repository} from "@welshman/relay"
import {Tracker} from "@welshman/net"
import {
initStorage,
clearStorage,
storageAdapters,
dead,
getAll,
bulkPut,
bulkDelete,
} from "../src/storage"
describe("storage", () => {
const DB_NAME = "test-db"
const DB_VERSION = 1
beforeEach(async () => {
vi.clearAllMocks()
dead.set(false)
vi.useFakeTimers()
})
afterEach(async () => {
vi.useRealTimers()
await clearStorage()
// Clean up the test database
await new Promise((resolve, reject) => {
const req = indexedDB.deleteDatabase(DB_NAME)
req.onsuccess = () => resolve(undefined)
req.onerror = () => reject(req.error)
})
})
describe("basic operations", () => {
it("should initialize storage and store items", async () => {
const store = writable<{id: string; value: string}[]>([])
const adapters = {
items: storageAdapters.fromCollectionStore("id", store),
}
initStorage(DB_NAME, DB_VERSION, adapters)
await vi.runAllTimersAsync()
store.set([
{id: "1", value: "test1"},
{id: "2", value: "test2"},
])
const itemsPromise = getAll("items")
await vi.runAllTimersAsync()
const items = await itemsPromise
expect(items).toHaveLength(2)
expect(items).toContainEqual({id: "1", value: "test1"})
expect(items).toContainEqual({id: "2", value: "test2"})
})
it("should update items when store changes", async () => {
const store = writable<{id: string; value: string}[]>([])
const adapters = {
items: storageAdapters.fromCollectionStore("id", store),
}
initStorage(DB_NAME, DB_VERSION, adapters)
await vi.runAllTimersAsync()
// init storage with the first item
store.set([{id: "1", value: "test1"}])
store.update(items => [...items, {id: "2", value: "test2"}])
const itemsPromise = getAll("items")
await vi.runAllTimersAsync()
const items = await itemsPromise
expect(items).toHaveLength(2)
expect(items).toContainEqual({id: "2", value: "test2"})
})
it("should remove items when deleted from store", async () => {
const store = writable<{id: string; value: string}[]>()
const adapters = {
items: storageAdapters.fromCollectionStore("id", store),
}
initStorage(DB_NAME, DB_VERSION, adapters)
await vi.runAllTimersAsync()
store.set([
{id: "1", value: "test1"},
{id: "2", value: "test2"},
])
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"})
})
})
describe("storage adapters", () => {
it("should handle repository adapter", async () => {
const repository = new Repository()
const adapters = {
events: storageAdapters.fromRepository(repository),
}
initStorage(DB_NAME, DB_VERSION, adapters)
await vi.runAllTimersAsync()
const event = {
id: "test-id",
pubkey: "test-pubkey",
kind: 1,
created_at: 123,
content: "test",
tags: [],
}
repository.publish(event)
const eventsPromise = getAll("events")
await vi.runAllTimersAsync()
const events = await eventsPromise
expect(events).toContainEqual(event)
})
it("should handle tracker adapter", async () => {
const tracker = new Tracker()
const adapters = {
relays: storageAdapters.fromTracker(tracker),
}
initStorage(DB_NAME, DB_VERSION, adapters)
await vi.runAllTimersAsync()
tracker.track("event1", "relay1")
tracker.track("event1", "relay2")
const relaysPromise = getAll("relays")
await vi.runAllTimersAsync()
const relays = await relaysPromise
expect(relays).toContainEqual({
key: "event1",
value: ["relay1", "relay2"],
})
})
})
describe("error handling", () => {
it("should handle initialization errors", async () => {
const badAdapter = {
keyPath: undefined,
store: writable([]),
options: {},
}
const rejectPromise = initStorage(DB_NAME, DB_VERSION, {bad: badAdapter})
await vi.runAllTimersAsync()
// we can initialize storage with an undefined keypath
expect(rejectPromise).to.not.rejects
})
it("should prevent multiple initializations", async () => {
const adapters = {
test: {
keyPath: "id",
store: writable([]),
options: {},
},
}
initStorage(DB_NAME, DB_VERSION, adapters)
await vi.runAllTimersAsync()
await expect(initStorage(DB_NAME, DB_VERSION, adapters)).rejects.toThrow(
"Db initialized multiple times",
)
})
})
})
+11 -12
View File
@@ -15,7 +15,7 @@ import {
prepEvent,
publishThunk,
publishThunks,
thunkWorker,
thunkQueue,
walkThunks,
} from "../src/thunk"
@@ -34,12 +34,13 @@ describe("thunk", () => {
addSession({method: 'nip01', secret, pubkey})
})
afterEach(() => {
afterEach(async () => {
thunkQueue.stop()
thunkQueue.clear()
await vi.runAllTimersAsync()
vi.useRealTimers()
vi.clearAllMocks()
thunkWorker.clear()
thunkWorker.pause()
thunkWorker.resume()
thunkQueue.start()
dropSession(pubkey)
})
@@ -110,23 +111,21 @@ describe("thunk", () => {
it("should update status during publishing", async () => {
const send = vi.fn()
const track = vi.spyOn(tracker, 'track')
const statuses: Map<string, any> = new Map<string, any>()
const thunk = makeThunk(mockRequest)
let status = {}
// Subscribe to status updates
thunk.status.subscribe(status => {
for (const [key, value] of Object.entries(status)) {
statuses.set(key, value)
}
thunk.status.subscribe(_status => {
status = _status
})
// Start the publish process
thunkWorker.push(thunk)
thunkQueue.push(thunk)
// Wait for initial async operations
await vi.runAllTimersAsync()
expect(statuses.get(LOCAL_RELAY_URL)).toEqual({
expect(status[LOCAL_RELAY_URL]).toEqual({
status: PublishStatus.Success,
message: "",
})