Skip to content

armgit / cindy

Public Imported from GitHub
0

Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。

18.0 KB
Raw Blame
import { beforeEach, describe, expect, it, vi } from 'vitest';
import type { ResolvedSharePayload } from 'expo-sharing';
import { getMobileAuthOwner, invalidateMobileAuthOwnerForSwitch, isMobileAuthOwnerCurrent, setMobileAuthOwner, __testing as authOwnerTesting } from '@/auth/authOwnerGeneration';

import {
  __resetIncomingShareForTest,
  consumeIncomingShareBatch,
  discardIncomingShareBatch,
  leaveIncomingShareChooser,
  incomingShareBatchId,
  selectIncomingShareUploadCandidates,
  stageIncomingShareBatch,
  receiveIncomingShare,
  watchIncomingShareAccount,
} from '@/session/incomingShare';

const convertToJpeg = vi.fn(async (uri: string) => `${uri}.jpg`);
const deleteAsync = vi.hoisted(() => vi.fn(async (_uri: string, _options: { idempotent: boolean }) => undefined));
vi.mock('expo-file-system/legacy', () => ({ deleteAsync }));

vi.mock('@/session/pastedImageAttachment', async (importOriginal) => {
  const original = await importOriginal<typeof import('@/session/pastedImageAttachment')>();
  return {
    ...original,
    resolvePastedImageAsset: async (uri: string, index: number) => {
      const classified = original.classifyPastedImageUri(uri);
      if (classified.needsJpegConversion) {
        return {
          uri: await convertToJpeg(uri),
          fileName: `pasted-image-${index + 1}.jpg`,
          mimeType: 'image/jpeg',
        };
      }
      return original.resolvePastedImageAsset(uri, index);
    },
  };
});

function payload(
  patch: Partial<ResolvedSharePayload>,
): ResolvedSharePayload {
  return {
    value: 'file:///shared/report.pdf',
    shareType: 'file',
    mimeType: 'application/pdf',
    contentUri: 'file:///shared/report.pdf',
    contentType: 'file',
    contentMimeType: 'application/pdf',
    originalName: 'report.pdf',
    contentSize: 1024,
    ...patch,
  } as ResolvedSharePayload;
}

describe('incoming Share Extension payloads', () => {
  beforeEach(() => {
    __resetIncomingShareForTest();
    authOwnerTesting.reset();
    setMobileAuthOwner('account-a');
    deleteAsync.mockClear();
    convertToJpeg.mockClear();
  });

  it('leaves after native cancellation fails without losing files or the retryable batch', () => {
    const acknowledge = vi.fn().mockImplementationOnce(() => { throw new Error('storage unavailable'); });
    const batch = stageIncomingShareBatch([payload({})], acknowledge)!;
    const leave = vi.fn();
    leaveIncomingShareChooser(batch.id, leave);
    expect(leave).toHaveBeenCalledOnce();
    expect(deleteAsync).not.toHaveBeenCalled();
    expect(stageIncomingShareBatch([payload({})], vi.fn())).toBe(batch);
    expect(consumeIncomingShareBatch(batch.id)).toBe(true);
    expect(acknowledge).toHaveBeenCalledTimes(2);
  });

  it('never interprets file-URL text as an upload or cleanup capability', async () => {
    const text = payload({ shareType: 'text', value: 'file:///private/document.txt', contentUri: 'file:///private/document.txt', contentType: 'text' });
    expect(selectIncomingShareUploadCandidates([text]).candidates).toEqual([]);
    const batch = stageIncomingShareBatch([text], vi.fn())!;
    expect(discardIncomingShareBatch(batch.id)).toBe(true);
    const clearSharedPayloads = vi.fn();
    const stop = watchIncomingShareAccount({ getSharedPayloads: () => [text], clearSharedPayloads });
    stageIncomingShareBatch([text], vi.fn());
    setMobileAuthOwner('account-b');
    await new Promise(resolve => setTimeout(resolve, 0));
    expect(deleteAsync).not.toHaveBeenCalled();
    stop();
  });

  it('converts non-sendable iOS image formats before entering the attachment uploader', async () => {
    const result = selectIncomingShareUploadCandidates([
      payload({
        value: 'file:///shared/IMG_0001.HEIC',
        shareType: 'image',
        mimeType: 'image/heic',
        contentUri: 'file:///shared/IMG_0001.HEIC',
        contentType: 'image',
        contentMimeType: 'image/heic',
        originalName: 'IMG_0001.HEIC',
      }),
    ]);

    expect(result.candidates).toHaveLength(1);
    expect(result.candidates[0]).toMatchObject({
      kind: 'image',
      name: 'IMG_0001.HEIC',
      mimeType: 'image/heic',
    });
    await expect(result.candidates[0]!.resolve!()).resolves.toEqual({
      uri: 'file:///shared/IMG_0001.HEIC.jpg',
      name: 'IMG_0001.jpg',
      mimeType: 'image/jpeg',
      size: 0,
      skipPreprocess: true,
    });
    expect(convertToJpeg).toHaveBeenCalledWith('file:///shared/IMG_0001.HEIC');
  });

  it.each(['svg', 'psd', 'dng'])('preserves %s image exports as files instead of forcing a JPEG decoder', (extension) => {
    const input = payload({ shareType: 'image', contentType: 'image', originalName: `drawing.${extension}` });
    const result = selectIncomingShareUploadCandidates([input]);
    expect(result.candidates).toEqual([expect.objectContaining({ kind: 'file', name: `drawing.${extension}` })]);
    expect(result.candidates[0]!.resolve).toBeUndefined();
    expect(convertToJpeg).not.toHaveBeenCalled();
  });

  it('converts supported files and images into the existing attachment upload candidates', () => {
    const result = selectIncomingShareUploadCandidates([
      payload({}),
      payload({
        value: 'file:///shared/screenshot.png',
        shareType: 'image',
        mimeType: 'image/png',
        contentUri: 'file:///shared/screenshot.png',
        contentType: 'image',
        contentMimeType: 'image/png',
        originalName: 'screenshot.png',
        contentSize: 2048,
      }),
    ]);

    expect(result).toEqual({
      candidates: [
        {
          kind: 'file',
          uri: 'file:///shared/report.pdf',
          name: 'report.pdf',
          size: 1024,
          mimeType: 'application/pdf',
        },
        {
          kind: 'image',
          uri: 'file:///shared/screenshot.png',
          name: 'screenshot.png',
          size: 2048,
          mimeType: 'image/png',
        },
      ],
      rejectedUris: [],
    });
  });

  it('falls back to a decoded URI basename and accepts any attachment type', () => {
    const result = selectIncomingShareUploadCandidates([
      payload({
        contentUri: 'file:///shared/My%20Notes.md',
        originalName: null,
        contentSize: null,
        contentMimeType: null,
        mimeType: 'text/markdown',
      }),
      payload({
        value: 'file:///shared/archive.zip',
        contentUri: 'file:///shared/archive.zip',
        originalName: 'archive.zip',
      }),
    ]);

    expect(result.candidates).toEqual([{
      kind: 'file',
      uri: 'file:///shared/My%20Notes.md',
      name: 'My Notes.md',
      size: 0,
      mimeType: 'text/markdown',
    }, expect.objectContaining({
      kind: 'file',
      uri: 'file:///shared/archive.zip',
      name: 'archive.zip',
    })]);
    expect(result.rejectedUris).toEqual([]);
  });

  it('keeps a batch until the matching consumer acknowledges it exactly once', () => {
    const acknowledge = vi.fn();
    const shared = [payload({})];
    const first = stageIncomingShareBatch(shared, acknowledge);
    const duplicate = stageIncomingShareBatch(shared, vi.fn());

    expect(first?.id).toBe(incomingShareBatchId(shared));
    expect(duplicate).toBe(first);
    expect(consumeIncomingShareBatch('wrong-id')).toBe(false);
    expect(acknowledge).not.toHaveBeenCalled();
    expect(consumeIncomingShareBatch(first!.id)).toBe(true);
    expect(acknowledge).toHaveBeenCalledTimes(1);
    expect(consumeIncomingShareBatch(first!.id)).toBe(false);
  });

  it('retains successive shares during login and does not clear the newer native slot', () => {
    setMobileAuthOwner(null);
    let raw = [{ value: 'file:///group/cindy-share-a/report.pdf', shareType: 'file' as const }];
    const native = { getSharedPayloads: () => raw, clearSharedPayloads: vi.fn((expected) => {
      if (expected === raw) raw = [];
    }) };
    receiveIncomingShare(native);
    const first = stageIncomingShareBatch([payload({
      value: raw[0]!.value, contentUri: raw[0]!.value, mimeType: undefined,
      contentMimeType: null, contentSize: null,
    })], vi.fn())!;
    raw = [{ value: 'file:///group/cindy-share-b/report.pdf', shareType: 'file' }];
    receiveIncomingShare(native);
    receiveIncomingShare(native);
    expect(consumeIncomingShareBatch(first.id)).toBe(false);
    setMobileAuthOwner('account-a');
    expect(consumeIncomingShareBatch(first.id)).toBe(true);
    expect(native.clearSharedPayloads).toHaveBeenCalledTimes(1);
    expect(consumeIncomingShareBatch(first.id)).toBe(false);
    const secondId = incomingShareBatchId([payload({
      value: raw[0]!.value, contentUri: raw[0]!.value, mimeType: undefined,
      contentMimeType: null, contentSize: null,
    })]);
    expect(consumeIncomingShareBatch(secondId)).toBe(true);
    expect(native.clearSharedPayloads).toHaveBeenCalledTimes(2);
  });

  it('retains the mailbox until durable acknowledgement succeeds', () => {
    const acknowledge = vi.fn().mockImplementationOnce(() => { throw new Error('storage unavailable'); });
    const batch = stageIncomingShareBatch([payload({})], acknowledge)!;
    expect(() => consumeIncomingShareBatch(batch.id)).toThrow('storage unavailable');
    expect(stageIncomingShareBatch([payload({})], vi.fn())).toBe(batch);
    expect(consumeIncomingShareBatch(batch.id)).toBe(true);
    expect(consumeIncomingShareBatch(batch.id)).toBe(false);
    expect(acknowledge).toHaveBeenCalledTimes(2);
  });

  it('never treats remote URLs as local upload/cleanup targets', () => {
    expect(selectIncomingShareUploadCandidates([payload({ contentUri: 'https://example.com/report.pdf' })]))
      .toEqual({ candidates: [], rejectedUris: [] });
  });

  it.each(['switch', 'logout', 'same-account-relogin', 'realm-switch'])('discards received shares on %s', async (transition) => {
    const acknowledge = vi.fn();
    const batch = stageIncomingShareBatch([payload({})], acknowledge)!;
    if (transition === 'switch') setMobileAuthOwner('account-b');
    else if (transition === 'realm-switch') setMobileAuthOwner('account-a', 'cn');
    else {
      setMobileAuthOwner(null);
      if (transition === 'same-account-relogin') setMobileAuthOwner('account-a');
    }
    expect(consumeIncomingShareBatch(batch.id)).toBe(false);
    expect(acknowledge).toHaveBeenCalledTimes(1);
    await vi.waitFor(() => expect(deleteAsync).toHaveBeenCalledWith('file:///shared/report.pdf', { idempotent: true }));
  });

  it('keeps shares across a same-account refresh, but not first-login then switch', () => {
    const batch = stageIncomingShareBatch([payload({})], vi.fn())!;
    setMobileAuthOwner('account-a');
    expect(consumeIncomingShareBatch(batch.id)).toBe(true);
    setMobileAuthOwner(null);
    const loggedOutBatch = stageIncomingShareBatch([payload({})], vi.fn())!;
    setMobileAuthOwner('account-a');
    setMobileAuthOwner('account-b');
    expect(consumeIncomingShareBatch(loggedOutBatch.id)).toBe(false);
  });

  it.each(['text', 'url'] as const)('fences uncleared %s from the next account until retry succeeds', (shareType) => {
    let raw = [{ value: shareType === 'text' ? 'account A private text' : 'https://private.example/a', shareType }];
    const native = {
      getSharedPayloads: () => raw.map(item => ({ ...item })),
      clearSharedPayloads: vi.fn((): void => { throw new Error('App Group unavailable'); }),
    };
    const stop = watchIncomingShareAccount(native);
    setMobileAuthOwner('account-b');
    // Foreground refresh must not relabel the surviving native snapshot as B.
    receiveIncomingShare(native);
    const next = stageIncomingShareBatch([payload({ value: 'file:///new.pdf', contentUri: 'file:///new.pdf' })], vi.fn())!;
    expect(consumeIncomingShareBatch(next.id)).toBe(true);
    native.clearSharedPayloads.mockImplementation(() => { raw = []; });
    receiveIncomingShare(native);
    // Once clearing has succeeded, a deliberate identical share belongs to B.
    raw = [{ value: shareType === 'text' ? 'account A private text' : 'https://private.example/a', shareType }];
    receiveIncomingShare(native);
    const id = incomingShareBatchId([payload({
      ...(shareType === 'text'
        ? { value: raw[0]!.value, shareType, contentUri: null, contentType: 'text', originalName: raw[0]!.value }
        : { value: raw[0]!.value, shareType, contentUri: raw[0]!.value, contentType: 'website', originalName: 'a' }),
      contentSize: null, contentMimeType: null, mimeType: undefined,
    })]);
    expect(consumeIncomingShareBatch(id)).toBe(true);
    stop();
  });

  it('persists an unknown-slot fence when the account transition cannot read native storage', () => {
    const native = {
      getSharedPayloads: vi.fn(() => { throw new Error('App Group unavailable'); }),
      clearSharedPayloads: vi.fn(),
      rejectUnreadableSharedPayloads: vi.fn(),
    };
    const stop = watchIncomingShareAccount(native);
    setMobileAuthOwner('account-b');
    expect(native.rejectUnreadableSharedPayloads).toHaveBeenCalledOnce();
    expect(native.clearSharedPayloads).not.toHaveBeenCalled();
    stop();
  });

  it('clears an unobserved native slot on switch and allows new shares for the new account', () => {
    let raw = [{ value: 'file:///shared/report.pdf', shareType: 'file' as const }];
    const native = { getSharedPayloads: () => raw, clearSharedPayloads: vi.fn(() => { raw = []; }) };
    const stop = watchIncomingShareAccount(native);
    setMobileAuthOwner('account-b');
    expect(raw).toEqual([]);
    raw = [{ value: 'file:///shared/new.pdf', shareType: 'file' }];
    receiveIncomingShare(native);
    const batch = stageIncomingShareBatch([payload({
      value: raw[0]!.value, contentUri: raw[0]!.value, originalName: 'new.pdf',
      contentSize: null, contentMimeType: null, mimeType: undefined,
    })], vi.fn())!;
    expect(batch.owner.accountId).toBe('account-b');
    expect(consumeIncomingShareBatch(batch.id)).toBe(true);
    expect(raw).toEqual([]);
    stop();
  });

  it('does not reassign native files when login and switch happen during module loading', () => {
    setMobileAuthOwner(null);
    const initialOwner = getMobileAuthOwner();
    setMobileAuthOwner('account-a');
    setMobileAuthOwner('account-b');
    const native = { getSharedPayloads: () => [], clearSharedPayloads: vi.fn() };
    const stop = watchIncomingShareAccount(native, initialOwner);
    expect(native.clearSharedPayloads).toHaveBeenCalledOnce();
    stop();
  });

  it.each(['rollback', 'commit', 'realm', 'logout', 'logout-relogin'])(
    'settles unclaimed JS and native shares after switch %s without weakening cancellation',
    async (outcome) => {
      const before = getMobileAuthOwner();
      let raw = [{ value: 'file:///shared/report.pdf', shareType: 'file' as const }];
      const native = { getSharedPayloads: () => raw, clearSharedPayloads: vi.fn(() => { raw = []; }) };
      const stop = watchIncomingShareAccount(native);
      const acknowledge = vi.fn();
      const batch = stageIncomingShareBatch([payload({})], acknowledge)!;
      invalidateMobileAuthOwnerForSwitch();
      expect(getMobileAuthOwner()).toMatchObject({ accountId: '', accountKey: '', switching: true });
      expect(isMobileAuthOwnerCurrent(before)).toBe(false);
      expect(consumeIncomingShareBatch(batch.id)).toBe(false);
      expect(acknowledge).not.toHaveBeenCalled();
      expect(native.clearSharedPayloads).not.toHaveBeenCalled();
      await Promise.resolve();
      expect(deleteAsync).not.toHaveBeenCalled();

      if (outcome === 'rollback') setMobileAuthOwner('account-a');
      else if (outcome === 'commit') setMobileAuthOwner('account-b');
      else if (outcome === 'realm') setMobileAuthOwner('account-a', 'cn');
      else {
        setMobileAuthOwner(null);
        if (outcome === 'logout-relogin') setMobileAuthOwner('account-a');
      }
      expect(getMobileAuthOwner().switching).toBeUndefined();
      // Restoring a share must not revalidate any old auth-scoped operation.
      expect(isMobileAuthOwnerCurrent(before)).toBe(false);
      if (outcome === 'rollback') {
        expect(native.clearSharedPayloads).not.toHaveBeenCalled();
        expect(acknowledge).not.toHaveBeenCalled();
        expect(consumeIncomingShareBatch(batch.id)).toBe(true);
        expect(consumeIncomingShareBatch(batch.id)).toBe(false);
        expect(acknowledge).toHaveBeenCalledOnce();
        expect(deleteAsync).not.toHaveBeenCalled();
      } else {
        expect(raw).toEqual([]);
        expect(consumeIncomingShareBatch(batch.id)).toBe(false);
        expect(acknowledge).toHaveBeenCalledOnce();
        await vi.waitFor(() => expect(deleteAsync).toHaveBeenCalled());
      }
      stop();
    },
  );

  it.each(['rollback', 'commit'])('leaves shares arriving during a switch in native storage until %s', (outcome) => {
    let raw = [{ value: 'file:///shared/report.pdf', shareType: 'file' as const }];
    const native = { getSharedPayloads: () => raw, clearSharedPayloads: vi.fn(() => { raw = []; }) };
    const stop = watchIncomingShareAccount(native);
    invalidateMobileAuthOwnerForSwitch();
    expect(stageIncomingShareBatch([payload({})], vi.fn())).toBeNull();
    receiveIncomingShare(native);
    expect(native.clearSharedPayloads).not.toHaveBeenCalled();
    setMobileAuthOwner(outcome === 'rollback' ? 'account-a' : 'account-b');
    if (outcome === 'rollback') {
      receiveIncomingShare(native);
      const batch = stageIncomingShareBatch([payload({})], vi.fn())!;
      expect(batch.owner.accountId).toBe('account-a');
      // receive uses raw metadata, so consume that batch first.
      const rawId = incomingShareBatchId([payload({ mimeType: undefined, contentMimeType: null, contentSize: null })]);
      expect(consumeIncomingShareBatch(rawId)).toBe(true);
    }
    expect(raw).toEqual([]);
    stop();
  });

  it.each(['account-a', 'account-b'])('fails closed when the native watcher first mounts mid-switch, settling to %s', (account) => {
    invalidateMobileAuthOwnerForSwitch();
    const initial = getMobileAuthOwner();
    const native = { getSharedPayloads: () => [], clearSharedPayloads: vi.fn() };
    const stop = watchIncomingShareAccount(native, initial);
    expect(native.clearSharedPayloads).not.toHaveBeenCalled();
    setMobileAuthOwner(account);
    expect(native.clearSharedPayloads).toHaveBeenCalledOnce();
    stop();
  });
});