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。

8.3 KB
Raw Blame
import { readFileSync } from "node:fs";
import { resolve } from "node:path";
import { describe, expect, it } from "vitest";
import {
  buildIosActionSheetSpec,
  resolveIosActionSheetResult,
} from "@/platform/chrome/actionMenuModel";

const readTextLf = (...args: Parameters<typeof readFileSync>): string =>
  String(readFileSync(...args)).replace(/\r\n/g, "\n");

describe("iOS chrome action menu model", () => {
  it("pins cancel last, drops disabled items, and marks the destructive index", () => {
    const spec = buildIosActionSheetSpec({
      cancelLabel: "取消",
      items: [
        { key: "rename", label: "重命名" },
        { key: "rewind", label: "回到此处", disabled: true },
        { key: "delete", label: "删除", destructive: true },
      ],
      title: "更多",
    });

    expect(spec.options).toEqual(["重命名", "删除", "取消"]);
    expect(spec.cancelButtonIndex).toBe(2);
    expect(spec.destructiveButtonIndex).toBe(1);
    expect(spec.enabledKeys).toEqual(["rename", "delete"]);
    expect(spec.title).toBe("更多");
  });

  it("resolves cancel, action, and out-of-range taps", () => {
    const spec = buildIosActionSheetSpec({
      cancelLabel: "Cancel",
      items: [
        { key: "copy", label: "Copy" },
        { key: "delete", label: "Delete", destructive: true },
      ],
    });

    expect(resolveIosActionSheetResult(spec, 0)).toEqual({
      kind: "action",
      key: "copy",
    });
    expect(resolveIosActionSheetResult(spec, 1)).toEqual({
      kind: "action",
      key: "delete",
    });
    expect(resolveIosActionSheetResult(spec, 2)).toEqual({ kind: "cancel" });
    expect(resolveIosActionSheetResult(spec, -1)).toEqual({ kind: "cancel" });
    expect(resolveIosActionSheetResult(spec, 9)).toEqual({ kind: "cancel" });
  });
});

describe("iOS chrome presenters stay on the system menu path", () => {
  it("opens session / message / chip / language menus through showActionMenu", () => {
    const sessionActions = readTextLf(
      resolve(process.cwd(), "src/session/useSessionListActions.ts"),
      "utf8",
    );
    const messageRenderer = readTextLf(
      resolve(process.cwd(), "src/session/MessageRenderer.tsx"),
      "utf8",
    );
    const sessionScreen = readTextLf(
      resolve(process.cwd(), "app/sessions/[sessionId].tsx"),
      "utf8",
    );
    const settings = readTextLf(
      resolve(process.cwd(), "app/settings.tsx"),
      "utf8",
    );
    const home = readTextLf(
      resolve(process.cwd(), "src/session/HomeSurface.tsx"),
      "utf8",
    );
    const pullDown = readTextLf(
      resolve(process.cwd(), "src/platform/chrome/NativePullDownMenu.tsx"),
      "utf8",
    );

    expect(sessionActions).toContain("setActionSheetSession(session)");
    expect(sessionActions).not.toContain("showActionMenu({");
    expect(sessionActions).toContain("action === 'rename' || action === 'delete'");
    expect(sessionActions).not.toContain("Platform.OS !== 'ios'");
    const expoSheet = readTextLf(
      resolve(process.cwd(), "src/session/SessionOptionsExpoSheet.tsx"),
      "utf8",
    );
    expect(expoSheet).toMatch(/from ['"]@expo\/ui\/swift-ui['"]/);
    expect(expoSheet).toContain("<ComposerSheet");
    expect(expoSheet).toContain("<Section>");
    expect(expoSheet).toContain("<ComposerNativeRow");
    expect(expoSheet).toContain("onClosed={onClosed}");
    const nativeSheet = readTextLf(resolve(process.cwd(), "src/session/ComposerSheet.ios.tsx"), "utf8");
    expect(nativeSheet).toContain("<BottomSheet");
    expect(nativeSheet).toContain("onDismiss={onClosed}");
    expect(expoSheet).not.toContain("snapPoints");
    expect(home).toContain("<SessionOptionsPresenter");
    const actionMenu = readTextLf(
      resolve(process.cwd(), "src/platform/chrome/showActionMenu.ts"),
      "utf8",
    );
    expect(actionMenu).toContain("showIosBottomActionSheet");
    expect(actionMenu).toMatch(/from ['"]xdt-ios-action-sheet['"]/);
    expect(messageRenderer).toContain("<NativePullDownMenu");
    expect(sessionScreen).toContain("usesSystemActionMenu()");
    expect(settings).toContain("<NativePullDownMenu");
    expect(home).toContain("<NativePullDownMenu");
    expect(home).toContain('testID="devices.title"');
    expect(home).toContain("<HomeNativeStackHeader");
    expect(home).toContain("filterActions={searchFilterMenu.filterActions}");
    const searchBar = readTextLf(
      resolve(process.cwd(), "src/session/HomeSearchBar.tsx"),
      "utf8",
    );
    expect(searchBar).toContain("<NativePullDownMenu");
    expect(pullDown).toContain("MenuView");
    expect(pullDown).toContain("usesNativePullDownMenu");
    const simpleHeader = readTextLf(
      resolve(process.cwd(), "src/platform/chrome/SimpleStackHeader.tsx"),
      "utf8",
    );
    const iosTitle = simpleHeader.slice(
      simpleHeader.indexOf("headerTitle: () =>"),
      simpleHeader.indexOf("headerLeft"),
    );
    expect(iosTitle).toContain("{title}");
    expect(iosTitle).not.toContain("eyebrow");
    expect(iosTitle).not.toContain("subtitle");
    // Android 自绘 ScreenHeader 跟随 iOS:同样不显示 eyebrow / subtitle。
    expect(simpleHeader).not.toContain("eyebrow={eyebrow}");
    expect(simpleHeader).not.toContain("subtitle={subtitle}");
  });

  it("keeps Android fallback sheets and Maestro header anchors", () => {
    const sessionSheet = readTextLf(
      resolve(process.cwd(), "src/session/SessionActionSheet.tsx"),
      "utf8",
    );
    const messageSheet = readTextLf(
      resolve(process.cwd(), "src/session/MessageActionSheet.tsx"),
      "utf8",
    );
    const chipSheet = readTextLf(
      resolve(process.cwd(), "src/session/ChatFileChipMenuSheet.tsx"),
      "utf8",
    );
    const settings = readTextLf(
      resolve(process.cwd(), "app/settings.tsx"),
      "utf8",
    );
    const accountDeletion = readTextLf(
      resolve(process.cwd(), "app/account-deletion.tsx"),
      "utf8",
    );
    const deviceDetail = readTextLf(
      resolve(process.cwd(), "app/devices/[deviceId].tsx"),
      "utf8",
    );

    expect(sessionSheet).toContain("<SessionActionSheetFrame");
    for (const file of [
      "SessionActionSheetFrame.tsx",
      "SessionActionSheetFrame.android.tsx",
    ]) {
      expect(
        readTextLf(resolve(process.cwd(), "src/session", file), "utf8"),
      ).toContain('testID="home.sessionActions"');
    }
    expect(messageSheet).toContain("<SheetModal");
    expect(chipSheet).toContain('testID="session.chipMenu"');
    expect(settings).toContain('backTestID="settings.backButton"');
    expect(settings).toContain('titleTestID="settings.title"');
    expect(settings).toContain("<SimpleStackHeader");
    expect(settings).not.toContain("ScreenHeader");
    expect(readTextLf(resolve(process.cwd(), "app/settings/voice-dictionary.tsx"), "utf8"))
      .toContain('backTestID="settings.voiceDictionary.backButton"');
    expect(readTextLf(resolve(process.cwd(), "app/settings/device-name.tsx"), "utf8"))
      .toContain('backTestID="settings.renameSelfDevice.backButton"');
    expect(accountDeletion).toContain("<SimpleStackHeader");
    expect(accountDeletion).toContain(
      'backTestID="accountDeletion.backButton"',
    );
    expect(deviceDetail).not.toContain('eyebrow=');
    expect(deviceDetail).toContain("<SimpleStackHeader");
    expect(deviceDetail).toContain('backTestID="deviceDetail.backButton"');
    expect(deviceDetail).toContain("<SessionOptionsPresenter");
    const nativeSwitch = readTextLf(
      resolve(process.cwd(), "src/platform/chrome/NativeSwitch.tsx"),
      "utf8",
    );
    expect(nativeSwitch).toContain("accessibilityLabel?: string");
    expect(nativeSwitch).toContain("onAccessibilityTap");
    expect(nativeSwitch).toContain("usesExpoNativeSwitch");
    expect(nativeSwitch).toContain(
      'Platform.OS === "ios" || Platform.OS === "android"',
    );
    expect(nativeSwitch).toContain("Switch as RNSwitch");
    expect(nativeSwitch).toContain("if (!usesExpoNativeSwitch())");
    expect(settings).toContain(
      "accessibilityLabel={t('settings.notifications.taskDone')}",
    );
    // Beta 渠道开关在二级页「开发者」。
    expect(readTextLf(resolve(process.cwd(), "app/settings/developer.tsx"), "utf8")).toContain(
      "accessibilityLabel={t('settings.betaChannel.title')}",
    );
    expect(settings).toContain(
      "accessibilityLabel={t('settings.legal.analytics')}",
    );
  });
});