Skip to content

Edge-Drop is a clipboard shelf on the edge of your screen. Hover to open it. Drag files and images into any app, click to copy text and links. Free and open-source for Windows.

fix(store): never lose saved history that cannot be read

Deepender25918865142f7c
diff --git a/electron/store/ItemStore.ts b/electron/store/ItemStore.ts
index 3e59e34..ba24f4b 100644
--- a/electron/store/ItemStore.ts
+++ b/electron/store/ItemStore.ts
@@ -10,7 +10,7 @@
* - Persist the index to JSON and image bytes to per-item PNG files.
* - Convert internal items to the serializable DTO form for the renderer.
*/
-import { existsSync, readFileSync, writeFileSync, rmSync, statSync, readdirSync } from 'node:fs'
+import { existsSync, readFileSync, writeFileSync, rmSync, statSync, readdirSync, openSync, writeSync, fsyncSync, closeSync, renameSync } from 'node:fs'
import { join, extname, basename as pathBasename } from 'node:path'
import { nativeImage, safeStorage } from 'electron'
import { thumbnailUrlForFile, thumbnailUrlForStoredImage } from '../main/imageProtocol'
@@ -36,11 +36,73 @@ function applyNote(it: ClipboardItem, note: string | undefined): void {
else delete it.note
}
+/**
+ * Replace a file only once its new contents are on disk: written to a side file, flushed, then
+ * swapped in. A crash or power cut mid-save leaves the previous file, never a cut-off or empty one.
+ */
+function writeFileAtomic(file: string, data: string): void {
+ const tmp = `${file}.tmp`
+ const fd = openSync(tmp, 'w')
+ try {
+ writeSync(fd, data, null, 'utf8')
+ fsyncSync(fd)
+ } finally {
+ closeSync(fd)
+ }
+ renameSync(tmp, file)
+}
+
+/** Read a file, retrying briefly while another process (antivirus, backup) holds it. */
+function readFileRetrying(file: string): Buffer {
+ for (let attempt = 0; ; attempt++) {
+ try {
+ return readFileSync(file)
+ } catch (err) {
+ const code = (err as NodeJS.ErrnoException).code
+ if (attempt >= 2 || (code !== 'EBUSY' && code !== 'EPERM' && code !== 'EACCES')) throw err
+ Atomics.wait(new Int32Array(new SharedArrayBuffer(4)), 0, 0, 100)
+ }
+ }
+}
+
/** Maps a signature -> item id so dedup is O(1). */
interface Index {
items: ClipboardItem[]
}
+/**
+ * A saved history file's items: the encrypted (DPAPI) envelope, or the plain JSON of early versions.
+ * `index` is null when the file cannot be read (it will not decrypt, or it is damaged).
+ */
+function readIndex(raw: Buffer): { index: Index | null; encrypted: boolean; plain: boolean } {
+ let parsedJson: any = null
+ try {
+ parsedJson = JSON.parse(raw.toString('utf8').trim())
+ } catch {
+ /* Raw non-JSON payload */
+ }
+ const valid = (index: any): Index | null =>
+ index && Array.isArray(index.items) ? { items: index.items.filter((it: any) => it && it.data && typeof it.id === 'string') } : null
+
+ if (parsedJson && parsedJson.encrypted === true && typeof parsedJson.payload === 'string') {
+ // Encrypted DPAPI Envelope
+ let index: Index | null = null
+ if (safeStorage.isEncryptionAvailable()) {
+ try {
+ index = valid(JSON.parse(safeStorage.decryptString(Buffer.from(parsedJson.payload, 'base64'))))
+ } catch (err) {
+ console.error('[ItemStore] DPAPI decryption failed:', err)
+ }
+ } else {
+ console.warn('[ItemStore] safeStorage unavailable to decrypt items.json')
+ }
+ return { index, encrypted: true, plain: false }
+ }
+ // Plain JSON (Legacy v0.1.1 format from active users)
+ if (parsedJson && Array.isArray(parsedJson.items)) return { index: valid(parsedJson), encrypted: false, plain: true }
+ return { index: null, encrypted: false, plain: false }
+}
+
/**
* Invoked with every batch of items permanently removed from history, no
* matter which door removed them (manual delete, batch delete, clear,
@@ -54,6 +116,14 @@ export class ItemStore {
private sigToId = new Map<string, string>()
/** Small, bounded thumbnails for renderer DTOs. Original image bytes stay on disk. */
private previewCache = new Map<string, string>()
+ /**
+ * The saved history could not be read this session and could not be set aside either (the file was
+ * held by another process, or the copy failed). It is never written over: nothing is saved until a
+ * later launch reads it, so a passing failure costs this session's new copies, never the history.
+ */
+ private indexUnreadable = false
+ /** The saved history is encrypted: it is never replaced by a plain copy. */
+ private indexEncrypted = false
constructor(private readonly onRemoved?: ItemsRemovedListener) {}
@@ -68,120 +138,161 @@ export class ItemStore {
/** Load persisted state from disk. Called once at startup. */
load(): void {
+ this.indexUnreadable = false
+ this.indexEncrypted = false
+ this.items = []
+ const file = PATHS.indexFile()
try {
- const file = PATHS.indexFile()
- if (!existsSync(file)) {
- this.items = []
- this.rebuildIndex()
- return
- }
+ if (existsSync(file)) this.loadIndex(file)
+ } catch (err) {
+ console.error('[ItemStore] Failed to load index file:', err)
+ // it could not even be read (held by another process, no access): leave it alone this session
+ this.items = []
+ this.indexUnreadable = existsSync(file)
+ }
+ this.rebuildIndex()
+ // a history set aside by an earlier launch comes back as soon as it can be read
+ if (!this.indexUnreadable) this.mergeSetAside(file)
+ }
- const rawBuffer = readFileSync(file)
- const rawStr = rawBuffer.toString('utf8').trim()
- let parsedIndex: Index | null = null
- let needsMigration = false
+ private loadIndex(file: string): void {
+ const rawBuffer = readFileRetrying(file)
+ // nothing in it to keep (older builds wrote in place, and a crash mid-save could leave this)
+ if (!rawBuffer.toString('utf8').trim()) return
+ const read = readIndex(rawBuffer)
+ if (read.encrypted) this.indexEncrypted = true
+ if (!read.index) {
+ this.setAsideUnreadable(file, rawBuffer)
+ return
+ }
+ this.items = read.index.items
+ const upgraded = this.upgradeItems(this.items)
- let parsedJson: any = null
+ // Auto-migrate legacy plain JSON: create backup & upgrade to DPAPI encryption
+ if (read.plain || upgraded) {
+ console.log('[ItemStore] Migrating items.json to DPAPI safeStorage encryption and disk payloads...')
try {
- parsedJson = JSON.parse(rawStr)
- } catch {
- /* Raw non-JSON payload */
+ const backupFile = `${file}.v1.bak`
+ if (!existsSync(backupFile)) {
+ writeFileSync(backupFile, rawBuffer)
+ }
+ this.persist()
+ } catch (err) {
+ console.error('[ItemStore] Auto-migration backup/persist failed:', err)
}
+ }
+ }
- if (parsedJson && parsedJson.encrypted === true && typeof parsedJson.payload === 'string') {
- // Encrypted DPAPI Envelope
- if (safeStorage.isEncryptionAvailable()) {
- try {
- const decryptedStr = safeStorage.decryptString(Buffer.from(parsedJson.payload, 'base64'))
- parsedIndex = JSON.parse(decryptedStr) as Index
- } catch (err) {
- console.error('[ItemStore] DPAPI decryption failed:', err)
- }
- } else {
- console.warn('[ItemStore] safeStorage unavailable to decrypt items.json')
+ /**
+ * Bring items saved by older versions up to date. Returns true when anything changed, so the
+ * history is saved again in the current shape.
+ */
+ private upgradeItems(items: ClipboardItem[]): boolean {
+ // Auto-migrate large text items to disk payload files
+ let changed = false
+ for (const it of items) {
+ // A hand-edited or damaged file must never feed an odd note to the
+ // renderer: re-run the same cleanup as writes. Items saved with the
+ // old label + tags get them as one note (they were pinned when
+ // named, and stay pinned).
+ const legacy = it as ClipboardItem & { label?: unknown; tags?: unknown }
+ if ('label' in legacy || 'tags' in legacy) {
+ applyNote(it, joinNotes(normalizeNote(it.note), noteFromLegacy(legacy.label, legacy.tags)))
+ delete legacy.label
+ delete legacy.tags
+ changed = true
+ } else if ('note' in it) {
+ applyNote(it, normalizeNote(it.note))
+ }
+ if (it.data.kind === 'text') {
+ // links saved before page titles were kept: read it from the saved html once
+ if (it.data.isUrl && it.data.html && !it.data.linkTitle) {
+ const linkTitle = linkTitleFromHtml(it.data.html, it.data.text.trim())
+ if (linkTitle) it.data.linkTitle = linkTitle
+ }
+ if (!it.data.hasFullPayload && it.data.text.length > 300) {
+ this.writeTextPayload(it.id, it.data.text)
+ it.data.hasFullPayload = true
+ it.data.previewText = it.data.text.slice(0, 300)
+ it.data.text = it.data.previewText
+ changed = true
+ }
+ if (it.data.html && it.data.html.length > MAX_ITEM_HTML_CHARS) {
+ delete it.data.html
+ changed = true
}
- } else if (parsedJson && Array.isArray(parsedJson.items)) {
- // Plain JSON (Legacy v0.1.1 format from active users)
- parsedIndex = parsedJson as Index
- needsMigration = true
}
+ }
+ return changed
+ }
- if (parsedIndex && Array.isArray(parsedIndex.items)) {
- this.items = parsedIndex.items.filter((it) => it && it.data && typeof it.id === 'string')
-
- // Auto-migrate large text items to disk payload files
- let migratedAnyPayloads = false
- for (const it of this.items) {
- // A hand-edited or damaged file must never feed an odd note to the
- // renderer: re-run the same cleanup as writes. Items saved with the
- // old label + tags get them as one note (they were pinned when
- // named, and stay pinned).
- const legacy = it as ClipboardItem & { label?: unknown; tags?: unknown }
- if ('label' in legacy || 'tags' in legacy) {
- applyNote(it, joinNotes(normalizeNote(it.note), noteFromLegacy(legacy.label, legacy.tags)))
- delete legacy.label
- delete legacy.tags
- migratedAnyPayloads = true
- } else if ('note' in it) {
- applyNote(it, normalizeNote(it.note))
- }
- if (it.data.kind === 'text') {
- // links saved before page titles were kept: read it from the saved html once
- if (it.data.isUrl && it.data.html && !it.data.linkTitle) {
- const linkTitle = linkTitleFromHtml(it.data.html, it.data.text.trim())
- if (linkTitle) it.data.linkTitle = linkTitle
- }
- if (!it.data.hasFullPayload && it.data.text.length > 300) {
- this.writeTextPayload(it.id, it.data.text)
- it.data.hasFullPayload = true
- it.data.previewText = it.data.text.slice(0, 300)
- it.data.text = it.data.previewText
- migratedAnyPayloads = true
- }
- if (it.data.html && it.data.html.length > MAX_ITEM_HTML_CHARS) {
- delete it.data.html
- migratedAnyPayloads = true
- }
- }
- }
+ /**
+ * The saved history is there but this launch cannot read it (it will not decrypt, or it is damaged).
+ * A byte-for-byte copy is set aside first and checked; a later launch that can read it merges it
+ * back (mergeSetAside). Only once that copy is safe does this session start a new history. If the
+ * copy cannot be made, nothing is saved this session and items.json stays exactly as it is.
+ */
+ private setAsideUnreadable(file: string, raw: Buffer): void {
+ console.warn('[ItemStore] Saved history could not be read; setting it aside to merge back later')
+ const copy = `${file}.unreadable-${Date.now()}.bak`
+ try {
+ writeFileSync(copy, raw)
+ if (!readFileSync(copy).equals(raw)) throw new Error('the copy does not match')
+ } catch (err) {
+ console.error('[ItemStore] Could not set the saved history aside; not saving this session:', err)
+ this.indexUnreadable = true
+ }
+ }
+ /**
+ * Merge back histories set aside because a launch could not read them, and the .corrupted copies
+ * older builds made in the same situation: once one reads, its items that are not here already
+ * join the history (newest first). The copy is removed only after the merged history is saved.
+ */
+ private mergeSetAside(file: string): void {
+ const dir = PATHS.root()
+ const base = pathBasename(file)
+ let names: string[] = []
+ try {
+ names = readdirSync(dir)
+ .filter((n) => (n.startsWith(`${base}.unreadable-`) && n.endsWith('.bak')) || n.startsWith(`${base}.corrupted.`))
+ .sort()
+ } catch {
+ return
+ }
+ for (const name of names) {
+ const copy = join(dir, name)
+ let read: ReturnType<typeof readIndex>
+ try {
+ read = readIndex(readFileSync(copy))
+ } catch {
+ continue
+ }
+ if (!read.index) continue // still unreadable: kept for a later launch
+ const here = new Set(this.items.map((it) => it.id))
+ const back = read.index.items.filter((it) => !here.has(it.id))
+ this.upgradeItems(back)
+ const fresh = back.filter((it) => !this.sigToId.has(this.signatureOf(it)))
+ if (fresh.length) {
+ this.items = [...this.items, ...fresh].sort((x, y) => y.capturedAt - x.capturedAt)
this.rebuildIndex()
-
- // Auto-migrate legacy plain JSON: create backup & upgrade to DPAPI encryption
- if (needsMigration || migratedAnyPayloads) {
- console.log('[ItemStore] Migrating items.json to DPAPI safeStorage encryption and disk payloads...')
- try {
- const backupFile = `${file}.v1.bak`
- if (!existsSync(backupFile)) {
- writeFileSync(backupFile, rawBuffer)
- }
- this.persist()
- } catch (err) {
- console.error('[ItemStore] Auto-migration backup/persist failed:', err)
- }
- }
- } else {
- console.warn('[ItemStore] Index file could not be parsed; preserving data without wiping')
- const backupFile = `${file}.corrupted.${Date.now()}`
- try { writeFileSync(backupFile, rawBuffer) } catch { /* ignore */ }
}
- } catch (err) {
- console.error('[ItemStore] Failed to load index file:', err)
- this.items = []
- this.sigToId.clear()
+ if (!this.persistSync()) break // not saved: keep every copy and try again next launch
+ rmSync(copy, { force: true })
+ console.log(`[ItemStore] Merged ${fresh.length} items back from ${name}`)
}
}
+ /** The signature an item is deduplicated by (long texts by their full text). */
+ private signatureOf(it: ClipboardItem): string {
+ return it.data.kind === 'text' && it.data.hasFullPayload
+ ? contentSignature({ ...it.data, text: this.getFullText(it.id) })
+ : contentSignature(it.data)
+ }
+
private rebuildIndex(): void {
this.sigToId.clear()
- for (const it of this.items) {
- if (it.data.kind === 'text' && it.data.hasFullPayload) {
- const full = this.getFullText(it.id)
- this.sigToId.set(contentSignature({ ...it.data, text: full }), it.id)
- } else {
- this.sigToId.set(contentSignature(it.data), it.id)
- }
- }
+ for (const it of this.items) this.sigToId.set(this.signatureOf(it), it.id)
}
private persistTimer: ReturnType<typeof setTimeout> | null = null
@@ -197,12 +308,14 @@ export class ItemStore {
}, 150)
}
- /** Synchronous disk write (called by debounced timer or on app shutdown). */
- public persistSync(): void {
+ /** Synchronous disk write (called by debounced timer or on app shutdown). True when written. */
+ public persistSync(): boolean {
if (this.persistTimer) {
clearTimeout(this.persistTimer)
this.persistTimer = null
}
+ // the saved history could not be read or set aside this session: it is never written over (see load)
+ if (this.indexUnreadable) return false
try {
const indexObj: Index = { items: this.items }
const jsonStr = JSON.stringify(indexObj)
@@ -215,12 +328,19 @@ export class ItemStore {
encrypted: true,
payload: encryptedBuf.toString('base64')
}
- writeFileSync(file, JSON.stringify(envelope), 'utf8')
- } else {
- writeFileSync(file, JSON.stringify(indexObj), 'utf8')
+ writeFileAtomic(file, JSON.stringify(envelope))
+ this.indexEncrypted = true
+ return true
+ } else if (this.indexEncrypted) {
+ // never trade encrypted history for a plain copy; the next save with encryption back writes it
+ console.warn('[ItemStore] Encryption unavailable; not replacing the encrypted history with a plain copy')
+ return false
}
+ writeFileAtomic(file, JSON.stringify(indexObj))
+ return true
} catch (err) {
console.error('[ItemStore] Persistence failed:', err)
+ return false
}
}
diff --git a/tests/itemStoreSafety.test.ts b/tests/itemStoreSafety.test.ts
new file mode 100644
index 0000000..af008ff
--- /dev/null
+++ b/tests/itemStoreSafety.test.ts
@@ -0,0 +1,219 @@
+import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
+import { existsSync, mkdirSync, readdirSync, readFileSync, rmSync, writeFileSync } from 'node:fs'
+import { join } from 'node:path'
+import { tmpdir } from 'node:os'
+
+const fsRoots = vi.hoisted(() => ({ home: '', userData: '' }))
+// A stand-in for DPAPI: the test decides whether encryption is there, whether decrypting works at all
+// (a passing failure) and which key is in use (data saved under another key will not decrypt).
+const crypto = vi.hoisted(() => ({ available: true, decryptFails: false, key: 'k1' }))
+
+vi.mock('electron', () => ({
+ app: {
+ isPackaged: false,
+ getAppPath: () => join(fsRoots.userData, 'app'),
+ getPath: (name: string) => (name === 'userData' ? fsRoots.userData : join(fsRoots.userData, name))
+ },
+ nativeImage: { createFromPath: () => ({ isEmpty: () => true }) },
+ safeStorage: {
+ isEncryptionAvailable: () => crypto.available,
+ encryptString: (s: string) => Buffer.from(`ENC:${crypto.key}:${s}`, 'utf8'),
+ decryptString: (b: Buffer) => {
+ const s = b.toString('utf8')
+ const head = `ENC:${crypto.key}:`
+ if (crypto.decryptFails || !s.startsWith(head)) throw new Error('Error while decrypting the ciphertext provided to safeStorage.decryptString.')
+ return s.slice(head.length)
+ }
+ }
+}))
+
+vi.mock('node:os', async (importOriginal) => {
+ const actual = await importOriginal<typeof import('node:os')>()
+ return { ...actual, homedir: () => fsRoots.home }
+})
+
+import { ItemStore } from '../electron/store/ItemStore'
+import { PATHS } from '../electron/store/paths'
+
+const text = (t: string) => ({ kind: 'text' as const, text: t, isUrl: false })
+const texts = (store: ItemStore) => store.list().map((i) => (i.data as { text: string }).text)
+const indexFile = () => PATHS.indexFile()
+const bytes = () => readFileSync(indexFile())
+const setAside = () => readdirSync(fsRoots.userData).filter((f) => /^items\.json\.(unreadable-\d+\.bak|corrupted\.\d+)$/.test(f))
+const sideFiles = () => readdirSync(fsRoots.userData).filter((f) => f.startsWith('items.json') && f !== 'items.json')
+
+/** A history saved by the app itself (encrypted), as it is on users' machines. */
+function savedHistory(...list: string[]): Buffer {
+ const store = new ItemStore()
+ store.load()
+ for (const t of list) store.add(text(t), 250)
+ store.persistSync()
+ return bytes()
+}
+
+function launch(): ItemStore {
+ const store = new ItemStore()
+ store.load()
+ return store
+}
+
+beforeEach(() => {
+ const base = join(tmpdir(), `edge-drop-history-${process.pid}-${Date.now()}-${Math.random().toString(36).slice(2)}`)
+ fsRoots.home = join(base, 'home')
+ fsRoots.userData = join(base, 'userData')
+ mkdirSync(fsRoots.home, { recursive: true })
+ mkdirSync(fsRoots.userData, { recursive: true })
+ crypto.available = true
+ crypto.decryptFails = false
+ crypto.key = 'k1'
+})
+
+afterEach(() => {
+ vi.restoreAllMocks()
+ rmSync(join(fsRoots.userData, '..'), { recursive: true, force: true })
+})
+
+describe('a saved history that cannot be read is never lost', () => {
+ it('a passing failure: set aside byte for byte, the app keeps working, everything is back next launch', () => {
+ const original = savedHistory('first', 'second')
+ crypto.decryptFails = true
+ const store = launch()
+ expect(store.list()).toHaveLength(0)
+ const [copy] = setAside()
+ expect(readFileSync(join(fsRoots.userData, copy)).equals(original)).toBe(true)
+ // the session goes on and saves what is new
+ store.add(text('copied meanwhile'), 250)
+ expect(store.persistSync()).toBe(true)
+ // readable again: everything is back, newest first, and the copy is gone
+ crypto.decryptFails = false
+ expect(texts(launch())).toEqual(['copied meanwhile', 'second', 'first'])
+ expect(setAside()).toEqual([])
+ expect(texts(launch())).toEqual(['copied meanwhile', 'second', 'first'])
+ })
+
+ it('a changed key (profile moved, keys damaged): the old history waits, and merges back when its key returns', () => {
+ const original = savedHistory('first', 'second')
+ crypto.key = 'k2'
+ const store = launch()
+ expect(store.list()).toHaveLength(0)
+ store.add(text('saved under the new key'), 250)
+ store.persistSync()
+ // every launch on the new key works normally, the old history kept aside
+ expect(texts(launch())).toEqual(['saved under the new key'])
+ const [oldCopy] = setAside()
+ expect(readFileSync(join(fsRoots.userData, oldCopy)).equals(original)).toBe(true)
+ // the old key back: the old history merges back; what the other key saved is now the one set aside
+ crypto.key = 'k1'
+ expect(texts(launch())).toEqual(['second', 'first'])
+ const left = setAside()
+ expect(left).toHaveLength(1)
+ expect(left[0]).not.toBe(oldCopy)
+ const kept = JSON.parse(readFileSync(join(fsRoots.userData, left[0]), 'utf8')) as { payload: string }
+ expect(Buffer.from(kept.payload, 'base64').toString('utf8')).toContain('ENC:k2:')
+ })
+
+ it('while encryption is unavailable the encrypted history is left as it is, and loads once it is back', () => {
+ const original = savedHistory('kept')
+ crypto.available = false
+ const store = launch()
+ store.add(text('new'), 250)
+ expect(store.persistSync()).toBe(false)
+ expect(bytes().equals(original)).toBe(true)
+ crypto.available = true
+ expect(texts(launch())).toEqual(['kept'])
+ expect(setAside()).toEqual([])
+ })
+
+ it('a damaged file is set aside unchanged and the app starts a new history', () => {
+ writeFileSync(indexFile(), '{"v":2,"encrypted":tr', 'utf8')
+ const original = bytes()
+ const store = launch()
+ store.add(text('new'), 250)
+ expect(store.persistSync()).toBe(true)
+ const [copy] = setAside()
+ expect(readFileSync(join(fsRoots.userData, copy)).equals(original)).toBe(true)
+ // it never reads, so it is kept (never deleted) on later launches
+ expect(texts(launch())).toEqual(['new'])
+ expect(setAside()).toEqual([copy])
+ })
+
+ it('when the copy cannot be made, nothing is saved and the history stays exactly as it is', () => {
+ const original = savedHistory('only copy')
+ crypto.decryptFails = true
+ vi.spyOn(Date, 'now').mockReturnValue(1234)
+ mkdirSync(indexFile() + '.unreadable-1234.bak') // the copy's name is taken: writing it fails
+ const store = launch()
+ store.add(text('new'), 250)
+ expect(store.persistSync()).toBe(false)
+ expect(bytes().equals(original)).toBe(true)
+ vi.restoreAllMocks()
+ crypto.decryptFails = false
+ expect(texts(launch())).toEqual(['only copy'])
+ })
+
+ it('a history file the app cannot open at all is left alone', () => {
+ mkdirSync(indexFile()) // reading it fails (EISDIR), like a file another program holds
+ const store = launch()
+ store.add(text('new'), 250)
+ expect(store.persistSync()).toBe(false)
+ expect(readdirSync(fsRoots.userData)).toContain('items.json')
+ expect(existsSync(indexFile() + '.tmp')).toBe(false)
+ })
+
+ it('history older builds set aside as .corrupted comes back when it reads', () => {
+ const original = savedHistory('lost to the old bug')
+ rmSync(indexFile())
+ writeFileSync(indexFile() + '.corrupted.1793345543710', original)
+ savedHistory('after')
+ expect(texts(launch())).toEqual(['after', 'lost to the old bug'])
+ expect(setAside()).toEqual([])
+ })
+
+ it('encrypted history is never replaced by a plain copy when encryption goes away mid-session', () => {
+ const original = savedHistory('secret')
+ const store = launch()
+ expect(store.list()).toHaveLength(1)
+ crypto.available = false
+ store.add(text('another'), 250)
+ expect(store.persistSync()).toBe(false)
+ expect(bytes().equals(original)).toBe(true)
+ expect(bytes().toString('utf8')).not.toContain('secret')
+ // back on, the next save writes everything, encrypted
+ crypto.available = true
+ expect(store.persistSync()).toBe(true)
+ expect(texts(launch())).toEqual(['another', 'secret'])
+ })
+})
+
+describe('saving', () => {
+ it('replaces the file only once the new one is complete (no side file left, old one never cut off)', () => {
+ savedHistory('one')
+ writeFileSync(indexFile() + '.tmp', 'left over from a crash', 'utf8')
+ const store = launch()
+ expect(store.list()).toHaveLength(1)
+ store.add(text('two'), 250)
+ store.persistSync()
+ expect(sideFiles()).toEqual([])
+ expect(JSON.parse(bytes().toString('utf8'))).toMatchObject({ v: 2, encrypted: true })
+ expect(launch().list()).toHaveLength(2)
+ })
+
+ it('an empty history file (nothing to keep) starts fresh and saves normally', () => {
+ writeFileSync(indexFile(), '', 'utf8')
+ const store = launch()
+ expect(store.list()).toHaveLength(0)
+ store.add(text('fresh'), 250)
+ store.persistSync()
+ expect(launch().list()).toHaveLength(1)
+ expect(sideFiles()).toEqual([])
+ })
+
+ it('a plain history from early versions still loads and is saved encrypted', () => {
+ writeFileSync(indexFile(), JSON.stringify({ items: [{ id: 'a1', data: text('legacy'), capturedAt: 1, hitCount: 1, pinned: false }] }), 'utf8')
+ const store = launch()
+ expect(store.list()).toHaveLength(1)
+ store.persistSync()
+ expect(JSON.parse(bytes().toString('utf8')).encrypted).toBe(true)
+ expect(launch().list()).toHaveLength(1)
+ })
+})