Skip to content

Commit

Permalink
StorageSubsystem.test: add tests for key/value storage
Browse files Browse the repository at this point in the history
  • Loading branch information
HerbCaudill committed Nov 1, 2023
1 parent 37d9fa4 commit 371ff59
Showing 1 changed file with 89 additions and 1 deletion.
90 changes: 89 additions & 1 deletion packages/automerge-repo/test/StorageSubsystem.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import { describe, it } from "vitest"
import { generateAutomergeUrl, parseAutomergeUrl } from "../src/AutomergeUrl.js"
import { StorageSubsystem } from "../src/storage/StorageSubsystem.js"
import { DummyStorageAdapter } from "./helpers/DummyStorageAdapter.js"
import { cbor } from "../src/index.js"
import { pause } from "../src/helpers/pause.js"

const tempDir = fs.mkdtempSync(path.join(os.tmpdir(), "automerge-repo-tests"))

Expand All @@ -19,7 +21,8 @@ describe("StorageSubsystem", () => {

Object.entries(adaptersToTest).forEach(([adapterName, adapter]) => {
describe(adapterName, () => {
it("can store and retrieve an Automerge document", async () => {
describe("Automerge document storage", () => {
it("stores and retrieves an Automerge document", async () => {
const storage = new StorageSubsystem(adapter)

const doc = A.change(A.init<any>(), "test", d => {
Expand Down Expand Up @@ -63,6 +66,91 @@ describe("StorageSubsystem", () => {

// save it to storage
storage2.saveDoc(key, changedDoc)
})

it("removes an Automerge document", async () => {
const storage = new StorageSubsystem(adapter)

const doc = A.change(A.init<any>(), "test", d => {
d.foo = "bar"
})

// save it to storage
const key = parseAutomergeUrl(generateAutomergeUrl()).documentId
await storage.saveDoc(key, doc)

// reload it from storage
const reloadedDoc = await storage.loadDoc(key)

// check that it's the same doc
assert.deepStrictEqual(reloadedDoc, doc)

// remove it
await storage.removeDoc(key)

// reload it from storage
const reloadedDoc2 = await storage.loadDoc(key)

// check that it's undefined
assert.equal(reloadedDoc2, undefined)
})
})

describe("Arbitrary key/value storage", () => {
it("stores and retrieves a blob", async () => {
const storage = new StorageSubsystem(adapter)

const value = cbor.encode({ foo: "bar" })

const namespace = "MyCoolAdapter"
const key = "ABC123"
await storage.save(namespace, key, value)

const reloadedValue = await storage.load(namespace, key)
assert.notEqual(reloadedValue, undefined)
assert.deepEqual(cbor.decode(reloadedValue)["foo"], "bar")
})

it("keeps namespaces separate", async () => {
const storage = new StorageSubsystem(adapter)

const key = "ABC123"

const namespace1 = "MyCoolAdapter"
const value1 = cbor.encode({ foo: "bar" })
await storage.save(namespace1, key, value1)

const namespace2 = "SomeDumbAdapter"
const value2 = cbor.encode({ baz: "pizza" })
await storage.save(namespace2, key, value2)

const reloadedValue1 = await storage.load(namespace1, key)
assert.notEqual(reloadedValue1, undefined)
assert.deepEqual(cbor.decode(reloadedValue1)["foo"], "bar")

const reloadedValue2 = await storage.load(namespace2, key)
assert.notEqual(reloadedValue2, undefined)
assert.deepEqual(cbor.decode(reloadedValue2)["baz"], "pizza")
})

it("removes a blob", async () => {
const storage = new StorageSubsystem(adapter)

const value = cbor.encode({ foo: "bar" })

const namespace = "MyCoolAdapter"
const key = "ABC123"
await storage.save(namespace, key, value)

const reloadedValue = await storage.load(namespace, key)
assert.notEqual(reloadedValue, undefined)
assert.deepEqual(cbor.decode(reloadedValue)["foo"], "bar")

await storage.remove(namespace, key)

const reloadedValue2 = await storage.load(namespace, key)
assert.equal(reloadedValue2, undefined)
})
})
})
})
Expand Down

0 comments on commit 371ff59

Please sign in to comment.