Skip to content

armgit / macro

Public Imported from GitHub
0

Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.

4.6 KB
Raw Blame
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
import type { ScheduleDraft } from './core/draft';
import {
  clearRoutineComposerDraft,
  loadRoutineComposerDraft,
  saveRoutineComposerDraft,
} from './draft-storage';

const storageKey = 'routine-composer-draft';
// A new routine's page keeps its draft for a day.
const expiry = 24 * 60 * 60 * 1000;
const agentId = '0195a096-3d24-7000-8000-000000000001';
const draft: ScheduleDraft = {
  name: '',
  prompt: 'Unfinished instructions ',
  frequency: 'week',
  time: '09:00',
  daysOfWeek: [],
  dayOfMonth: '',
  target: { kind: 'model', model: 'retired-model' },
  apps: [],
};

function store(value: unknown): void {
  localStorage.setItem(storageKey, JSON.stringify(value));
}

beforeEach(() => {
  localStorage.clear();
  vi.useFakeTimers();
  vi.setSystemTime(new Date('2026-09-28T12:00:00Z'));
});

afterEach(() => {
  vi.useRealTimers();
  vi.restoreAllMocks();
});

describe('routine composer storage', () => {
  it('returns null for empty storage and clears saved drafts', () => {
    expect(loadRoutineComposerDraft()).toBeNull();
    saveRoutineComposerDraft(draft);
    clearRoutineComposerDraft();
    expect(loadRoutineComposerDraft()).toBeNull();
  });

  it.each<ScheduleDraft['target']>([
    { kind: 'model', model: 'retired-model' },
    { kind: 'agent', agentId },
    { kind: 'agent', agentId, modelOverride: 'runtime/custom' },
  ])('round trips targets without a catalog: %j', (target) => {
    const saved = { ...draft, target };
    saveRoutineComposerDraft(saved);
    expect(loadRoutineComposerDraft()).toEqual(saved);
  });

  it('retains the draft activation choice', () => {
    store({ draft: { ...draft, enabled: false }, timestamp: Date.now() });
    expect(loadRoutineComposerDraft()).toEqual({ ...draft, enabled: false });
  });

  it('migrates an unexpired legacy model without refreshing its expiry', () => {
    const { target: _target, ...fields } = draft;
    store({
      draft: { ...fields, model: 'removed-from-catalog', enabled: true },
      timestamp: Date.now(),
    });
    vi.advanceTimersByTime(expiry);
    expect(loadRoutineComposerDraft()).toEqual({
      ...fields,
      enabled: true,
      target: { kind: 'model', model: 'removed-from-catalog' },
    });
    vi.advanceTimersByTime(1);
    expect(loadRoutineComposerDraft()).toBeNull();
    expect(localStorage.getItem(storageKey)).toBeNull();
  });

  it('retains drafts at the expiry boundary, then removes them', () => {
    saveRoutineComposerDraft(draft);
    vi.advanceTimersByTime(expiry);
    expect(loadRoutineComposerDraft()).toEqual(draft);
    vi.advanceTimersByTime(1);
    expect(loadRoutineComposerDraft()).toBeNull();
    expect(localStorage.getItem(storageKey)).toBeNull();
  });

  it.each([
    null,
    [],
    {},
    { draft },
    { draft, timestamp: 'today' },
    { draft, timestamp: null },
    { draft, timestamp: -1 },
    { draft: null, timestamp: 0 },
  ])('clears malformed envelopes: %j', (payload) => {
    store(payload);
    expect(loadRoutineComposerDraft()).toBeNull();
    expect(localStorage.getItem(storageKey)).toBeNull();
  });

  it.each([
    { target: null },
    { target: { kind: 'model', model: '' } },
    { target: { kind: 'agent', agentId: 'invalid' } },
    { target: { kind: 'agent', agentId, modelOverride: 42 } },
    { target: { kind: 'agent', agentId, modelOverride: null } },
    { target: { kind: 'future' }, model: 'do-not-fallback' },
    { target: undefined },
    { target: undefined, model: '' },
    { prompt: null },
    { name: 42 },
    { frequency: 'daily' },
    { time: 9 },
    { daysOfWeek: '2' },
    { daysOfWeek: [2] },
    { dayOfMonth: 1 },
    { enabled: 'true' },
    { id: 42 },
  ])(
    'clears malformed drafts without silently changing their selection: %j',
    (fields) => {
      store({ draft: { ...draft, ...fields }, timestamp: Date.now() });
      expect(loadRoutineComposerDraft()).toBeNull();
      expect(localStorage.getItem(storageKey)).toBeNull();
    }
  );

  it('handles invalid JSON and inaccessible storage', () => {
    vi.spyOn(console, 'warn').mockImplementation(() => {});
    localStorage.setItem(storageKey, '{');
    expect(loadRoutineComposerDraft()).toBeNull();
    expect(localStorage.getItem(storageKey)).toBeNull();
    vi.spyOn(Storage.prototype, 'getItem').mockImplementation(() => {
      throw new Error('Unavailable');
    });
    expect(loadRoutineComposerDraft()).toBeNull();
    vi.spyOn(Storage.prototype, 'setItem').mockImplementation(() => {
      throw new Error('Quota exceeded');
    });
    expect(() => saveRoutineComposerDraft(draft)).not.toThrow();
  });
});