@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,133 @@
1
+ import { beforeEach, describe, expect, it, vi } from "vitest";
2
+ import { _resetAuthCache } from "../lib/auth";
3
+ import { getViewTool } from "../lib/tools/get-view";
4
+ import { parseViewRef } from "../lib/url";
5
+ import {
6
+ TOKEN_OK,
7
+ fetchMock,
8
+ firstText,
9
+ invoke,
10
+ isTokenRequest,
11
+ jsonResponse,
12
+ stubZendeskEnv,
13
+ } from "./_helpers";
14
+
15
+ beforeEach(() => {
16
+ vi.unstubAllEnvs();
17
+ _resetAuthCache();
18
+ stubZendeskEnv();
19
+ });
20
+
21
+ describe("parseViewRef", () => {
22
+ it("accepts bare ids and /filters/ and /views/ URLs", () => {
23
+ expect(parseViewRef("20824934716439")).toBe(20824934716439);
24
+ expect(
25
+ parseViewRef("https://{subdomain}.zendesk.com/agent/filters/20824934716439"),
26
+ ).toBe(20824934716439);
27
+ expect(
28
+ parseViewRef("https://{subdomain}.zendesk.com/agent/views/42"),
29
+ ).toBe(42);
30
+ expect(parseViewRef("nonsense")).toBeNull();
31
+ });
32
+ });
33
+
34
+ function route(handler: (url: string) => Response | Promise<Response>) {
35
+ return fetchMock(async (url) => {
36
+ if (isTokenRequest(url)) return jsonResponse(TOKEN_OK);
37
+ return handler(url);
38
+ });
39
+ }
40
+
41
+ const VIEW = { view: { id: 20824934716439, title: "Mine - Web Team", active: true } };
42
+ const EXEC = {
43
+ count: 2,
44
+ next_page: null,
45
+ rows: [
46
+ {
47
+ ticket: {
48
+ id: 7942,
49
+ subject: "Manage Member button has disappeared in the dashboard",
50
+ status: "open",
51
+ priority: "urgent",
52
+ requester_id: 1042,
53
+ assignee_id: 2099,
54
+ created_at: "2026-08-14T09:30:00Z",
55
+ },
56
+ },
57
+ {
58
+ ticket: {
59
+ id: 7950,
60
+ subject: "Email digest stopped",
61
+ status: "pending",
62
+ priority: "normal",
63
+ requester_id: 2001,
64
+ assignee_id: 2099,
65
+ created_at: "2026-08-16T10:00:00Z",
66
+ },
67
+ },
68
+ ],
69
+ users: [
70
+ { id: 1042, name: "Ada Lovelace", active: true, role: "end-user" },
71
+ { id: 2099, name: "Grace Agent", active: true, role: "agent" },
72
+ ],
73
+ organizations: [{ id: 77, name: "Analytical Engines Ltd", active: true }],
74
+ };
75
+
76
+ describe("zendesk_get_view", () => {
77
+ it("rejects an unparseable view reference", async () => {
78
+ const result = await invoke(getViewTool, { view: "nope" });
79
+ expect(firstText(result)).toContain("Invalid view reference");
80
+ });
81
+
82
+ it("renders title and tickets with resolved people from a filter URL", async () => {
83
+ const mock = route((url) => {
84
+ if (url.includes("/api/v2/views/20824934716439/execute.json")) {
85
+ return jsonResponse(EXEC);
86
+ }
87
+ if (url.includes("/api/v2/views/20824934716439.json")) {
88
+ return jsonResponse(VIEW);
89
+ }
90
+ return jsonResponse({}, 404);
91
+ });
92
+
93
+ const result = await invoke(getViewTool, {
94
+ view: "https://{subdomain}.zendesk.com/agent/filters/20824934716439",
95
+ });
96
+ const text = firstText(result);
97
+
98
+ expect(text).toContain("View #20824934716439 - Mine - Web Team");
99
+ expect(text).toContain("2 matching tickets");
100
+ expect(text).toContain(
101
+ "#7942 [open] Manage Member button has disappeared in the dashboard (urgent)",
102
+ );
103
+ expect(text).toContain("#7950 [pending] Email digest stopped");
104
+
105
+ const [execUrl] = mock.mock.calls
106
+ .map((c) => String(c[0]))
107
+ .filter((u) => u.includes("execute.json"));
108
+ expect(execUrl).toContain("per_page=25");
109
+ expect(execUrl).toContain("include=");
110
+ });
111
+
112
+ it("respects the limit param and reports empty views", async () => {
113
+ route((url) => {
114
+ if (url.includes("execute.json")) {
115
+ return jsonResponse({ count: 0, next_page: null, rows: [] });
116
+ }
117
+ return jsonResponse(VIEW);
118
+ });
119
+
120
+ const result = await invoke(getViewTool, { view: "20824934716439", limit: 10 });
121
+ const text = firstText(result);
122
+
123
+ expect(text).toContain("0 matching tickets");
124
+ expect(text).toContain("(no matching tickets)");
125
+ });
126
+
127
+ it("maps API errors through the shared error formatter", async () => {
128
+ route(() => jsonResponse({ error: "Forbidden" }, 403));
129
+
130
+ const result = await invoke(getViewTool, { view: "20824934716439" });
131
+ expect(firstText(result)).toContain("Zendesk error:");
132
+ });
133
+ });
@@ -0,0 +1,131 @@
1
+ import { beforeEach, describe, expect, it, vi } from "vitest";
2
+ import { _resetAuthCache } from "../lib/auth";
3
+ import { effectiveQuery, searchTool } from "../lib/tools/search";
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
+ describe("effectiveQuery", () => {
21
+ it("prepends type:ticket for plain queries", () => {
22
+ expect(effectiveQuery("billing urgent", false)).toBe(
23
+ "type:ticket billing urgent",
24
+ );
25
+ });
26
+
27
+ it("leaves queries that already pin a type", () => {
28
+ expect(effectiveQuery("type:organization acme", false)).toBe(
29
+ "type:organization acme",
30
+ );
31
+ });
32
+
33
+ it("all_types disables the default", () => {
34
+ expect(effectiveQuery("billing", true)).toBe("billing");
35
+ });
36
+ });
37
+
38
+ describe("zendesk_search", () => {
39
+ it("searches tickets and renders result lines", async () => {
40
+ const mock = fetchMock((url) => {
41
+ if (isTokenRequest(url)) return jsonResponse(TOKEN_OK);
42
+ if (url.includes("/api/v2/search.json")) {
43
+ return jsonResponse({
44
+ count: 2,
45
+ next_page: null,
46
+ previous_page: null,
47
+ results: [
48
+ {
49
+ result_type: "ticket",
50
+ id: 7942,
51
+ subject: "Cannot login to portal",
52
+ status: "open",
53
+ priority: "high",
54
+ requester_id: 1042,
55
+ created_at: "2026-08-14T09:30:00Z",
56
+ },
57
+ {
58
+ result_type: "ticket",
59
+ id: 7901,
60
+ subject: "SSO loop after password change",
61
+ status: "solved",
62
+ priority: "normal",
63
+ requester_id: 2001,
64
+ created_at: "2026-08-01T08:00:00Z",
65
+ },
66
+ ],
67
+ });
68
+ }
69
+ return jsonResponse({}, 404);
70
+ });
71
+
72
+ const result = await invoke(searchTool, { query: "login" });
73
+ const text = firstText(result);
74
+
75
+ expect(text).toContain("2 results for: type:ticket login");
76
+ expect(text).toContain("#7942 [open] Cannot login to portal (high)");
77
+ expect(text).toContain("#7901 [solved] SSO loop after password change");
78
+ expect(text).toContain("#7942: created 2026-08-14 09:30 UTC");
79
+
80
+ const [url] = mock.mock.calls.find(
81
+ (c) => !isTokenRequest(String(c[0])),
82
+ ) as unknown as [string];
83
+ expect(url).toContain("query=type%3Aticket+login");
84
+ expect(url).toContain("per_page=25");
85
+ });
86
+
87
+ it("reports empty result sets plainly", async () => {
88
+ fetchMock((url) => {
89
+ if (isTokenRequest(url)) return jsonResponse(TOKEN_OK);
90
+ return jsonResponse({
91
+ count: 0,
92
+ next_page: null,
93
+ previous_page: null,
94
+ results: [],
95
+ });
96
+ });
97
+
98
+ const result = await invoke(searchTool, { query: "zzz-nothing" });
99
+ expect(firstText(result)).toContain(
100
+ "No results for: type:ticket zzz-nothing",
101
+ );
102
+ });
103
+
104
+ it("notes pagination when more results exist", async () => {
105
+ fetchMock((url) => {
106
+ if (isTokenRequest(url)) return jsonResponse(TOKEN_OK);
107
+ return jsonResponse({
108
+ count: 40,
109
+ next_page:
110
+ "https://{subdomain}.zendesk.com/api/v2/search.json?page=2",
111
+ previous_page: null,
112
+ results: [
113
+ {
114
+ result_type: "ticket",
115
+ id: 1,
116
+ subject: "a",
117
+ status: "open",
118
+ priority: "low",
119
+ requester_id: 5,
120
+ created_at: "2026-08-14T09:30:00Z",
121
+ },
122
+ ],
123
+ });
124
+ });
125
+
126
+ const result = await invoke(searchTool, { query: "open" });
127
+ expect(firstText(result)).toContain(
128
+ "showing first 1 - narrow the query to refine",
129
+ );
130
+ });
131
+ });
@@ -0,0 +1,30 @@
1
+ import { describe, expect, it } from "vitest";
2
+ import { parseTicketRef } from "../lib/url";
3
+
4
+ describe("parseTicketRef", () => {
5
+ it("accepts a bare numeric id", () => {
6
+ expect(parseTicketRef("7942")).toBe(7942);
7
+ });
8
+
9
+ it("accepts a full agent URL", () => {
10
+ expect(parseTicketRef("https://{subdomain}.zendesk.com/agent/tickets/7942")).toBe(
11
+ 7942,
12
+ );
13
+ });
14
+
15
+ it("accepts an API URL containing /tickets/N", () => {
16
+ expect(
17
+ parseTicketRef("https://{subdomain}.zendesk.com/api/v2/tickets/12.json"),
18
+ ).toBe(12);
19
+ });
20
+
21
+ it("trims whitespace", () => {
22
+ expect(parseTicketRef(" 7942 ")).toBe(7942);
23
+ });
24
+
25
+ it("returns null for non-ticket input", () => {
26
+ expect(parseTicketRef("abc")).toBeNull();
27
+ expect(parseTicketRef("")).toBeNull();
28
+ expect(parseTicketRef("https://{subdomain}.zendesk.com/agent/")).toBeNull();
29
+ });
30
+ });
@@ -0,0 +1,12 @@
1
+ import { defineConfig } from "vitest/config";
2
+
3
+ export default defineConfig({
4
+ test: {
5
+ include: ["tests/**/*.test.ts"],
6
+ environment: "node",
7
+ // Each test gets a fresh module cache so the auth token cache does not
8
+ // leak across files; pairs with the explicit env-var management in
9
+ // tests/auth.test.ts.
10
+ isolate: true,
11
+ },
12
+ });