| author | Stephen Paul Weber
<singpolyma@singpolyma.net> 2026-07-21 18:39:57 UTC |
| committer | Stephen Paul Weber
<singpolyma@singpolyma.net> 2026-07-21 18:48:54 UTC |
| parent | a86a7a480922b574704b63ca2189792f178dd477 |
| test/TestSqlite.hx | +23 | -17 |
| test/idb.spec.ts | +25 | -7 |
| test/sqlite.spec.ts | +26 | -7 |
diff --git a/test/TestSqlite.hx b/test/TestSqlite.hx index 8a5f219..646818e 100644 --- a/test/TestSqlite.hx +++ b/test/TestSqlite.hx @@ -25,6 +25,7 @@ import borogove.Member; import borogove.MemberUpdate; import borogove.Role; import borogove.Stanza; +import borogove.Source; using Lambda; using thenshim.PromiseTools; @@ -58,9 +59,8 @@ class MockMediaStore implements MediaStore { } } - public function hasMedia(hashAlgorithm:String, hash:BytesData): Promise<Bool> { - final hash = new Hash(hashAlgorithm, hash); - return getMediaPath(hash.toUri()).then(path -> path != null); + public function hasMedia(hash: Hash): Promise<Null<String>> { + return getMediaPath(hash.toUri()); } public function removeMedia(hashAlgorithm: String, hash: BytesData) { @@ -68,14 +68,20 @@ class MockMediaStore implements MediaStore { return getMediaPath(hash.toUri()).then(p -> kv.set(p, null)).then(_ -> true); } - public function storeMedia(mime: String, bd: BytesData): Promise<Bool> { - final bytes = Bytes.ofData(bd); - final sha1 = Hash.sha1(bytes); - final sha256 = Hash.sha256(bytes); - return thenshim.PromiseTools.all([ - kv.set(sha1.serializeUri(), sha256.serializeUri()), - kv.set(sha256.serializeUri(), mime) - ]).then(_ -> true); + public function storeMedia(mime: String, source: Source): Promise<Null<String>> { + return new Promise((resolve, reject) -> { + tink.io.Source.RealSourceTools.all(source).handle(o -> switch o { + case Success(bytes): resolve((bytes : Bytes)); + case Failure(e): reject(e); + }); + }).then(bytes -> { + final sha1 = Hash.sha1(bytes); + final sha256 = Hash.sha256(bytes); + return thenshim.PromiseTools.all([ + kv.set(sha1.serializeUri(), sha256.serializeUri()), + kv.set(sha256.serializeUri(), mime) + ]).then(_ -> "/path"); + }); } } @@ -751,16 +757,16 @@ class TestSqlite extends utest.Test { @:timeout(3000) public function testMedia(async: Async) { - final bytes = haxe.io.Bytes.ofString("hello").getData(); + final bytes = haxe.io.Bytes.ofString("hello"); persistence.storeMedia("image/png", bytes).then(_ -> { - return persistence.hasMedia("sha-256", Hash.sha256(haxe.io.Bytes.ofData(bytes)).hash); + return persistence.hasMedia(Hash.sha256(bytes)); }).then(hasBefore -> { - Assert.isTrue(hasBefore); - persistence.removeMedia("sha-256", Hash.sha256(haxe.io.Bytes.ofData(bytes)).hash); + Assert.equals("ni:///sha-256;LPJNul-wow4m6DsqxbninhsWHlwfp0JecwQzYpOLmCQ", hasBefore); + persistence.removeMedia("sha-256", Hash.sha256(bytes).hash); }).then(_ -> { - return persistence.hasMedia("sha-256", Hash.sha256(haxe.io.Bytes.ofData(bytes)).hash); + return persistence.hasMedia(Hash.sha256(bytes)); }).then(hasAfter -> { - Assert.isFalse(hasAfter); + Assert.isNull(hasAfter); async.done(); }).catchError(e -> { Assert.fail(Std.string(e)); diff --git a/test/idb.spec.ts b/test/idb.spec.ts index ed33b7b..64946e8 100644 --- a/test/idb.spec.ts +++ b/test/idb.spec.ts @@ -887,18 +887,36 @@ test("media storage functions", async ({ page }) => { const mediaStore = await borogove.persistence.MediaStoreCache("snikket"); const persistence = await borogove.persistence.IDB("snikket", mediaStore); - const buffer = new Uint8Array([1, 2, 3]).buffer; - await persistence.storeMedia("image/png", buffer); - const sha256 = await crypto.subtle.digest("SHA-256", buffer); - const hasBefore = await persistence.hasMedia("sha-256", sha256); + function bufferToByteStream(buffer) { + let done = false; + return new ReadableStream({ + type: "bytes", + async pull(controller) { + if (done) { + controller.close(); + } else { + controller.enqueue(buffer); + done = true; + } + }, + }); + } + + const buffer = new Uint8Array([1, 2, 3]); + const sha256 = await crypto.subtle.digest("SHA-256", buffer.buffer); + await persistence.storeMedia("image/png", bufferToByteStream(buffer)); + const hash = new borogove.Hash("sha-256", sha256); + const hasBefore = await persistence.hasMedia(hash); await persistence.removeMedia("sha-256", sha256); - const hasAfter = await persistence.hasMedia("sha-256", sha256); + const hasAfter = await persistence.hasMedia(hash); return { hasBefore, hasAfter }; }, code); - expect(result.hasBefore).toBe(true); - expect(result.hasAfter).toBe(false); + expect(result.hasBefore).toBe( + "/.well-known/ni/sha-256/A5BYxvLAy0ksUzsKTRTvd8wPeKvMztUofYShogEc-4E", + ); + expect(result.hasAfter).toBe(null); }); test("hydrate message with incomplete replyToMessage keys", async ({ diff --git a/test/sqlite.spec.ts b/test/sqlite.spec.ts index 6c10e0a..e443040 100644 --- a/test/sqlite.spec.ts +++ b/test/sqlite.spec.ts @@ -1385,12 +1385,29 @@ test.describe("not webkit", () => { ); try { - const buffer = new Uint8Array([1, 2, 3]).buffer; - await persistence.storeMedia("image/png", buffer); - const sha256 = await crypto.subtle.digest("SHA-256", buffer); - const hasBefore = await persistence.hasMedia("sha-256", sha256); + function bufferToByteStream(buffer) { + let done = false; + return new ReadableStream({ + type: "bytes", + async pull(controller) { + if (done) { + controller.close(); + } else { + controller.enqueue(buffer); + done = true; + } + }, + }); + } + + const buffer = new Uint8Array([1, 2, 3]); + const sha256 = await crypto.subtle.digest("SHA-256", buffer.buffer); + await persistence.storeMedia("image/png", bufferToByteStream(buffer)); + const hash = new borogove.Hash("sha-256", sha256); + const hasBefore = await persistence.hasMedia(hash); await persistence.removeMedia("sha-256", sha256); - const hasAfter = await persistence.hasMedia("sha-256", sha256); + const hasAfter = await persistence.hasMedia(hash); + return { hasBefore, hasAfter }; } catch (e) { console.error(e, e.result); @@ -1400,8 +1417,10 @@ test.describe("not webkit", () => { [code, sqlite, worker1], ); - expect(result.hasBefore).toBe(true); - expect(result.hasAfter).toBe(false); + expect(result.hasBefore).toBe( + "/.well-known/ni/sha-256/A5BYxvLAy0ksUzsKTRTvd8wPeKvMztUofYShogEc-4E", + ); + expect(result.hasAfter).toBe(null); }); test("hydrate message with incomplete replyToMessage", async ({ page }) => {