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。

3.3 KB
Raw Blame
// @vitest-environment jsdom
import { cleanup, render } from '@testing-library/react';
import { useMemo, type ReactNode } from 'react';
import ts from 'typescript';
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';

const seenScreenOptions: unknown[] = [];

function Screen({ options }: { options: unknown }) {
  seenScreenOptions.push(options);
  return null;
}

function Toolbar({ children }: { children?: ReactNode }) {
  return <>{children}</>;
}
Toolbar.Button = () => null;

vi.mock('expo-router', () => ({
  Stack: { Screen, Toolbar },
}));
vi.mock('react-native', () => ({
  Platform: { OS: 'ios' },
}));
vi.mock('@/platform/AdaptiveWindowContext', () => ({
  useAdaptiveWindow: () => ({ barEdge: 'top' }),
}));
vi.mock('@/theme', () => ({
  useTheme: () => ({ colors: { textPrimary: '#111111' } }),
}));

import { SystemNavigationBack } from '@/platform/chrome/SystemNavigationBack';

beforeEach(() => {
  seenScreenOptions.length = 0;
});

afterEach(() => cleanup());

describe('navigation option stability', () => {
  it('retains system-back options through unrelated rerenders and replaces them for option inputs', () => {
    const view = render(
      <SystemNavigationBack label="Back" onPress={() => undefined} />,
    );
    const initialOptions = seenScreenOptions.at(-1);

    view.rerender(
      <SystemNavigationBack label="Return" disabled onPress={() => undefined} />,
    );
    expect(seenScreenOptions.at(-1)).toBe(initialOptions);

    view.rerender(
      <SystemNavigationBack available={false} label="Return" disabled onPress={() => undefined} />,
    );
    expect(seenScreenOptions.at(-1)).not.toBe(initialOptions);
  });

  it('keeps the login screen status-bar options memoized by its only visual input', async () => {
    const { readFile } = await import('node:fs/promises');
    const { resolve } = await import('node:path');
    const source = await readFile(resolve(process.cwd(), 'app/(auth)/login.tsx'), 'utf8');

    const tree = ts.createSourceFile('login.tsx', source, ts.ScriptTarget.Latest, true, ts.ScriptKind.TSX);
    let initializer: ts.Expression | undefined;
    function visit(node: ts.Node) {
      if (ts.isVariableDeclaration(node) && node.name.getText(tree) === 'loginStatusBarOptions') initializer = node.initializer;
      ts.forEachChild(node, visit);
    }
    visit(tree);
    if (!initializer) throw new Error('Missing production login status-bar options');
    const compiled = ts.transpileModule('const options = ' + initializer.getText(tree) + ';', { compilerOptions: { target: ts.ScriptTarget.ES2022 } }).outputText;
    const optionsFor = new Function('useMemo', 'stageTheme', compiled + '\nreturn options;');
    function LoginOptions({ stageTheme }: { stageTheme: 'light' | 'dark' }) {
      return <Screen options={optionsFor(useMemo, stageTheme)} />;
    }
    const view = render(<LoginOptions stageTheme="light" />);
    const initial = seenScreenOptions.at(-1);
    expect(initial).toMatchObject({ statusBarStyle: 'dark' });
    view.rerender(<LoginOptions stageTheme="light" />);
    expect(seenScreenOptions.at(-1)).toBe(initial);
    view.rerender(<LoginOptions stageTheme="dark" />);
    expect(seenScreenOptions.at(-1)).not.toBe(initial);
    expect(seenScreenOptions.at(-1)).toMatchObject({ statusBarStyle: 'light' });
  });
});