π¬ A browser extension and desktop app that turns your clicks into step-by-step guides with annotated screenshots π
TypeScript browser-extensionchrome-extensiondocumentationfirefox-extensionguide-generatorlocal-firstmanifest-v3privacy-firstproductivityreactscreenshotstep-by-step-guidetutorialtypescriptworkflowwxt
feat(guides): add createSnapshot and getSnapshots
Luis Ortizbc87ded22105
diff --git a/src/core/guides/__tests__/snapshots.test.ts b/src/core/guides/__tests__/snapshots.test.tsnew file mode 100644index 0000000..46da9b3--- /dev/null+++ b/src/core/guides/__tests__/snapshots.test.ts@@ -0,0 +1,167 @@+import 'fake-indexeddb/auto';+import { afterEach, describe, expect, it, vi } from 'vitest';++vi.hoisted(() => {+ globalThis.BroadcastChannel = class BroadcastChannel {+ name: string;+ constructor(name: string) {+ this.name = name;+ }+ postMessage() {}+ addEventListener() {}+ removeEventListener() {}+ close() {}+ onmessage = null;+ onmessageerror = null;+ dispatchEvent() {+ return true;+ }+ } as unknown as typeof BroadcastChannel;+});++import { db } from '../db';+import { createSnapshot, getSnapshots, updateStepDescription } from '../service';+import type { Guide, Screenshot, Step } from '../types';++function makeStep(overrides: Partial<Step> & { id: string; guideId: string }): Step {+ return {+ index: 0,+ description: 'Test step',+ action: 'click',+ url: 'https://example.com',+ timestamp: Date.now(),+ ...overrides,+ };+}++function makeScreenshot(overrides: Partial<Screenshot> & { id: string; stepId: string }): Screenshot {+ return {+ blob: new Blob(['img'], { type: 'image/png' }),+ mimeType: 'image/png',+ width: 800,+ height: 600,+ ...overrides,+ };+}++async function seedGuide(id: string, extras?: Partial<Guide>): Promise<Guide> {+ const guide: Guide = {+ id,+ title: 'Test Guide',+ createdAt: Date.now(),+ updatedAt: Date.now(),+ stepIds: [],+ starred: false,+ deletedAt: null,+ ...extras,+ };+ await db.guides.add(guide);+ return guide;+}++afterEach(async () => {+ await db.guides.clear();+ await db.steps.clear();+ await db.screenshots.clear();+ await db.snapshots.clear();+});++describe('createSnapshot', () => {+ it('stores the guide, steps and screenshot rows without blobs', 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');++ expect(snapshot).not.toBeNull();+ expect(snapshot?.title).toBe('Original');+ expect(snapshot?.stepIds).toEqual(['s1']);+ expect(snapshot?.steps).toHaveLength(1);+ expect(snapshot?.screenshots).toHaveLength(1);+ expect(snapshot?.screenshots[0]).not.toHaveProperty('blob');+ expect(snapshot?.contentHash).toBeTypeOf('string');+ });++ it('returns null for a guide that does not exist', async () => {+ expect(await createSnapshot('missing')).toBeNull();+ });++ it('orders steps by index, not insertion order', async () => {+ await seedGuide('g1', { stepIds: ['s2', 's1'] });+ await db.steps.add(makeStep({ id: 's2', guideId: 'g1', index: 1, description: 'Second' }));+ await db.steps.add(makeStep({ id: 's1', guideId: 'g1', index: 0, description: 'First' }));++ const snapshot = await createSnapshot('g1');++ expect(snapshot?.steps.map((s) => s.description)).toEqual(['First', 'Second']);+ expect(snapshot?.stepIds).toEqual(['s1', 's2']);+ });++ it('gives unchanged content the same hash and changed content a different one', async () => {+ await seedGuide('g1', { stepIds: ['s1'] });+ await db.steps.add(makeStep({ id: 's1', guideId: 'g1', description: 'Before' }));++ const first = await createSnapshot('g1');+ const unchanged = await createSnapshot('g1');+ expect(unchanged?.contentHash).toBe(first?.contentHash);++ await updateStepDescription('s1', 'After');+ const changed = await createSnapshot('g1');+ expect(changed?.contentHash).not.toBe(first?.contentHash);+ });++ it('keeps createdAt strictly increasing within a guide', async () => {+ await seedGuide('g1', { stepIds: [] });++ const a = await createSnapshot('g1');+ const b = await createSnapshot('g1');+ const c = await createSnapshot('g1');++ expect(b!.createdAt).toBeGreaterThan(a!.createdAt);+ expect(c!.createdAt).toBeGreaterThan(b!.createdAt);+ expect((await getSnapshots('g1')).map((s) => s.id)).toEqual([c!.id, b!.id, a!.id]);+ });+});++describe('getSnapshots', () => {+ it('returns snapshots for the guide, newest first', async () => {+ await seedGuide('g1', { stepIds: [] });+ await db.snapshots.bulkAdd([+ {+ id: 'n1',+ guideId: 'g1',+ createdAt: 100,+ contentHash: 'a',+ title: 'A',+ stepIds: [],+ steps: [],+ screenshots: [],+ },+ {+ id: 'n2',+ guideId: 'g1',+ createdAt: 300,+ contentHash: 'b',+ title: 'B',+ stepIds: [],+ steps: [],+ screenshots: [],+ },+ {+ id: 'n3',+ guideId: 'g2',+ createdAt: 200,+ contentHash: 'c',+ title: 'C',+ stepIds: [],+ steps: [],+ screenshots: [],+ },+ ]);++ const list = await getSnapshots('g1');++ expect(list.map((s) => s.id)).toEqual(['n2', 'n1']);+ });+});diff --git a/src/core/guides/db.ts b/src/core/guides/db.tsindex b61402a..2c22a9e 100644--- a/src/core/guides/db.ts+++ b/src/core/guides/db.ts@@ -15,7 +15,7 @@ export class MimikDB extends Dexie {screenshots: 'id, stepId',});this.version(2).stores({- snapshots: 'id, guideId, createdAt',+ snapshots: 'id, guideId, createdAt, [guideId+createdAt]',});}}diff --git a/src/core/guides/service.ts b/src/core/guides/service.tsindex f2ac65e..8212e5a 100644--- a/src/core/guides/service.ts+++ b/src/core/guides/service.ts@@ -1,7 +1,8 @@import { i18n } from '#imports';import type { ScreenshotEdits } from '@/core/screenshot/types';import { db } from './db';-import type { Guide, Screenshot, Step } from './types';+import { hashPayload } from './snapshot-hash';+import type { Guide, Screenshot, Snapshot, Step } from './types';export type GuideChangeEvent = { type: 'starred'; id: string; starred: boolean } | { type: 'mutated' };@@ -194,3 +195,37 @@ export async function getFirstScreenshot(guideId: string): Promise<Screenshot |}return null;}++export async function createSnapshot(guideId: string): Promise<Snapshot | null> {+ return db.transaction('rw', db.guides, db.steps, db.screenshots, db.snapshots, async () => {+ const guide = await db.guides.get(guideId);+ if (!guide) return null;+ const steps = await db.steps.where('guideId').equals(guideId).sortBy('index');+ const stepIds = steps.map((s) => s.id);+ const rows = await db.screenshots.where('stepId').anyOf(stepIds).toArray();+ const screenshots = rows.map(({ blob, ...rest }) => rest);+ const payload = { title: guide.title, stepIds, steps, screenshots };+ const latest = await db.snapshots+ .where('[guideId+createdAt]')+ .between([guideId, -Infinity], [guideId, Infinity])+ .last();+ const now = Date.now();+ const snapshot: Snapshot = {+ id: crypto.randomUUID(),+ guideId,+ createdAt: latest && latest.createdAt >= now ? latest.createdAt + 1 : now,+ contentHash: hashPayload(payload),+ ...payload,+ };+ await db.snapshots.add(snapshot);+ return snapshot;+ });+}++export async function getSnapshots(guideId: string): Promise<Snapshot[]> {+ return db.snapshots+ .where('[guideId+createdAt]')+ .between([guideId, -Infinity], [guideId, Infinity])+ .reverse()+ .toArray();+}diff --git a/src/core/guides/snapshot-hash.ts b/src/core/guides/snapshot-hash.tsnew file mode 100644index 0000000..d3358f9--- /dev/null+++ b/src/core/guides/snapshot-hash.ts@@ -0,0 +1,8 @@+export function hashPayload(value: unknown): string {+ const json = JSON.stringify(value) ?? '';+ let hash = 5381;+ for (let i = 0; i < json.length; i++) {+ hash = ((hash << 5) + hash + json.charCodeAt(i)) | 0;+ }+ return (hash >>> 0).toString(36);+}