import { describe, expect, it } from 'vitest';
import { IosShortcutsChannel, SimulatedIosAppIntentBridge, handleSharedImage } from '../src/services/iosShortcuts';

describe('iOS Shortcuts', () => {
  it('IosShortcutsChannel 接收原生事件', async () => {
    const received: string[] = [];
    const bridge = new SimulatedIosAppIntentBridge();
    const channel = new IosShortcutsChannel(bridge, e => received.push(e.type));
    channel.start();
    bridge.simulate({ type: 'screenshot', base64: 'img' });
    bridge.simulate({ type: 'share-image', base64: 'img2' });
    expect(received).toEqual(['screenshot', 'share-image']);
    channel.stop();
  });

  it('handleEvent 可手动调用', async () => {
    const channel = new IosShortcutsChannel(null, () => {});
    await channel.handleEvent({ type: 'voice', text: '花了30元' });
  });

  it('notifyComplete 调用桥接', async () => {
    const bridge = new SimulatedIosAppIntentBridge();
    const channel = new IosShortcutsChannel(bridge, () => {});
    await channel.notifyComplete();
    expect(bridge.completedCount).toBe(1);
  });

  it('无桥接时 start/notifyComplete 不报错', async () => {
    const channel = new IosShortcutsChannel(null, () => {});
    channel.start();
    await channel.notifyComplete();
    channel.stop();
  });

  it('handleSharedImage 生成 ScreenshotEvent', () => {
    const event = handleSharedImage('base64-data');
    expect(event.base64).toBe('base64-data');
    expect(event.uri).toContain('share-extension');
    expect(event.timestamp).toBeGreaterThan(0);
  });

  it('stop 后不再接收事件', () => {
    const received: string[] = [];
    const bridge = new SimulatedIosAppIntentBridge();
    const channel = new IosShortcutsChannel(bridge, e => received.push(e.type));
    channel.start();
    channel.stop();
    bridge.simulate({ type: 'screenshot' });
    expect(received).toHaveLength(0);
  });
});
