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(core): give the capture machine an armed state, step removal and an area pause

Luis Ortize13977960e9c
diff --git a/packages/core/src/capture/__tests__/machine.test.ts b/packages/core/src/capture/__tests__/machine.test.ts
index 3058311..4b98ddd 100644
--- a/packages/core/src/capture/__tests__/machine.test.ts
+++ b/packages/core/src/capture/__tests__/machine.test.ts
@@ -1,5 +1,6 @@
import { describe, expect, it } from 'vitest';
import { createActor } from 'xstate';
+import { isLive } from '../is-live';
import { CaptureState, captureMachine } from '../machine';
function startActor() {
@@ -214,4 +215,50 @@ describe('captureMachine', () => {
expect(snap.context.pauseReason).toBe('blur');
expect(snap.context.currentGuideId).toBe(guideId);
});
+
+ it('arms from IDLE, disarms back, and starts from ARMED', () => {
+ const actor = startActor();
+ actor.send({ type: 'ARM' });
+ expect(actor.getSnapshot().value).toBe(CaptureState.ARMED);
+ actor.send({ type: 'DISARM' });
+ expect(actor.getSnapshot().value).toBe(CaptureState.IDLE);
+ actor.send({ type: 'ARM' });
+ actor.send({ type: 'START_RECORDING', insertTargetGuideId: 'target', insertAtIndex: 2 });
+ const snap = actor.getSnapshot();
+ expect(snap.value).toBe(CaptureState.RECORDING);
+ expect(snap.context.currentGuideId).toHaveLength(36);
+ expect(snap.context.insertTargetGuideId).toBe('target');
+ expect(snap.context.insertAtIndex).toBe(2);
+ });
+
+ it('ignores steps, pauses and stops while ARMED', () => {
+ const actor = startActor();
+ actor.send({ type: 'ARM' });
+ actor.send({ type: 'USER_ACTION' });
+ actor.send({ type: 'PAUSE_CAPTURE', reason: 'manual' });
+ actor.send({ type: 'STOP_RECORDING' });
+ expect(actor.getSnapshot().value).toBe(CaptureState.ARMED);
+ expect(actor.getSnapshot().context.stepCount).toBe(0);
+ });
+
+ it('counts a removed step back down, never below zero, while recording or paused', () => {
+ const actor = startActor();
+ actor.send({ type: 'START_RECORDING' });
+ actor.send({ type: 'USER_ACTION' });
+ actor.send({ type: 'USER_ACTION' });
+ actor.send({ type: 'STEP_REMOVED' });
+ expect(actor.getSnapshot().context.stepCount).toBe(1);
+ actor.send({ type: 'PAUSE_CAPTURE', reason: 'area' });
+ expect(actor.getSnapshot().context.pauseReason).toBe('area');
+ actor.send({ type: 'STEP_REMOVED' });
+ actor.send({ type: 'STEP_REMOVED' });
+ expect(actor.getSnapshot().context.stepCount).toBe(0);
+ });
+
+ it('treats RECORDING and PAUSED as live, and IDLE and ARMED as not', () => {
+ expect(isLive(CaptureState.RECORDING)).toBe(true);
+ expect(isLive(CaptureState.PAUSED)).toBe(true);
+ expect(isLive(CaptureState.IDLE)).toBe(false);
+ expect(isLive(CaptureState.ARMED)).toBe(false);
+ });
});
diff --git a/packages/core/src/capture/is-live.ts b/packages/core/src/capture/is-live.ts
new file mode 100644
index 0000000..491110b
--- /dev/null
+++ b/packages/core/src/capture/is-live.ts
@@ -0,0 +1,5 @@
+import { CaptureState, type CaptureStateValue } from './machine';
+
+export function isLive(state: CaptureStateValue | string | undefined): boolean {
+ return state === CaptureState.RECORDING || state === CaptureState.PAUSED;
+}
diff --git a/packages/core/src/capture/machine.ts b/packages/core/src/capture/machine.ts
index 9d7a3bc..2325d1a 100644
--- a/packages/core/src/capture/machine.ts
+++ b/packages/core/src/capture/machine.ts
@@ -1,21 +1,25 @@
-import { assign, createMachine, type SnapshotFrom } from 'xstate';
+import { assign, type SnapshotFrom, setup } from 'xstate';
export const CaptureState = {
IDLE: 'IDLE',
+ ARMED: 'ARMED',
RECORDING: 'RECORDING',
PAUSED: 'PAUSED',
} as const;
export type CaptureStateValue = (typeof CaptureState)[keyof typeof CaptureState];
-export type PauseReason = 'blur' | 'manual';
+export type PauseReason = 'blur' | 'manual' | 'area';
type CaptureEvent =
+ | { type: 'ARM' }
+ | { type: 'DISARM' }
| { type: 'START_RECORDING'; url?: string; insertTargetGuideId?: string; insertAtIndex?: number }
| { type: 'STOP_RECORDING' }
| { type: 'PAUSE_CAPTURE'; reason: PauseReason; narrationWasLive?: boolean }
| { type: 'RESUME_CAPTURE' }
| { type: 'USER_ACTION' }
+ | { type: 'STEP_REMOVED' }
| { type: 'URL_CHANGED'; url: string };
interface CaptureContext {
@@ -38,29 +42,42 @@ const IDLE_CONTEXT: CaptureContext = {
narrationWasLive: false,
};
-export const captureMachine = createMachine({
- id: 'capture',
- initial: CaptureState.IDLE,
+export const captureMachine = setup({
types: {} as {
context: CaptureContext;
events: CaptureEvent;
},
+ actions: {
+ beginGuide: assign(({ event }) =>
+ event.type === 'START_RECORDING'
+ ? {
+ currentGuideId: crypto.randomUUID(),
+ stepCount: 0,
+ currentUrl: event.url ?? '',
+ insertTargetGuideId: event.insertTargetGuideId ?? null,
+ insertAtIndex: event.insertAtIndex ?? null,
+ pauseReason: null,
+ narrationWasLive: false,
+ }
+ : {},
+ ),
+ removeStep: assign({ stepCount: ({ context }) => Math.max(0, context.stepCount - 1) }),
+ },
+}).createMachine({
+ id: 'capture',
+ initial: CaptureState.IDLE,
context: { ...IDLE_CONTEXT },
states: {
[CaptureState.IDLE]: {
on: {
- START_RECORDING: {
- target: CaptureState.RECORDING,
- actions: assign({
- currentGuideId: () => crypto.randomUUID(),
- stepCount: 0,
- currentUrl: ({ event }) => event.url ?? '',
- insertTargetGuideId: ({ event }) => event.insertTargetGuideId ?? null,
- insertAtIndex: ({ event }) => event.insertAtIndex ?? null,
- pauseReason: null,
- narrationWasLive: false,
- }),
- },
+ ARM: { target: CaptureState.ARMED },
+ START_RECORDING: { target: CaptureState.RECORDING, actions: 'beginGuide' },
+ },
+ },
+ [CaptureState.ARMED]: {
+ on: {
+ DISARM: { target: CaptureState.IDLE },
+ START_RECORDING: { target: CaptureState.RECORDING, actions: 'beginGuide' },
},
},
[CaptureState.RECORDING]: {
@@ -81,6 +98,7 @@ export const captureMachine = createMachine({
stepCount: ({ context }) => context.stepCount + 1,
}),
},
+ STEP_REMOVED: { actions: 'removeStep' },
URL_CHANGED: {
actions: assign({
currentUrl: ({ event }) => event.url,
@@ -94,6 +112,7 @@ export const captureMachine = createMachine({
target: CaptureState.RECORDING,
actions: assign({ pauseReason: null, narrationWasLive: false }),
},
+ STEP_REMOVED: { actions: 'removeStep' },
STOP_RECORDING: {
target: CaptureState.IDLE,
actions: assign({ ...IDLE_CONTEXT }),