@estebanforge/pi-zendesk-me 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,143 @@
1
+ import { beforeEach, describe, expect, it, vi } from "vitest";
2
+ import { ZendeskApiError, zdGet } from "../lib/api";
3
+ import { _resetAuthCache } from "../lib/auth";
4
+ import {
5
+ TOKEN_OK,
6
+ fetchMock,
7
+ isTokenRequest,
8
+ jsonResponse,
9
+ stubZendeskEnv,
10
+ } from "./_helpers";
11
+
12
+ beforeEach(() => {
13
+ vi.unstubAllEnvs();
14
+ _resetAuthCache();
15
+ stubZendeskEnv();
16
+ });
17
+
18
+ // Route token requests to a canned token; tests match API paths separately.
19
+ function route(handler: (url: string) => Response | Promise<Response>) {
20
+ return fetchMock(async (url) => {
21
+ if (isTokenRequest(url)) return jsonResponse(TOKEN_OK);
22
+ return handler(url);
23
+ });
24
+ }
25
+
26
+ describe("zdGet", () => {
27
+ it("sends a Bearer header to the right API v2 URL", async () => {
28
+ const mock = route((url) =>
29
+ url.includes("/api/v2/tickets/7942.json")
30
+ ? jsonResponse({ ticket: { id: 7942 } })
31
+ : jsonResponse({}, 404),
32
+ );
33
+
34
+ const resp = await zdGet<{ ticket: { id: number } }>(
35
+ "tickets/7942.json",
36
+ { query: { include: "users,organizations" } },
37
+ );
38
+
39
+ expect(resp.ticket.id).toBe(7942);
40
+ const [url, init] = mock.mock.calls.find(
41
+ (c) => !isTokenRequest(String(c[0])),
42
+ ) as unknown as [string, RequestInit];
43
+ expect(url).toContain("https://{subdomain}.zendesk.com/api/v2/tickets/7942.json");
44
+ expect(url).toContain("include=");
45
+ expect((init.headers as Record<string, string>).Authorization).toBe(
46
+ "Bearer test-access-token",
47
+ );
48
+ });
49
+
50
+ it("re-tokens once on 401 invalid_token and retries", async () => {
51
+ let apiCalls = 0;
52
+ const mock = fetchMock((url) => {
53
+ if (isTokenRequest(url)) return jsonResponse(TOKEN_OK);
54
+ apiCalls += 1;
55
+ if (apiCalls === 1) {
56
+ return jsonResponse(
57
+ { error: "invalid_token", description: "expired" },
58
+ 401,
59
+ );
60
+ }
61
+ return jsonResponse({ ticket: { id: 7942 } });
62
+ });
63
+
64
+ const resp = await zdGet<{ ticket: { id: number } }>("tickets/7942.json");
65
+
66
+ expect(resp.ticket.id).toBe(7942);
67
+ // 2 token requests (initial + refresh) + 2 API calls (401 + retry).
68
+ expect(mock).toHaveBeenCalledTimes(4);
69
+ expect(apiCalls).toBe(2);
70
+ });
71
+
72
+ it("fails after invalid_token persists across the retry", async () => {
73
+ fetchMock((url) =>
74
+ isTokenRequest(url)
75
+ ? jsonResponse(TOKEN_OK)
76
+ : jsonResponse({ error: "invalid_token" }, 401),
77
+ );
78
+
79
+ await expect(zdGet("tickets/7942.json")).rejects.toThrow(
80
+ /unauthorized even after refreshing/,
81
+ );
82
+ });
83
+
84
+ it("fails fast on a non-invalid_token 401 without retrying", async () => {
85
+ const mock = fetchMock((url) =>
86
+ isTokenRequest(url)
87
+ ? jsonResponse(TOKEN_OK)
88
+ : jsonResponse({ error: "invalid_client" }, 401),
89
+ );
90
+
91
+ await expect(zdGet("tickets/7942.json")).rejects.toThrow(
92
+ /unauthorized \(HTTP 401\)\. Check the OAuth/,
93
+ );
94
+ // No retry: one token request, one API call.
95
+ expect(mock).toHaveBeenCalledTimes(2);
96
+ });
97
+
98
+ it("surfaces 429 with the Retry-After hint", async () => {
99
+ route(() => jsonResponse({}, 429, { "Retry-After": "7" }));
100
+
101
+ const err = await zdGet("tickets.json").catch((e) => e);
102
+ expect(err).toBeInstanceOf(ZendeskApiError);
103
+ expect((err as ZendeskApiError).isRateLimited).toBe(true);
104
+ expect((err as ZendeskApiError).retryAfter).toBe(7);
105
+ expect((err as ZendeskApiError).message).toContain("Retry in ~7s");
106
+ });
107
+
108
+ it("maps 403 to the scope-ceiling hint", async () => {
109
+ route(() => jsonResponse({ error: "Forbidden" }, 403));
110
+
111
+ const err = await zdGet("tickets.json").catch((e) => e);
112
+ expect((err as ZendeskApiError).status).toBe(403);
113
+ expect((err as ZendeskApiError).message).toMatch(/Allowed scopes/);
114
+ });
115
+
116
+ it("maps 404 to not-found with Zendesk's description", async () => {
117
+ route(() =>
118
+ jsonResponse(
119
+ { error: "RecordNotFound", description: "Ticket 1 not found" },
120
+ 404,
121
+ ),
122
+ );
123
+
124
+ const err = await zdGet("tickets/1.json").catch((e) => e);
125
+ expect((err as ZendeskApiError).status).toBe(404);
126
+ expect((err as ZendeskApiError).message).toContain("Ticket 1 not found");
127
+ });
128
+
129
+ it("maps 5xx to a retryable server error", async () => {
130
+ route(() => new Response("oops", { status: 502 }));
131
+
132
+ const err = await zdGet("tickets.json").catch((e) => e);
133
+ expect((err as ZendeskApiError).status).toBe(502);
134
+ expect((err as ZendeskApiError).message).toMatch(/server error/);
135
+ });
136
+
137
+ it("rejects non-JSON success bodies", async () => {
138
+ route(() => new Response("<html>hi</html>", { status: 200 }));
139
+
140
+ const err = await zdGet("tickets.json").catch((e) => e);
141
+ expect((err as ZendeskApiError).message).toMatch(/non-JSON/);
142
+ });
143
+ });
@@ -0,0 +1,124 @@
1
+ import { beforeEach, describe, expect, it, vi } from "vitest";
2
+ import {
3
+ ZendeskAuthError,
4
+ _resetAuthCache,
5
+ getZendeskAccessToken,
6
+ getZendeskCredentials,
7
+ requestZendeskAccessToken,
8
+ } from "../lib/auth";
9
+ import {
10
+ TOKEN_OK,
11
+ fetchMock,
12
+ isTokenRequest,
13
+ jsonResponse,
14
+ stubZendeskEnv,
15
+ } from "./_helpers";
16
+
17
+ beforeEach(() => {
18
+ vi.unstubAllEnvs();
19
+ _resetAuthCache();
20
+ stubZendeskEnv();
21
+ });
22
+
23
+ describe("getZendeskCredentials", () => {
24
+ it("returns the three credentials when set", () => {
25
+ const creds = getZendeskCredentials();
26
+ expect(creds).toEqual({
27
+ subdomain: "{subdomain}",
28
+ clientId: "test-client-id",
29
+ clientSecret: "test-client-secret",
30
+ });
31
+ });
32
+
33
+ it("throws a rich error listing every missing var", () => {
34
+ vi.stubEnv("ZENDESK_SUBDOMAIN", "");
35
+ vi.stubEnv("ZENDESK_CLIENT_SECRET", "");
36
+ expect(() => getZendeskCredentials()).toThrow(ZendeskAuthError);
37
+ expect(() => getZendeskCredentials()).toThrow(
38
+ /ZENDESK_SUBDOMAIN, ZENDESK_CLIENT_SECRET/,
39
+ );
40
+ });
41
+
42
+ it("the missing-env error mentions the deprecation deadline", () => {
43
+ vi.stubEnv("ZENDESK_CLIENT_ID", "");
44
+ expect(() => getZendeskCredentials()).toThrow(/2027-04-30/);
45
+ });
46
+ });
47
+
48
+ describe("requestZendeskAccessToken", () => {
49
+ it("POSTs client_credentials to the tenant token endpoint", async () => {
50
+ const mock = fetchMock((url) =>
51
+ isTokenRequest(url) ? jsonResponse(TOKEN_OK) : jsonResponse({}, 404),
52
+ );
53
+
54
+ const token = await requestZendeskAccessToken();
55
+
56
+ expect(token).toBe("test-access-token");
57
+ expect(mock).toHaveBeenCalledTimes(1);
58
+ const [url, init] = mock.mock.calls[0] as [string, RequestInit];
59
+ expect(url).toBe("https://{subdomain}.zendesk.com/oauth/tokens");
60
+ expect(init.method).toBe("POST");
61
+ expect(init.headers).toMatchObject({
62
+ "Content-Type": "application/x-www-form-urlencoded",
63
+ });
64
+ const body = String(init.body);
65
+ expect(body).toContain("grant_type=client_credentials");
66
+ expect(body).toContain("client_id=test-client-id");
67
+ expect(body).toContain("client_secret=test-client-secret");
68
+ expect(body).toContain("scope=tickets%3Aread+users%3Aread+organizations%3Aread");
69
+ });
70
+
71
+ it("surfaces Zendesk's OAuth error code (invalid_scope)", async () => {
72
+ fetchMock(() =>
73
+ jsonResponse(
74
+ {
75
+ error: "invalid_scope",
76
+ error_description: "The requested scope is invalid.",
77
+ },
78
+ 400,
79
+ ),
80
+ );
81
+
82
+ await expect(requestZendeskAccessToken()).rejects.toThrow(
83
+ /Allowed scopes.*invalid_scope/s,
84
+ );
85
+ });
86
+
87
+ it("surfaces a non-JSON failure with the HTTP status", async () => {
88
+ fetchMock(() => new Response("gateway timeout", { status: 504 }));
89
+ await expect(requestZendeskAccessToken()).rejects.toThrow(/HTTP 504/);
90
+ });
91
+
92
+ it("rejects a 200 body without access_token", async () => {
93
+ fetchMock(() => jsonResponse({ token_type: "bearer" }));
94
+ await expect(requestZendeskAccessToken()).rejects.toThrow(
95
+ /token request failed/,
96
+ );
97
+ });
98
+ });
99
+
100
+ describe("getZendeskAccessToken cache", () => {
101
+ it("requests once and serves the second call from cache", async () => {
102
+ const mock = fetchMock((url) =>
103
+ isTokenRequest(url) ? jsonResponse(TOKEN_OK) : jsonResponse({}, 404),
104
+ );
105
+
106
+ await getZendeskAccessToken();
107
+ await getZendeskAccessToken();
108
+
109
+ expect(mock).toHaveBeenCalledTimes(1);
110
+ });
111
+
112
+ it("re-requests after resetTokenCache", async () => {
113
+ const mock = fetchMock((url) =>
114
+ isTokenRequest(url) ? jsonResponse(TOKEN_OK) : jsonResponse({}, 404),
115
+ );
116
+
117
+ await getZendeskAccessToken();
118
+ const { resetTokenCache } = await import("../lib/auth");
119
+ resetTokenCache();
120
+ await getZendeskAccessToken();
121
+
122
+ expect(mock).toHaveBeenCalledTimes(2);
123
+ });
124
+ });
@@ -0,0 +1,135 @@
1
+ import { readFile, rm } from "node:fs/promises";
2
+ import { tmpdir } from "node:os";
3
+ import path from "node:path";
4
+ import { beforeEach, describe, expect, it, vi } from "vitest";
5
+ import { _resetAuthCache } from "../lib/auth";
6
+ import { downloadAttachmentTool } from "../lib/tools/download-attachment";
7
+ import {
8
+ TOKEN_OK,
9
+ fetchMock,
10
+ firstText,
11
+ invoke,
12
+ isTokenRequest,
13
+ jsonResponse,
14
+ stubZendeskEnv,
15
+ } from "./_helpers";
16
+
17
+ beforeEach(() => {
18
+ vi.unstubAllEnvs();
19
+ vi.unstubAllGlobals();
20
+ _resetAuthCache();
21
+ stubZendeskEnv();
22
+ });
23
+
24
+ const COMMENTS = {
25
+ comments: [
26
+ {
27
+ id: 1,
28
+ body: "<p>see screenshot</p>",
29
+ public: true,
30
+ author_id: 1042,
31
+ created_at: "2026-08-27T14:45:00Z",
32
+ attachments: [
33
+ {
34
+ id: 43038971788439,
35
+ file_name: "Screenshot 2026-08-27.png",
36
+ content_url: "https://storage.example/signed/abc",
37
+ content_type: "image/png",
38
+ size: 8,
39
+ },
40
+ ],
41
+ },
42
+ ],
43
+ };
44
+
45
+ const PNG_BYTES = new Uint8Array([0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a]);
46
+
47
+ function routeWithStorage(body: Uint8Array) {
48
+ return fetchMock((url, init) => {
49
+ if (isTokenRequest(url)) return jsonResponse(TOKEN_OK);
50
+ if (url.includes("/api/v2/tickets/7942/comments.json")) {
51
+ return jsonResponse(COMMENTS);
52
+ }
53
+ if (url.startsWith("https://storage.example/")) {
54
+ // The signed-URL download must NOT carry the bearer token (it breaks
55
+ // the storage signature).
56
+ const hasAuth = Boolean(
57
+ (init?.headers as Record<string, string> | undefined)?.Authorization,
58
+ );
59
+ if (hasAuth) return new Response("forbidden", { status: 403 });
60
+ return new Response(body as unknown as BodyInit, {
61
+ status: 200,
62
+ headers: { "Content-Type": "image/png" },
63
+ });
64
+ }
65
+ return jsonResponse({}, 404);
66
+ });
67
+ }
68
+
69
+ describe("zendesk_download_attachment", () => {
70
+ it("downloads by attachment id and writes to the temp dir", async () => {
71
+ routeWithStorage(PNG_BYTES);
72
+
73
+ const result = await invoke(downloadAttachmentTool, {
74
+ ticket: "7942",
75
+ attachment: 43038971788439,
76
+ });
77
+ const text = firstText(result);
78
+
79
+ expect(text).toContain("Downloaded: ");
80
+ expect(text).toContain("image/png");
81
+ expect(text).toContain("from comment #1");
82
+ expect(text).toContain("`read` tool");
83
+
84
+ const filePath = text.split("Downloaded: ")[1]!.split("\n")[0]!;
85
+ expect(filePath.startsWith(path.join(tmpdir(), "pi-zendesk-me"))).toBe(
86
+ true,
87
+ );
88
+ expect(path.basename(filePath)).toContain("Screenshot_2026-08-27.png");
89
+ const written = await readFile(filePath);
90
+ expect([...written]).toEqual([...PNG_BYTES]);
91
+
92
+ await rm(path.join(tmpdir(), "pi-zendesk-me"), {
93
+ recursive: true,
94
+ force: true,
95
+ });
96
+ });
97
+
98
+ it("rejects an unparseable ticket reference", async () => {
99
+ const result = await invoke(downloadAttachmentTool, {
100
+ ticket: "junk",
101
+ attachment: 1,
102
+ });
103
+ expect(firstText(result)).toContain("Invalid ticket reference");
104
+ });
105
+
106
+ it("lists available attachment ids when the id is not found", async () => {
107
+ routeWithStorage(PNG_BYTES);
108
+
109
+ const result = await invoke(downloadAttachmentTool, {
110
+ ticket: "7942",
111
+ attachment: 42,
112
+ });
113
+ const text = firstText(result);
114
+
115
+ expect(text).toContain("Attachment 42 not found on ticket 7942");
116
+ expect(text).toContain("#43038971788439 Screenshot 2026-08-27.png");
117
+ });
118
+
119
+ it("surfaces storage download failures through the error formatter", async () => {
120
+ fetchMock((url) => {
121
+ if (isTokenRequest(url)) return jsonResponse(TOKEN_OK);
122
+ if (url.includes("/api/v2/tickets/7942/comments.json")) {
123
+ return jsonResponse(COMMENTS);
124
+ }
125
+ return new Response("gone", { status: 410 });
126
+ });
127
+
128
+ const result = await invoke(downloadAttachmentTool, {
129
+ ticket: "https://{subdomain}.zendesk.com/agent/tickets/7942",
130
+ attachment: 43038971788439,
131
+ });
132
+ expect(firstText(result)).toContain("Zendesk error:");
133
+ expect(firstText(result)).toContain("HTTP 410");
134
+ });
135
+ });
@@ -0,0 +1,122 @@
1
+ import { beforeEach, describe, expect, it, vi } from "vitest";
2
+ import { _resetAuthCache } from "../lib/auth";
3
+ import { getCommentsTool } from "../lib/tools/get-comments";
4
+ import {
5
+ TOKEN_OK,
6
+ fetchMock,
7
+ firstText,
8
+ invoke,
9
+ isTokenRequest,
10
+ jsonResponse,
11
+ stubZendeskEnv,
12
+ } from "./_helpers";
13
+
14
+ beforeEach(() => {
15
+ vi.unstubAllEnvs();
16
+ _resetAuthCache();
17
+ stubZendeskEnv();
18
+ });
19
+
20
+ function route(handler: (url: string) => Response | Promise<Response>) {
21
+ return fetchMock(async (url) => {
22
+ if (isTokenRequest(url)) return jsonResponse(TOKEN_OK);
23
+ return handler(url);
24
+ });
25
+ }
26
+
27
+ const COMMENTS_RESPONSE = {
28
+ comments: [
29
+ {
30
+ id: 1,
31
+ body: "<p>I cannot log in at all.</p>",
32
+ public: true,
33
+ author_id: 1042,
34
+ created_at: "2026-08-14T09:30:00Z",
35
+ },
36
+ {
37
+ id: 2,
38
+ body: "<p>Internal: user is on an old plan, check entitlements.</p>",
39
+ public: false,
40
+ author_id: 2099,
41
+ created_at: "2026-08-14T10:00:00Z",
42
+ },
43
+ {
44
+ id: 3,
45
+ body: "<p>We pushed a fix, please retry.</p>",
46
+ public: true,
47
+ author_id: 2099,
48
+ created_at: "2026-08-15T11:00:00Z",
49
+ attachments: [
50
+ { id: 900, file_name: "fix-notes.pdf", size: 2048 },
51
+ ],
52
+ },
53
+ ],
54
+ users: [
55
+ { id: 1042, name: "Ada Lovelace", active: true, role: "end-user" },
56
+ { id: 2099, name: "Grace Agent", active: true, role: "agent" },
57
+ ],
58
+ };
59
+
60
+ describe("zendesk_get_comments", () => {
61
+ it("renders the thread oldest-first with visibility flags", async () => {
62
+ route((url) =>
63
+ url.includes("/api/v2/tickets/7942/comments.json")
64
+ ? jsonResponse(COMMENTS_RESPONSE)
65
+ : jsonResponse({}, 404),
66
+ );
67
+
68
+ const result = await invoke(getCommentsTool, { ticket: "7942" });
69
+ const text = firstText(result);
70
+
71
+ expect(text).toContain("Ticket #7942: 3 entries");
72
+ expect(text).toContain("Ada Lovelace - public reply");
73
+ expect(text).toContain("Grace Agent - INTERNAL NOTE");
74
+ expect(text).toContain("I cannot log in at all.");
75
+ expect(text).toContain("attachment: #900 fix-notes.pdf (2048 bytes)");
76
+ expect(text).not.toContain("<p>");
77
+ // Newest last: the fix reply appears after the internal note.
78
+ expect(text.indexOf("Internal: user is on an old plan")).toBeLessThan(
79
+ text.indexOf("We pushed a fix"),
80
+ );
81
+ });
82
+
83
+ it("limits rendering to the N most recent and counts the hidden tail", async () => {
84
+ route((url) =>
85
+ url.includes("/api/v2/tickets/7942/comments.json")
86
+ ? jsonResponse(COMMENTS_RESPONSE)
87
+ : jsonResponse({}, 404),
88
+ );
89
+
90
+ const result = await invoke(getCommentsTool, {
91
+ ticket: "7942",
92
+ limit: 1,
93
+ });
94
+ const text = firstText(result);
95
+
96
+ expect(text).toContain("showing 1 most recent, 2 oldest hidden");
97
+ expect(text).toContain("We pushed a fix");
98
+ expect(text).not.toContain("I cannot log in at all.");
99
+ });
100
+
101
+ it("public_only drops internal notes", async () => {
102
+ route((url) =>
103
+ url.includes("/api/v2/tickets/7942/comments.json")
104
+ ? jsonResponse(COMMENTS_RESPONSE)
105
+ : jsonResponse({}, 404),
106
+ );
107
+
108
+ const result = await invoke(getCommentsTool, {
109
+ ticket: "7942",
110
+ public_only: true,
111
+ });
112
+ const text = firstText(result);
113
+
114
+ expect(text).toContain("2 entries (public replies only)");
115
+ expect(text).not.toContain("INTERNAL NOTE");
116
+ });
117
+
118
+ it("rejects an unparseable ticket reference", async () => {
119
+ const result = await invoke(getCommentsTool, { ticket: "nope" });
120
+ expect(firstText(result)).toContain("Invalid ticket reference");
121
+ });
122
+ });
@@ -0,0 +1,120 @@
1
+ import { beforeEach, describe, expect, it, vi } from "vitest";
2
+ import { _resetAuthCache } from "../lib/auth";
3
+ import { getTicketTool } from "../lib/tools/get-ticket";
4
+ import {
5
+ TOKEN_OK,
6
+ fetchMock,
7
+ firstText,
8
+ invoke,
9
+ isTokenRequest,
10
+ jsonResponse,
11
+ stubZendeskEnv,
12
+ } from "./_helpers";
13
+
14
+ beforeEach(() => {
15
+ vi.unstubAllEnvs();
16
+ _resetAuthCache();
17
+ stubZendeskEnv();
18
+ });
19
+
20
+ function route(handler: (url: string) => Response | Promise<Response>) {
21
+ return fetchMock(async (url) => {
22
+ if (isTokenRequest(url)) return jsonResponse(TOKEN_OK);
23
+ return handler(url);
24
+ });
25
+ }
26
+
27
+ const TICKET_RESPONSE = {
28
+ ticket: {
29
+ id: 7942,
30
+ subject: "Cannot login to portal",
31
+ description: "<p>Password reset emails <b>never arrive</b>.</p>",
32
+ status: "open",
33
+ priority: "high",
34
+ type: "incident",
35
+ requester_id: 1042,
36
+ submitter_id: 1042,
37
+ assignee_id: 2099,
38
+ organization_id: 77,
39
+ group_id: 1,
40
+ tags: ["login", "email"],
41
+ created_at: "2026-08-14T09:30:00Z",
42
+ updated_at: "2026-08-15T11:00:00Z",
43
+ },
44
+ users: [
45
+ {
46
+ id: 1042,
47
+ name: "Ada Lovelace",
48
+ email: "ada@example.com",
49
+ active: true,
50
+ role: "end-user",
51
+ },
52
+ {
53
+ id: 2099,
54
+ name: "Grace Agent",
55
+ active: true,
56
+ role: "agent",
57
+ },
58
+ ],
59
+ organizations: [
60
+ { id: 77, name: "Analytical Engines Ltd", active: true },
61
+ ],
62
+ };
63
+
64
+ describe("zendesk_get_ticket", () => {
65
+ it("rejects an unparseable ticket reference", async () => {
66
+ const result = await invoke(getTicketTool, { ticket: "banana" });
67
+ expect(firstText(result)).toContain("Invalid ticket reference");
68
+ });
69
+
70
+ it("fetches by bare id and renders people from side-loads", async () => {
71
+ const mock = route((url) =>
72
+ url.includes("/api/v2/tickets/7942.json")
73
+ ? jsonResponse(TICKET_RESPONSE)
74
+ : jsonResponse({}, 404),
75
+ );
76
+
77
+ const result = await invoke(getTicketTool, { ticket: "7942" });
78
+ const text = firstText(result);
79
+
80
+ expect(text).toContain("Ticket #7942 - [open] Cannot login to portal");
81
+ expect(text).toContain("Requester: Ada Lovelace <ada@example.com>");
82
+ expect(text).toContain("Assignee: Grace Agent");
83
+ expect(text).toContain("Organization: Analytical Engines Ltd");
84
+ expect(text).toContain("Priority: high");
85
+ expect(text).toContain("Tags: login, email");
86
+ expect(text).toContain("Password reset emails never arrive.");
87
+ expect(text).not.toContain("<p>");
88
+
89
+ const [url] = mock.mock.calls.find(
90
+ (c) => !isTokenRequest(String(c[0])),
91
+ ) as unknown as [string];
92
+ expect(url).toContain("tickets/7942.json");
93
+ expect(url).toContain("include=");
94
+ });
95
+
96
+ it("fetches from a pasted agent URL", async () => {
97
+ route((url) =>
98
+ url.includes("/api/v2/tickets/7942.json")
99
+ ? jsonResponse(TICKET_RESPONSE)
100
+ : jsonResponse({}, 404),
101
+ );
102
+
103
+ const result = await invoke(getTicketTool, {
104
+ ticket: "https://{subdomain}.zendesk.com/agent/tickets/7942",
105
+ });
106
+ expect(firstText(result)).toContain("Ticket #7942");
107
+ });
108
+
109
+ it("maps API errors through the shared error formatter", async () => {
110
+ route(() =>
111
+ jsonResponse(
112
+ { error: "RecordNotFound", description: "Ticket 7942 not found" },
113
+ 404,
114
+ ),
115
+ );
116
+
117
+ const result = await invoke(getTicketTool, { ticket: "7942" });
118
+ expect(firstText(result)).toContain("Zendesk error:");
119
+ });
120
+ });