



























@@ -31,7 +31,7 @@ describe("handleControlUiHttpRequest", () => {
3131}
32323333function parseBootstrapPayload(end: ReturnType<typeof makeMockHttpResponse>["end"]) {
34-return JSON.parse(String(end.mock.calls.at(0)?.[0] ?? "")) as {
34+return JSON.parse(responseBody(end)) as {
3535basePath: string;
3636assistantName: string;
3737assistantAvatar: string;
@@ -41,6 +41,18 @@ describe("handleControlUiHttpRequest", () => {
4141};
4242}
434344+function responseBody(end: ReturnType<typeof makeMockHttpResponse>["end"]) {
45+return String(end.mock.calls.at(0)?.[0] ?? "");
46+}
47+48+function responseJson(end: ReturnType<typeof makeMockHttpResponse>["end"]) {
49+return JSON.parse(responseBody(end)) as unknown;
50+}
51+52+function firstEndCallLength(end: ReturnType<typeof makeMockHttpResponse>["end"]) {
53+return end.mock.calls.at(0)?.length ?? -1;
54+}
55+4456function expectNotFoundResponse(params: {
4557handled: boolean;
4658res: ReturnType<typeof makeMockHttpResponse>["res"];
@@ -214,7 +226,7 @@ describe("handleControlUiHttpRequest", () => {
214226}) {
215227expect(params.handled).toBe(true);
216228expect(params.res.statusCode).toBe(403);
217-expect(JSON.parse(String(params.end.mock.calls.at(0)?.[0] ?? ""))).toEqual({
229+expect(responseJson(params.end)).toEqual({
218230ok: false,
219231error: {
220232type: "forbidden",
@@ -372,7 +384,7 @@ describe("handleControlUiHttpRequest", () => {
372384});
373385expect(handled).toBe(true);
374386expect(res.statusCode).toBe(200);
375-const payload = JSON.parse(String(end.mock.calls.at(0)?.[0] ?? "")) as {
387+const payload = responseJson(end) as {
376388available?: boolean;
377389mediaTicket?: string;
378390mediaTicketExpiresAt?: string;
@@ -414,7 +426,7 @@ describe("handleControlUiHttpRequest", () => {
414426});
415427expect(handled).toBe(true);
416428expect(res.statusCode).toBe(200);
417-const payload = JSON.parse(String(end.mock.calls.at(0)?.[0] ?? "")) as {
429+const payload = responseJson(end) as {
418430available?: boolean;
419431mediaTicket?: string;
420432mediaTicketExpiresAt?: string;
@@ -440,7 +452,7 @@ describe("handleControlUiHttpRequest", () => {
440452authorization: "Bearer test-token",
441453},
442454});
443-const payload = JSON.parse(String(meta.end.mock.calls.at(0)?.[0] ?? "")) as {
455+const payload = responseJson(meta.end) as {
444456mediaTicket?: string;
445457};
446458expect(meta.handled).toBe(true);
@@ -472,7 +484,7 @@ describe("handleControlUiHttpRequest", () => {
472484authorization: "Bearer test-token",
473485},
474486});
475-const payload = JSON.parse(String(meta.end.mock.calls.at(0)?.[0] ?? "")) as {
487+const payload = responseJson(meta.end) as {
476488mediaTicket?: string;
477489};
478490@@ -483,7 +495,7 @@ describe("handleControlUiHttpRequest", () => {
483495});
484496expect(refresh.handled).toBe(true);
485497expect(refresh.res.statusCode).toBe(401);
486-expect(String(refresh.end.mock.calls.at(0)?.[0] ?? "")).toContain("Unauthorized");
498+expect(responseBody(refresh.end)).toContain("Unauthorized");
487499},
488500});
489501});
@@ -501,7 +513,7 @@ describe("handleControlUiHttpRequest", () => {
501513});
502514expect(handled).toBe(true);
503515expect(res.statusCode).toBe(401);
504-expect(String(end.mock.calls.at(0)?.[0] ?? "")).toContain("Unauthorized");
516+expect(responseBody(end)).toContain("Unauthorized");
505517},
506518});
507519});
@@ -514,7 +526,7 @@ describe("handleControlUiHttpRequest", () => {
514526});
515527expect(handled).toBe(true);
516528expect(res.statusCode).toBe(200);
517-expect(JSON.parse(String(end.mock.calls.at(0)?.[0] ?? ""))).toEqual({
529+expect(responseJson(end)).toEqual({
518530available: false,
519531code: "outside-allowed-folders",
520532reason: "Outside allowed folders",
@@ -534,7 +546,7 @@ describe("handleControlUiHttpRequest", () => {
534546});
535547expect(handled).toBe(true);
536548expect(res.statusCode).toBe(401);
537-expect(String(end.mock.calls.at(0)?.[0] ?? "")).toContain("Unauthorized");
549+expect(responseBody(end)).toContain("Unauthorized");
538550},
539551});
540552});
@@ -598,7 +610,7 @@ describe("handleControlUiHttpRequest", () => {
598610});
599611expect(handled).toBe(true);
600612expect(res.statusCode).toBe(401);
601-expect(String(end.mock.calls.at(0)?.[0] ?? "")).toContain("Unauthorized");
613+expect(responseBody(end)).toContain("Unauthorized");
602614},
603615});
604616});
@@ -719,7 +731,7 @@ describe("handleControlUiHttpRequest", () => {
719731});
720732expect(handled).toBe(true);
721733expect(res.statusCode).toBe(401);
722-expect(String(end.mock.calls.at(0)?.[0] ?? "")).toContain("Unauthorized");
734+expect(responseBody(end)).toContain("Unauthorized");
723735},
724736});
725737});
@@ -805,7 +817,7 @@ describe("handleControlUiHttpRequest", () => {
805817806818expect(handled).toBe(true);
807819expect(res.statusCode).toBe(200);
808-expect(String(end.mock.calls.at(0)?.[0] ?? "")).toBe("avatar-bytes\n");
820+expect(responseBody(end)).toBe("avatar-bytes\n");
809821} finally {
810822await fs.rm(tmp, { recursive: true, force: true });
811823}
@@ -876,7 +888,7 @@ describe("handleControlUiHttpRequest", () => {
876888877889expect(handled).toBe(true);
878890expect(res.statusCode).toBe(200);
879-expect(String(end.mock.calls.at(0)?.[0] ?? "")).toBe("avatar-bytes\n");
891+expect(responseBody(end)).toBe("avatar-bytes\n");
880892} finally {
881893await fs.rm(tmp, { recursive: true, force: true });
882894}
@@ -901,7 +913,7 @@ describe("handleControlUiHttpRequest", () => {
901913902914expect(handled).toBe(true);
903915expect(res.statusCode).toBe(200);
904-expect(JSON.parse(String(end.mock.calls.at(0)?.[0] ?? ""))).toEqual({
916+expect(responseJson(end)).toEqual({
905917avatarUrl: "https://example.com/avatar.png",
906918avatarSource: "remote URL",
907919avatarStatus: "remote",
@@ -922,7 +934,7 @@ describe("handleControlUiHttpRequest", () => {
922934923935expect(handled).toBe(true);
924936expect(res.statusCode).toBe(200);
925-expect(JSON.parse(String(end.mock.calls.at(0)?.[0] ?? ""))).toEqual({
937+expect(responseJson(end)).toEqual({
926938avatarUrl: null,
927939avatarSource: null,
928940avatarStatus: "none",
@@ -940,7 +952,7 @@ describe("handleControlUiHttpRequest", () => {
940952941953expect(handled).toBe(true);
942954expect(res.statusCode).toBe(401);
943-expect(String(end.mock.calls.at(0)?.[0] ?? "")).toContain("Unauthorized");
955+expect(responseBody(end)).toContain("Unauthorized");
944956});
945957946958it("rejects trusted-proxy avatar metadata requests without operator.read scope", async () => {
@@ -995,7 +1007,7 @@ describe("handleControlUiHttpRequest", () => {
99510079961008expect(handled).toBe(true);
9971009expect(res.statusCode).toBe(200);
998-expect(String(end.mock.calls.at(0)?.[0] ?? "")).toBe("inside-ok\n");
1010+expect(responseBody(end)).toBe("inside-ok\n");
9991011},
10001012});
10011013});
@@ -1013,7 +1025,7 @@ describe("handleControlUiHttpRequest", () => {
1013102510141026expect(handled).toBe(true);
10151027expect(res.statusCode).toBe(200);
1016-expect(end.mock.calls.at(0)?.length ?? -1).toBe(0);
1028+expect(firstEndCallLength(end)).toBe(0);
10171029},
10181030});
10191031});
@@ -1096,7 +1108,7 @@ describe("handleControlUiHttpRequest", () => {
1096110810971109expect(handled).toBe(true);
10981110expect(res.statusCode).toBe(200);
1099-expect(String(end.mock.calls.at(0)?.[0] ?? "")).toBe("console.log('hi');");
1111+expect(responseBody(end)).toBe("console.log('hi');");
11001112},
11011113});
11021114});
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。