Skip to content

armgit / mimik

Public Imported from GitHub
0

🎬 A browser extension and desktop app that turns your clicks into step-by-step guides with annotated screenshots πŸ“–

feat(guides): restore a snapshot and snapshot the state it replaces

Luis Ortiz60ac8f08f5dd
diff --git a/src/core/guides/__tests__/snapshots.test.ts b/src/core/guides/__tests__/snapshots.test.ts
index 46da9b3..4be5096 100644
--- a/src/core/guides/__tests__/snapshots.test.ts
+++ b/src/core/guides/__tests__/snapshots.test.ts
@@ -1,13 +1,16 @@
import 'fake-indexeddb/auto';
import { afterEach, describe, expect, it, vi } from 'vitest';
-vi.hoisted(() => {
+const { broadcasts } = vi.hoisted(() => {
+ const broadcasts: unknown[] = [];
globalThis.BroadcastChannel = class BroadcastChannel {
name: string;
constructor(name: string) {
this.name = name;
}
- postMessage() {}
+ postMessage(message: unknown) {
+ broadcasts.push(message);
+ }
addEventListener() {}
removeEventListener() {}
close() {}
@@ -17,10 +20,23 @@ vi.hoisted(() => {
return true;
}
} as unknown as typeof BroadcastChannel;
+ return { broadcasts };
});
import { db } from '../db';
-import { createSnapshot, getSnapshots, updateStepDescription } from '../service';
+import {
+ createSnapshot,
+ deleteStep,
+ getGuide,
+ getSnapshots,
+ permanentlyDeleteGuide,
+ reorderSteps,
+ revertToSnapshot,
+ softDeleteGuide,
+ toggleStar,
+ updateGuideTitle,
+ updateStepDescription,
+} from '../service';
import type { Guide, Screenshot, Step } from '../types';
function makeStep(overrides: Partial<Step> & { id: string; guideId: string }): Step {
@@ -165,3 +181,149 @@ describe('getSnapshots', () => {
expect(list.map((s) => s.id)).toEqual(['n2', 'n1']);
});
});
+
+describe('revertToSnapshot', () => {
+ it('restores title, step order and step content', async () => {
+ await seedGuide('g1', { stepIds: ['s1', 's2'], title: 'Original' });
+ await db.steps.bulkAdd([
+ makeStep({ id: 's1', guideId: 'g1', index: 0, description: 'First' }),
+ makeStep({ id: 's2', guideId: 'g1', index: 1, description: 'Second' }),
+ ]);
+ const snapshot = await createSnapshot('g1');
+
+ await updateGuideTitle('g1', 'Changed');
+ await updateStepDescription('s1', 'Edited');
+ await deleteStep('g1', 's2');
+
+ await revertToSnapshot(snapshot!.id);
+
+ const restored = await getGuide('g1');
+ expect(restored?.guide.title).toBe('Original');
+ expect(restored?.guide.stepIds).toEqual(['s1', 's2']);
+ expect(restored?.steps.map((s) => s.description)).toEqual(['First', 'Second']);
+ });
+
+ it('restores step order after a reorder', async () => {
+ await seedGuide('g1', { stepIds: ['s3', 's1', 's2'] });
+ await db.steps.bulkAdd([
+ makeStep({ id: 's1', guideId: 'g1', index: 1, description: 'Middle' }),
+ makeStep({ id: 's2', guideId: 'g1', index: 2, description: 'Last' }),
+ makeStep({ id: 's3', guideId: 'g1', index: 0, description: 'First' }),
+ ]);
+ const snapshot = await createSnapshot('g1');
+
+ await reorderSteps('g1', ['s1', 's2', 's3']);
+
+ await revertToSnapshot(snapshot!.id);
+
+ const restored = await getGuide('g1');
+ expect(restored?.steps.map((s) => s.description)).toEqual(['First', 'Middle', 'Last']);
+ expect(restored?.guide.stepIds).toEqual(['s3', 's1', 's2']);
+ });
+
+ it('removes steps added after the snapshot was taken', async () => {
+ await seedGuide('g1', { stepIds: ['s1'], title: 'Original' });
+ await db.steps.add(makeStep({ id: 's1', guideId: 'g1', index: 0, description: 'First' }));
+ const snapshot = await createSnapshot('g1');
+
+ await db.steps.add(makeStep({ id: 's2', guideId: 'g1', index: 1, description: 'Added later' }));
+ await db.guides.update('g1', { stepIds: ['s1', 's2'] });
+
+ await revertToSnapshot(snapshot!.id);
+
+ const restored = await getGuide('g1');
+ expect(restored?.guide.stepIds).toEqual(['s1']);
+ expect(restored?.steps.map((s) => s.description)).toEqual(['First']);
+ expect(await db.steps.get('s2')).toBeUndefined();
+ });
+
+ it('snapshots the current state before restoring, so restore is undoable', async () => {
+ await seedGuide('g1', { stepIds: [], title: 'Original' });
+ const first = await createSnapshot('g1');
+ await updateGuideTitle('g1', 'Changed');
+
+ const undo = await revertToSnapshot(first!.id);
+
+ const list = await getSnapshots('g1');
+ expect(list).toHaveLength(2);
+ expect(list[0].title).toBe('Changed');
+ expect(undo?.id).toBe(list[0].id);
+ expect(undo?.title).toBe('Changed');
+
+ await revertToSnapshot(undo!.id);
+ expect((await db.guides.get('g1'))?.title).toBe('Changed');
+ });
+
+ it('does not un-star or un-trash the guide', async () => {
+ await seedGuide('g1', { stepIds: [], starred: false, deletedAt: null });
+ const snapshot = await createSnapshot('g1');
+ await toggleStar('g1');
+ await softDeleteGuide('g1');
+
+ await revertToSnapshot(snapshot!.id);
+
+ const guide = await db.guides.get('g1');
+ expect(guide?.starred).toBe(true);
+ expect(guide?.deletedAt).not.toBeNull();
+ });
+
+ it('restores screenshot metadata onto the live blob and bumps updatedAt', async () => {
+ await seedGuide('g1', { stepIds: ['s1'], updatedAt: 1000 });
+ await db.steps.add(makeStep({ id: 's1', guideId: 'g1', screenshotId: 'sc1' }));
+ await db.screenshots.add(
+ makeScreenshot({
+ id: 'sc1',
+ stepId: 's1',
+ width: 800,
+ edits: {
+ alt: 'original caption',
+ annotations: [{ id: 'a1', type: 'redact', x: 1, y: 2, w: 3, h: 4, style: 'blur' }],
+ },
+ }),
+ );
+ const snapshot = await createSnapshot('g1');
+
+ await db.screenshots.update('sc1', {
+ width: 1234,
+ edits: { alt: 'edited caption', annotations: [] },
+ blob: new Blob(['edited'], { type: 'image/png' }),
+ });
+
+ const undo = await revertToSnapshot(snapshot!.id);
+
+ expect(undo).not.toBeNull();
+ const restored = await db.screenshots.get('sc1');
+ expect(restored?.width).toBe(800);
+ expect(restored?.edits).toEqual({
+ alt: 'original caption',
+ annotations: [{ id: 'a1', type: 'redact', x: 1, y: 2, w: 3, h: 4, style: 'blur' }],
+ });
+ expect(await restored?.blob.text()).toBe('edited');
+ expect((await db.guides.get('g1'))?.updatedAt).toBeGreaterThan(1000);
+ });
+
+ it('is a no-op for an unknown snapshot id', async () => {
+ await seedGuide('g1', { stepIds: [], title: 'Original' });
+ broadcasts.length = 0;
+ expect(await revertToSnapshot('missing')).toBeNull();
+ expect((await db.guides.get('g1'))?.title).toBe('Original');
+ expect(broadcasts).toEqual([]);
+ });
+
+ it('writes nothing and returns null when the guide no longer exists', async () => {
+ await seedGuide('g1', { stepIds: ['s1'], title: 'Original' });
+ await db.steps.add(makeStep({ id: 's1', guideId: 'g1', screenshotId: 'sc1' }));
+ await db.screenshots.add(makeScreenshot({ id: 'sc1', stepId: 's1' }));
+ const snapshot = await createSnapshot('g1');
+
+ await permanentlyDeleteGuide('g1');
+ broadcasts.length = 0;
+
+ expect(await revertToSnapshot(snapshot!.id)).toBeNull();
+ expect(broadcasts).toEqual([]);
+ expect(await db.steps.count()).toBe(0);
+ expect(await db.screenshots.count()).toBe(0);
+ expect(await db.guides.count()).toBe(0);
+ expect(await getSnapshots('g1')).toHaveLength(1);
+ });
+});
diff --git a/src/core/guides/service.ts b/src/core/guides/service.ts
index 8212e5a..7dc0348 100644
--- a/src/core/guides/service.ts
+++ b/src/core/guides/service.ts
@@ -74,13 +74,15 @@ export async function updateGuideTitle(id: string, title: string): Promise<void>
}
export async function addStepToGuide(guideId: string, stepId: string): Promise<void> {
- const guide = await db.guides.get(guideId);
- if (guide) {
- await db.guides.update(guideId, {
- stepIds: [...guide.stepIds, stepId],
- updatedAt: Date.now(),
- });
- }
+ await db.transaction('rw', db.guides, async () => {
+ const guide = await db.guides.get(guideId);
+ if (guide) {
+ await db.guides.update(guideId, {
+ stepIds: [...guide.stepIds, stepId],
+ updatedAt: Date.now(),
+ });
+ }
+ });
}
export async function toggleStar(id: string): Promise<boolean> {
@@ -229,3 +231,29 @@ export async function getSnapshots(guideId: string): Promise<Snapshot[]> {
.reverse()
.toArray();
}
+
+export async function revertToSnapshot(snapshotId: string): Promise<Snapshot | null> {
+ const undo = await db.transaction('rw', db.guides, db.steps, db.screenshots, db.snapshots, async () => {
+ const snapshot = await db.snapshots.get(snapshotId);
+ if (!snapshot) return null;
+ const previous = await createSnapshot(snapshot.guideId);
+ if (!previous) return null;
+ const existing = await db.steps.where('guideId').equals(snapshot.guideId).toArray();
+ const keep = new Set(snapshot.steps.map((s) => s.id));
+ await db.steps.bulkDelete(existing.filter((s) => !keep.has(s.id)).map((s) => s.id));
+ await db.steps.bulkPut(snapshot.steps);
+ const live = await db.screenshots.bulkGet(snapshot.screenshots.map((r) => r.id));
+ const merged = snapshot.screenshots
+ .map((row, i) => (live[i] ? { ...row, blob: live[i]!.blob } : null))
+ .filter((r): r is Screenshot => r !== null);
+ if (merged.length > 0) await db.screenshots.bulkPut(merged);
+ await db.guides.update(snapshot.guideId, {
+ title: snapshot.title,
+ stepIds: snapshot.stepIds,
+ updatedAt: Date.now(),
+ });
+ return previous;
+ });
+ if (undo) notifyGuidesChanged({ type: 'mutated' });
+ return undo;
+}