File size: 2,376 Bytes
f778c12
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import { afterEach, expect, it, vi } from "vitest";
import { createOpenClawTestState } from "../../test-utils/openclaw-test-state.js";
import { modelsAuthActivateCommand } from "./auth-activate.js";

const mocks = vi.hoisted(() => ({ callGateway: vi.fn() }));
vi.mock("../../gateway/call.js", () => ({ callGateway: mocks.callGateway }));
vi.mock("../../system-agent/setup-inference.js", () => ({
  activateSetupInference: async () => ({
    ok: true,
    modelRef: "sample/chat",
    latencyMs: 1,
    lines: ["Connection verified"],
  }),
}));
vi.mock("./auth-refresh.js", () => ({
  refreshRunningGatewayAuthState: async () => "refreshed",
}));
afterEach(() => vi.clearAllMocks());

it.each([
  { name: "applied account", appliedHash: "saved", profile: "replacement", activated: true },
  { name: "reload disabled", appliedHash: "old", profile: "replacement", activated: false },
  { name: "another account selected", appliedHash: "saved", profile: "other", activated: false },
  { name: "lost acknowledgement", appliedHash: null, profile: "replacement", activated: false },
])(
  "reports the saved sign-in accurately with $name",
  async ({ appliedHash, profile, activated }) => {
    const state = await createOpenClawTestState({ label: "activate-saved-sign-in" });
    try {
      await state.writeConfig({ agents: { entries: { main: { workspace: state.workspaceDir } } } });
      const runtime = { log: vi.fn(), error: vi.fn(), exit: vi.fn() };
      if (appliedHash === null) {
        mocks.callGateway.mockRejectedValue(new Error("connection closed"));
      } else {
        mocks.callGateway.mockResolvedValue({
          config: { agents: { entries: { main: { model: `sample/chat@${profile}` } } } },
          configRevisionHash: "saved",
          appliedConfigHash: appliedHash,
        });
      }
      await modelsAuthActivateCommand({ profileId: "replacement", agent: "main" }, runtime);
      const output = runtime.log.mock.calls.flat().join("\n");
      expect(output).toContain("Connection verified");
      if (activated) {
        expect(output).toContain("Saved sign-in activated");
      } else {
        expect(output).not.toContain("Saved sign-in activated");
        expect(output).toContain("verified and saved");
        expect(output).toContain("openclaw gateway restart");
      }
    } finally {
      await state.cleanup();
    }
  },
);