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 πŸ“–

fix(guides): keep spoken text deletable everywhere it lands

davbio46b20d777d7c
diff --git a/src/core/guides/__tests__/duplicate.test.ts b/src/core/guides/__tests__/duplicate.test.ts
index 9ec4d54..4b3f6fa 100644
--- a/src/core/guides/__tests__/duplicate.test.ts
+++ b/src/core/guides/__tests__/duplicate.test.ts
@@ -24,7 +24,15 @@ const { broadcasts } = vi.hoisted(() => {
});
import { db } from '../db';
-import { createSnapshot, duplicateGuide, getGuide, getSnapshots, replaceScreenshot } from '../service';
+import {
+ createSnapshot,
+ deleteTranscripts,
+ duplicateGuide,
+ getGuide,
+ getSnapshots,
+ getTranscripts,
+ replaceScreenshot,
+} from '../service';
import type { Guide, Screenshot, Step } from '../types';
function makeStep(overrides: Partial<Step> & { id: string; guideId: string }): Step {
@@ -142,23 +150,6 @@ describe('duplicateGuide', () => {
expect(await getSnapshots('g1')).toHaveLength(1);
});
- it('does not carry the transcript over', async () => {
- await seedGuide('g1', { stepIds: ['s1'] });
- await db.steps.add(makeStep({ id: 's1', guideId: 'g1', narratedDescription: 'what I said' }));
- await db.transcripts.add({
- id: 't1',
- guideId: 'g1',
- epochMs: Date.now(),
- lines: [{ text: 'what I said', startSeconds: 0, endSeconds: 1, stepId: 's1' }],
- } as never);
-
- const copyId = await duplicateGuide('g1');
-
- expect(await db.transcripts.where('guideId').equals(copyId!).count()).toBe(0);
- const copy = await getGuide(copyId!);
- expect(copy?.steps[0].narratedDescription).toBe('what I said');
- });
-
it('copies only the screenshot each step currently points at', async () => {
await seedGuide('g1', { stepIds: ['s1'] });
await db.steps.add(makeStep({ id: 's1', guideId: 'g1', screenshotId: 'sc1' }));
@@ -213,4 +204,49 @@ describe('duplicateGuide', () => {
await duplicateGuide('g1');
expect(broadcasts).toContainEqual({ type: 'mutated' });
});
+
+ it('brings the transcript along, so the copy can still delete its spoken text', async () => {
+ await seedGuide('g1', { stepIds: ['s1'] });
+ await db.steps.add(
+ makeStep({
+ id: 's1',
+ guideId: 'g1',
+ description: 'Open billing',
+ narratedDescription: 'Open billing',
+ descriptionSource: 'narration',
+ }),
+ );
+ await db.transcripts.add({
+ id: 't1',
+ guideId: 'g1',
+ epochMs: 1,
+ createdAt: 1,
+ lines: [
+ { start: 0, end: 1, text: 'Open billing', stepId: 's1', rejectReason: null },
+ { start: 2, end: 3, text: 'stray', stepId: 'gone', rejectReason: null, addedByHand: true },
+ ],
+ });
+
+ const copyId = (await duplicateGuide('g1'))!;
+ const copyStepId = (await getGuide(copyId))!.steps[0].id;
+ const [copied] = await getTranscripts(copyId);
+ expect(copied.id).not.toBe('t1');
+ expect(copied.lines[0].stepId).toBe(copyStepId);
+ expect(copied.lines[1]).toMatchObject({ stepId: null });
+ expect(copied.lines[1].addedByHand).toBeUndefined();
+
+ await deleteTranscripts(copyId);
+ const copyStep = (await getGuide(copyId))!.steps[0];
+ expect(copyStep.narratedDescription).toBeUndefined();
+ expect(copyStep.description).not.toBe('Open billing');
+ expect(await getTranscripts('g1')).toHaveLength(1);
+ });
+
+ it('keeps the copy title on one line when the original predates the rule', async () => {
+ await seedGuide('g1', { title: 'Reset\na password' });
+
+ const copyId = await duplicateGuide('g1');
+
+ expect((await getGuide(copyId!))?.guide.title).not.toContain('\n');
+ });
});
diff --git a/src/core/guides/__tests__/spoken-original.test.ts b/src/core/guides/__tests__/spoken-original.test.ts
index 8475a12..1dabb2a 100644
--- a/src/core/guides/__tests__/spoken-original.test.ts
+++ b/src/core/guides/__tests__/spoken-original.test.ts
@@ -95,6 +95,37 @@ describe('the spoken original', () => {
expect(step?.narratedDescription).toBe(SPOKEN);
});
+ it('adds speech from a later slice after what an earlier slice said, instead of replacing it', async () => {
+ const captured = (await db.steps.get('s1'))!.timestamp;
+ await applyNarrationToSteps([{ stepId: 's1', description: SPOKEN }], captured - 1);
+
+ await applyNarrationToSteps([{ stepId: 's1', description: 'and that is it' }], captured + 1);
+
+ const step = await db.steps.get('s1');
+ expect(step?.description).toBe(`${SPOKEN} and that is it`);
+ expect(step?.narratedDescription).toBe(`${SPOKEN} and that is it`);
+ });
+
+ it('replaces what a step said when the new speech comes from the slice it was captured in', async () => {
+ const captured = (await db.steps.get('s1'))!.timestamp;
+ await applyNarrationToSteps([{ stepId: 's1', description: 'first try' }], captured - 1);
+
+ await applyNarrationToSteps([{ stepId: 's1', description: SPOKEN }], captured - 1);
+
+ expect((await db.steps.get('s1'))?.narratedDescription).toBe(SPOKEN);
+ });
+
+ it('does not overwrite a description the user already edited', async () => {
+ await updateStepDescription('s1', 'Click the Billing tab', 'manual');
+
+ await applyNarrationToSteps([{ stepId: 's1', description: SPOKEN }]);
+
+ const step = await db.steps.get('s1');
+ expect(step?.description).toBe('Click the Billing tab');
+ expect(step?.descriptionSource).toBe('manual');
+ expect(step?.narratedDescription).toBe(SPOKEN);
+ });
+
it('survives an edit that replaces the description', async () => {
await applyNarrationToSteps([{ stepId: 's1', description: SPOKEN }]);
await updateStepDescription('s1', 'Click Billing', 'manual');
@@ -161,6 +192,17 @@ describe('adding a transcript line to a step', () => {
expect((await db.steps.get('s1'))?.description).toBe('Clicked Billing then confirm the change');
});
+ it('accepts a line whose step has since been deleted, as the panel shows it unused', async () => {
+ await saveTranscript('g1', {
+ epochMs: 1_700_000_000_000,
+ lines: [{ start: 1, end: 3, text: 'Open billing', stepId: 'deleted-step', rejectReason: null }],
+ });
+ const row = (await getTranscripts('g1'))[0];
+
+ expect(await addTranscriptLineToStep(row.id, 0, 's1', 'Open billing')).toContain('Open billing');
+ expect((await getTranscripts('g1'))[0].lines[0]).toMatchObject({ stepId: 's1', addedByHand: true });
+ });
+
it('refuses a line index that is not in the row', async () => {
const rowId = await seedRow();
expect(await addTranscriptLineToStep(rowId, 7, 's1', 'anything')).toBeNull();
@@ -423,6 +465,18 @@ describe('deleting a transcript and the description narration wrote', () => {
expect(step?.description).toBe('Clicked Billing');
});
+ it('leaves a step narration never touched alone, even when its text ends like a transcript line', async () => {
+ await updateStepDescription('s1', 'Click Save.', 'manual');
+ await saveTranscript('g1', {
+ epochMs: 1_700_000_000_000,
+ lines: [{ start: 1, end: 3, text: 'Save.', stepId: null, rejectReason: null }],
+ });
+
+ await deleteTranscripts('g1');
+
+ expect((await db.steps.get('s1'))?.description).toBe('Click Save.');
+ });
+
it('leaves a description the user wrote alone', async () => {
await applyNarrationToSteps([{ stepId: 's1', description: SPOKEN }]);
await saveTranscript('g1', transcript);
@@ -435,6 +489,52 @@ describe('deleting a transcript and the description narration wrote', () => {
expect(step?.descriptionSource).toBe('manual');
});
+ it('strips the narration from a step a hand-added line turned manual', async () => {
+ const secret = 'my password is hunter2';
+ await db.steps.update('s1', { elementMeta: BUTTON_META });
+ await applyNarrationToSteps([{ stepId: 's1', description: SPOKEN }]);
+ await saveTranscript('g1', {
+ epochMs: 1_700_000_000_000,
+ lines: [
+ { start: 1, end: 3, text: SPOKEN, stepId: 's1', rejectReason: null },
+ { start: 4, end: 6, text: secret, stepId: null, rejectReason: null },
+ ],
+ });
+ const row = (await getTranscripts('g1'))[0];
+ await addTranscriptLineToStep(row.id, 1, 's1', secret);
+ expect((await db.steps.get('s1'))?.descriptionSource).toBe('manual');
+
+ await deleteTranscripts('g1');
+
+ const step = await db.steps.get('s1');
+ expect(step?.description).toBe('steps.click[Billing]');
+ expect(step?.descriptionSource).toBe('heuristic');
+ });
+
+ it('strips a released line from a snapshot taken while it was attached', async () => {
+ const secret = 'my password is hunter2';
+ await db.steps.update('s1', { elementMeta: BUTTON_META });
+ await applyNarrationToSteps([{ stepId: 's1', description: SPOKEN }]);
+ await saveTranscript('g1', {
+ epochMs: 1_700_000_000_000,
+ lines: [
+ { start: 1, end: 3, text: SPOKEN, stepId: 's1', rejectReason: null },
+ { start: 4, end: 6, text: secret, stepId: null, rejectReason: null },
+ ],
+ });
+ const row = (await getTranscripts('g1'))[0];
+ await addTranscriptLineToStep(row.id, 1, 's1', secret);
+ await createSnapshot('g1');
+ await restoreNarratedDescription('s1');
+
+ await deleteTranscripts('g1');
+
+ const snapshots = await db.snapshots.where('guideId').equals('g1').toArray();
+ const text = JSON.stringify(snapshots.flatMap((s) => s.steps));
+ expect(text).not.toContain('hunter2');
+ expect(text).not.toContain(SPOKEN);
+ });
+
it('takes the spoken words out of the snapshots as well', async () => {
await db.steps.update('s1', { elementMeta: BUTTON_META });
await applyNarrationToSteps([{ stepId: 's1', description: SPOKEN }]);
diff --git a/src/core/guides/service.ts b/src/core/guides/service.ts
index ecd033e..daa5dd8 100644
--- a/src/core/guides/service.ts
+++ b/src/core/guides/service.ts
@@ -213,7 +213,7 @@ export async function createStep(step: Step): Promise<void> {
}
export async function duplicateGuide(guideId: string): Promise<string | null> {
- const copyId = await db.transaction('rw', db.guides, db.steps, db.screenshots, async () => {
+ const copyId = await db.transaction('rw', [db.guides, db.steps, db.screenshots, db.transcripts], async () => {
const guide = await db.guides.get(guideId);
if (!guide) return null;
const steps = await db.steps.where('guideId').equals(guideId).sortBy('index');
@@ -231,7 +231,7 @@ export async function duplicateGuide(guideId: string): Promise<string | null> {
await db.guides.add({
...rest,
id: newGuideId,
- title: i18n.t('library.copyOfTitle', [guide.title]),
+ title: sanitizeGuideTitle(i18n.t('library.copyOfTitle', [guide.title])),
createdAt: now,
updatedAt: now,
stepIds: steps.map((step) => stepIdMap.get(step.id)!),
@@ -255,6 +255,20 @@ export async function duplicateGuide(guideId: string): Promise<string | null> {
stepId: stepIdMap.get(row.stepId)!,
})),
);
+ const transcripts = await db.transcripts.where('guideId').equals(guideId).toArray();
+ await db.transcripts.bulkAdd(
+ transcripts.map((row) => ({
+ ...row,
+ id: crypto.randomUUID(),
+ guideId: newGuideId,
+ lines: row.lines.map((line) => {
+ const stepId = line.stepId ? (stepIdMap.get(line.stepId) ?? null) : null;
+ if (stepId) return { ...line, stepId };
+ const { addedByHand: _addedByHand, ...rest } = line;
+ return { ...rest, stepId: null };
+ }),
+ })),
+ );
return newGuideId;
});
if (copyId) notifyGuidesChanged({ type: 'mutated' });
@@ -325,13 +339,25 @@ export async function updateStepDescription(
await db.steps.update(stepId, source ? { description, descriptionSource: source } : { description });
}
-export async function applyNarrationToSteps(updates: readonly NarrationUpdate[]): Promise<void> {
+export async function applyNarrationToSteps(
+ updates: readonly NarrationUpdate[],
+ sliceStartMs = Number.NEGATIVE_INFINITY,
+): Promise<void> {
if (updates.length === 0) return;
await db.transaction('rw', db.steps, async () => {
- for (const { stepId, description } of updates) {
- await db.steps.update(stepId, {
- description,
- narratedDescription: description,
+ for (const update of updates) {
+ const step = await db.steps.get(update.stepId);
+ if (!step) continue;
+ const earlier = step.narratedDescription?.trim();
+ const spokenBefore = earlier && step.timestamp < sliceStartMs ? earlier : '';
+ const spoken = spokenBefore ? `${spokenBefore} ${update.description}` : update.description;
+ if (step.descriptionSource === 'manual') {
+ await db.steps.update(step.id, { narratedDescription: spoken, aiPending: false });
+ continue;
+ }
+ await db.steps.update(step.id, {
+ description: spoken,
+ narratedDescription: spoken,
descriptionSource: 'narration',
aiPending: false,
});
@@ -404,46 +430,56 @@ function withoutAppendedSentences(description: string, additions: readonly strin
return kept;
}
-function forgetSpokenWords(step: Step, handAdded: ReadonlyMap<string, string[]>): void {
+function withoutSpokenPrefix(description: string, spoken: string | undefined): string {
+ if (!spoken) return description;
+ if (description === spoken) return '';
+ return description.startsWith(`${spoken} `) ? description.slice(spoken.length + 1).trim() : description;
+}
+
+function forgetSpokenWords(step: Step, rows: readonly GuideTranscript[]): void {
+ const spoken = step.narratedDescription?.trim();
delete step.narratedDescription;
if (step.descriptionSource === 'narration') {
step.description = rebuiltHeuristicDescription(step);
step.descriptionSource = 'heuristic';
return;
}
- const additions = handAdded.get(step.id);
- if (!additions) return;
- const kept = withoutAppendedSentences(step.description, additions);
- step.description = kept || rebuiltHeuristicDescription(step);
-}
-
-function handAddedSentences(rows: readonly GuideTranscript[]): Map<string, string[]> {
- const byStep = new Map<string, string[]>();
- for (const row of rows) {
- for (const line of row.lines) {
- if (!line.stepId || !line.addedByHand) continue;
- const existing = byStep.get(line.stepId);
- if (existing) existing.push(line.text);
- else byStep.set(line.stepId, [line.text]);
- }
+ const lines = spokenLinesFor(step.id, Boolean(spoken), rows);
+ if (!spoken && lines.length === 0) return;
+ const original = step.description.trim();
+ const kept = withoutSpokenPrefix(withoutAppendedSentences(original, lines), spoken);
+ if (kept === original) return;
+ if (kept) {
+ step.description = kept;
+ return;
}
- return byStep;
+ step.description = rebuiltHeuristicDescription(step);
+ step.descriptionSource = 'heuristic';
+}
+
+function spokenLinesFor(stepId: string, narrated: boolean, rows: readonly GuideTranscript[]): string[] {
+ return rows.flatMap((row) =>
+ row.lines
+ .filter((line) =>
+ narrated ? line.stepId === null || line.stepId === stepId : line.stepId === stepId && line.addedByHand,
+ )
+ .map((line) => line.text),
+ );
}
export async function deleteTranscripts(guideId: string): Promise<void> {
await db.transaction('rw', db.transcripts, db.steps, db.snapshots, async () => {
const rows = await db.transcripts.where('guideId').equals(guideId).toArray();
- const handAdded = handAddedSentences(rows);
await db.transcripts.where('guideId').equals(guideId).delete();
await db.steps
.where('guideId')
.equals(guideId)
- .modify((step) => forgetSpokenWords(step, handAdded));
+ .modify((step) => forgetSpokenWords(step, rows));
await db.snapshots
.where('guideId')
.equals(guideId)
.modify((snapshot) => {
- for (const step of snapshot.steps) forgetSpokenWords(step, handAdded);
+ for (const step of snapshot.steps) forgetSpokenWords(step, rows);
snapshot.contentHash = hashPayload({
title: snapshot.title,
stepIds: snapshot.stepIds,
@@ -501,7 +537,8 @@ async function writeAppendedDescription(stepId: string, addition: string): Promi
async function isLineStillUnused(rowId: string, lineIndex: number): Promise<boolean> {
const row = await db.transcripts.get(rowId);
const line = row?.lines[lineIndex];
- return line !== undefined && !line.stepId;
+ if (!line) return false;
+ return !line.stepId || !(await db.steps.get(line.stepId));
}
export async function addTranscriptLineToStep(