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。

6.1 KB
Raw Blame
// @vitest-environment jsdom
import { act, createElement } from "react";
import { createRoot } from "react-dom/client";
import { beforeEach, afterEach, it, expect, vi } from "vitest";
const h = vi.hoisted(() => ({
  invoke: vi.fn(),
  push: null as null | ((device: string) => void),
  view: null as any,
  unresponsive: new Set<string>(),
  context: {
    connectionEpoch: 1,
    status: "online",
    recoveringDeviceIds: new Set<string>(),
    subscribe: vi.fn(async () => {}),
    unsubscribe: vi.fn(async () => {}),
  },
}));
vi.mock("@/device-link/unresponsiveDevicesStore", () => ({
  useUnresponsiveDevices: () => h.unresponsive,
}));
vi.mock("react-native", () => ({
  AppState: { addEventListener: () => ({ remove: vi.fn() }) },
}));
vi.mock("@/device-link/DeviceLinkContext", () => ({
  useDeviceLink: () => ({ ...h.context, invoke: h.invoke }),
  subscribeRemoteFavoritesChanged: (fn: any) => {
    h.push = fn;
    return () => {
      h.push = null;
    };
  },
}));
import {
  useRemoteMobileFavorites,
  mobileFavorite,
  mobileFavoriteMutation,
  __resetRemoteMobileFavoritesCacheForTest,
} from "@/session/useRemoteMobileFavorites";
const item = {
  uid: "fav-1",
  providerId: "account",
  modelId: "model",
  agent: "cc" as const,
  effort: "high",
};
function Probe({ device = "a" }: { device?: string }) {
  h.view = useRemoteMobileFavorites(JSON.stringify(["user", device]), true);
  return null;
}
let root: ReturnType<typeof createRoot>;
beforeEach(() => {
  (globalThis as any).IS_REACT_ACT_ENVIRONMENT = true;
  h.invoke.mockReset().mockResolvedValue([item]);
  h.context.status = "online";
  h.context.recoveringDeviceIds.clear();
  h.unresponsive.clear();
  __resetRemoteMobileFavoritesCacheForTest();
  root = createRoot(document.createElement("div"));
});
afterEach(() => act(() => root.unmount()));
it.each(['peer', 'unresponsive'])('retains favorites and refreshes after %s recovery without a relay reconnect', async kind => {
  await act(async () => root.render(createElement(Probe, {})));
  h.invoke.mockRejectedValueOnce(new Error('temporarily offline'));
  await act(async () => h.push?.('a'));
  expect(h.view.ready).toBe(true);
  expect(h.view.items).toEqual([mobileFavorite(item)]);
  const devices = kind === 'peer' ? h.context.recoveringDeviceIds : h.unresponsive;
  devices.add('a');
  await act(async () => root.render(createElement(Probe, {})));
  const calls = h.invoke.mock.calls.length;
  devices.delete('a');
  h.invoke.mockResolvedValue([{ ...item, effort: 'low' }]);
  await act(async () => root.render(createElement(Probe, {})));
  expect(h.invoke).toHaveBeenCalledTimes(calls + 1);
  expect(h.view.items[0].effort).toBe('low');
  expect(h.view.error).toBeNull();
});
// 回归:任务页 / 新建页每次挂载选择器都从空列表开始,等读回来「收藏」才出现。
// 重新挂载时先显示上次读到的收藏,后台读到新的再替换。
it("a freshly mounted picker shows the last favorites while the fresh read is in flight", async () => {
  await act(async () => root.render(createElement(Probe, {})));
  act(() => root.unmount());
  root = createRoot(document.createElement("div"));
  let resolve!: (v: unknown) => void;
  h.invoke.mockImplementationOnce(() => new Promise((r) => { resolve = r; }));
  await act(async () => root.render(createElement(Probe, {})));
  expect(h.view.ready).toBe(true);
  expect(h.view.items).toEqual([mobileFavorite(item)]);
  await act(async () => resolve([{ ...item, effort: "low" }]));
  expect(h.view.items[0].effort).toBe("low");
});
it("maps engine spelling and treats unchanged field order as no operation", () => {
  const mobile = mobileFavorite(item);
  expect(mobile.agent).toBe("claude-code");
  expect(
    mobileFavoriteMutation(
      [item],
      [
        {
          agent: mobile.agent,
          modelId: mobile.modelId,
          providerId: mobile.providerId,
          effort: "high",
          fast: false,
          uid: "fav-1",
        },
      ],
    ),
  ).toBeNull();
  expect(
    mobileFavoriteMutation([item], [{ ...mobile, fast: true }]),
  ).toMatchObject({
    kind: "update",
    expected: item,
    item: { agent: "cc", fast: true },
  });
  expect(mobileFavoriteMutation([item], [])).toEqual({
    kind: "remove",
    expected: item,
  });
});
it("reads only the controlled computer and reloads on its invalidation", async () => {
  await act(async () => root.render(createElement(Probe, {})));
  expect(h.invoke).toHaveBeenCalledWith("a", "maker:model-favorites:get", []);
  expect(h.view.items).toEqual([mobileFavorite(item)]);
  await act(async () => h.push?.("b"));
  expect(h.invoke).toHaveBeenCalledTimes(1);
  h.invoke.mockResolvedValue([]);
  await act(async () => h.push?.("a"));
  expect(h.view.items).toEqual([]);
});
it("discards a late snapshot when the target changes", async () => {
  let resolve!: (v: unknown) => void;
  h.invoke.mockImplementationOnce(
    () =>
      new Promise((r) => {
        resolve = r;
      }),
  );
  await act(async () => root.render(createElement(Probe, {})));
  h.invoke.mockResolvedValue([{ ...item, providerId: "other" }]);
  await act(async () => root.render(createElement(Probe, { device: "b" })));
  await act(async () => resolve([item]));
  expect(h.view.items[0].providerId).toBe("other");
});
it("writes a single mutation and re-reads after confirmed save", async () => {
  await act(async () => root.render(createElement(Probe, {})));
  h.invoke.mockImplementation(async (_device, channel) =>
    channel.endsWith(":apply") ? [] : [],
  );
  await act(async () => h.view.save([]));
  expect(h.invoke).toHaveBeenCalledWith("a", "maker:model-favorites:apply", [
    { kind: "remove", expected: item },
  ]);
  expect(h.view.items).toEqual([]);
});
it("old hosts do not fall back to local favorites or claim successful saves", async () => {
  h.invoke.mockRejectedValue(new Error("CHANNEL_NOT_ALLOWED"));
  await act(async () => root.render(createElement(Probe, {})));
  expect(h.view.items).toEqual([]);
  expect(h.view.error).toBeTruthy();
  expect(h.view.ready).toBe(false);
  await expect(h.view.save([])).resolves.toBeUndefined();
  await expect(h.view.save([mobileFavorite(item)])).rejects.toThrow(
    "not ready",
  );
});