| … | … | @@ -0,0 +1,107 @@ |
| 1 | import test from 'brittle' |
| 2 | import fs from 'bare-fs' |
| 3 | import path from 'bare-path' |
| 4 | import os from 'bare-os' |
| 5 | import Corestore from 'corestore' |
| 6 | import { MediaStore } from '../../lib/media-store.js' |
| 7 | |
| 8 | function tmpSetup () { |
| 9 | const dir = path.join(os.tmpdir(), 'hn-ms-' + Date.now() + '-' + Math.random().toString(36).slice(2)) |
| 10 | fs.mkdirSync(dir, { recursive: true }) |
| 11 | const store = new Corestore(dir) |
| 12 | return { store, dir } |
| 13 | } |
| 14 | |
| 15 | function rmrf (dir) { |
| 16 | try { |
| 17 | for (const f of fs.readdirSync(dir)) { |
| 18 | const fp = path.join(dir, f) |
| 19 | const stat = fs.statSync(fp) |
| 20 | if (stat.isDirectory()) rmrf(fp) |
| 21 | else fs.unlinkSync(fp) |
| 22 | } |
| 23 | fs.rmdirSync(dir) |
| 24 | } catch {} |
| 25 | } |
| 26 | |
| 27 | test('MediaStore - put stores blob and returns sha256 hash', async (t) => { |
| 28 | const { store, dir } = tmpSetup() |
| 29 | const media = new MediaStore(store) |
| 30 | await media.ready() |
| 31 | |
| 32 | const buf = Buffer.from('hello world') |
| 33 | const hash = await media.put(buf, 'text/plain') |
| 34 | |
| 35 | t.is(typeof hash, 'string') |
| 36 | t.is(hash.length, 64) |
| 37 | |
| 38 | await store.close() |
| 39 | rmrf(dir) |
| 40 | }) |
| 41 | |
| 42 | test('MediaStore - get retrieves stored blob by hash', async (t) => { |
| 43 | const { store, dir } = tmpSetup() |
| 44 | const media = new MediaStore(store) |
| 45 | await media.ready() |
| 46 | |
| 47 | const buf = Buffer.from('test data for retrieval') |
| 48 | const hash = await media.put(buf, 'application/octet-stream') |
| 49 | const retrieved = await media.get(hash) |
| 50 | |
| 51 | t.alike(retrieved, buf) |
| 52 | |
| 53 | await store.close() |
| 54 | rmrf(dir) |
| 55 | }) |
| 56 | |
| 57 | test('MediaStore - has returns true for existing hash', async (t) => { |
| 58 | const { store, dir } = tmpSetup() |
| 59 | const media = new MediaStore(store) |
| 60 | await media.ready() |
| 61 | |
| 62 | const buf = Buffer.from('existence check') |
| 63 | const hash = await media.put(buf, 'text/plain') |
| 64 | |
| 65 | t.is(await media.has(hash), true) |
| 66 | t.is(await media.has('0'.repeat(64)), false) |
| 67 | |
| 68 | await store.close() |
| 69 | rmrf(dir) |
| 70 | }) |
| 71 | |
| 72 | test('MediaStore - dedup: same content returns same hash, stored once', async (t) => { |
| 73 | const { store, dir } = tmpSetup() |
| 74 | const media = new MediaStore(store) |
| 75 | await media.ready() |
| 76 | |
| 77 | const buf = Buffer.from('duplicate content') |
| 78 | const hash1 = await media.put(buf, 'text/plain') |
| 79 | const hash2 = await media.put(buf, 'text/plain') |
| 80 | |
| 81 | t.is(hash1, hash2) |
| 82 | |
| 83 | const info = await media.getInfo(hash1) |
| 84 | t.ok(info) |
| 85 | t.is(info.size, buf.length) |
| 86 | |
| 87 | await store.close() |
| 88 | rmrf(dir) |
| 89 | }) |
| 90 | |
| 91 | test('MediaStore - getInfo returns metadata', async (t) => { |
| 92 | const { store, dir } = tmpSetup() |
| 93 | const media = new MediaStore(store) |
| 94 | await media.ready() |
| 95 | |
| 96 | const buf = Buffer.from('metadata test') |
| 97 | const hash = await media.put(buf, 'image/jpeg') |
| 98 | const info = await media.getInfo(hash) |
| 99 | |
| 100 | t.is(info.size, buf.length) |
| 101 | t.is(info.mimeType, 'image/jpeg') |
| 102 | t.ok(info.createdAt) |
| 103 | t.ok(info.blobId) |
| 104 | |
| 105 | await store.close() |
| 106 | rmrf(dir) |
| 107 | }) |