




















@@ -110,6 +110,15 @@ function mockRotateOperatorTokenSuccess(): void {
110110});
111111}
112112113+function expectRespondedErrorMessage(opts: GatewayRequestHandlerOptions, message: string): void {
114+const respond = opts.respond as ReturnType<typeof vi.fn>;
115+expect(respond).toHaveBeenCalledTimes(1);
116+const call = respond.mock.calls[0] as unknown as [boolean, unknown, { message?: string }];
117+expect(call[0]).toBe(false);
118+expect(call[1]).toBeUndefined();
119+expect(call[2]?.message).toBe(message);
120+}
121+113122describe("deviceHandlers", () => {
114123beforeEach(() => {
115124vi.clearAllMocks();
@@ -138,11 +147,7 @@ describe("deviceHandlers", () => {
138147await deviceHandlers["device.pair.remove"](opts);
139148140149expect(opts.context.disconnectClientsForDevice).not.toHaveBeenCalled();
141-expect(opts.respond).toHaveBeenCalledWith(
142-false,
143-undefined,
144-expect.objectContaining({ message: "unknown deviceId" }),
145-);
150+expectRespondedErrorMessage(opts, "unknown deviceId");
146151});
147152148153it("rejects removing another device from a non-admin device session", async () => {
@@ -155,11 +160,7 @@ describe("deviceHandlers", () => {
155160await deviceHandlers["device.pair.remove"](opts);
156161157162expect(removePairedDeviceMock).not.toHaveBeenCalled();
158-expect(opts.respond).toHaveBeenCalledWith(
159-false,
160-undefined,
161-expect.objectContaining({ message: "device pairing removal denied" }),
162-);
163+expectRespondedErrorMessage(opts, "device pairing removal denied");
163164});
164165165166it("treats normalized device ids as self-owned for paired device removal", async () => {
@@ -393,11 +394,7 @@ describe("deviceHandlers", () => {
393394callerScopes: ["operator.pairing"],
394395});
395396expect(opts.context.disconnectClientsForDevice).not.toHaveBeenCalled();
396-expect(opts.respond).toHaveBeenCalledWith(
397-false,
398-undefined,
399-expect.objectContaining({ message: "device token rotation denied" }),
400-);
397+expectRespondedErrorMessage(opts, "device token rotation denied");
401398});
402399403400it("does not disconnect clients when token revocation fails", async () => {
@@ -410,11 +407,7 @@ describe("deviceHandlers", () => {
410407await deviceHandlers["device.token.revoke"](opts);
411408412409expect(opts.context.disconnectClientsForDevice).not.toHaveBeenCalled();
413-expect(opts.respond).toHaveBeenCalledWith(
414-false,
415-undefined,
416-expect.objectContaining({ message: "device token revocation denied" }),
417-);
410+expectRespondedErrorMessage(opts, "device token revocation denied");
418411});
419412420413it("filters pairing list to the caller device for non-admin device sessions", async () => {
@@ -605,11 +598,7 @@ describe("deviceHandlers", () => {
605598await deviceHandlers["device.pair.approve"](opts);
606599607600expect(approveDevicePairingMock).not.toHaveBeenCalled();
608-expect(opts.respond).toHaveBeenCalledWith(
609-false,
610-undefined,
611-expect.objectContaining({ message: "device pairing approval denied" }),
612-);
601+expectRespondedErrorMessage(opts, "device pairing approval denied");
613602});
614603615604it("allows admins to approve another device", async () => {
@@ -717,11 +706,7 @@ describe("deviceHandlers", () => {
717706await deviceHandlers["device.pair.reject"](opts);
718707719708expect(rejectDevicePairingMock).not.toHaveBeenCalled();
720-expect(opts.respond).toHaveBeenCalledWith(
721-false,
722-undefined,
723-expect.objectContaining({ message: "device pairing rejection denied" }),
724-);
709+expectRespondedErrorMessage(opts, "device pairing rejection denied");
725710});
726711727712it("allows rejecting the caller device from a non-admin device session", async () => {
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。