-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproject_repository.test.ts
More file actions
909 lines (819 loc) · 32.2 KB
/
project_repository.test.ts
File metadata and controls
909 lines (819 loc) · 32.2 KB
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
/**
* Unit tests for ProjectRepository: isActorAllowedToModifyFiles, getProjectDetail, getUserFromToken,
* getTokenUserDetails, isContentLinked, linkContentId, getRandomMembers, getAllMembers,
* createRelease, createTag, updateTag, updateRelease.
*/
import { ProjectRepository } from "../project_repository";
import { ProjectDetail } from "../../model/project_detail";
jest.mock("../../../utils/logger", () => ({
logDebugInfo: jest.fn(),
logError: jest.fn(),
logInfo: jest.fn(),
}));
const mockGetByUsername = jest.fn();
const mockCheckMembershipForUser = jest.fn();
const mockGetAuthenticated = jest.fn();
const mockGraphql = jest.fn();
const mockTeamsList = jest.fn();
const mockListMembersInOrg = jest.fn();
const mockGetRef = jest.fn();
const mockCreateRef = jest.fn();
const mockUpdateRef = jest.fn();
const mockGetReleaseByTag = jest.fn();
const mockListReleases = jest.fn();
const mockUpdateRelease = jest.fn();
const mockCreateRelease = jest.fn();
const mockContext = { repo: { owner: "test-owner" } };
jest.mock("@actions/github", () => ({
getOctokit: () => ({
rest: {
users: {
getByUsername: (...args: unknown[]) => mockGetByUsername(...args),
getAuthenticated: (...args: unknown[]) => mockGetAuthenticated(...args),
},
orgs: {
checkMembershipForUser: (...args: unknown[]) =>
mockCheckMembershipForUser(...args),
},
teams: {
list: (...args: unknown[]) => mockTeamsList(...args),
listMembersInOrg: (...args: unknown[]) => mockListMembersInOrg(...args),
},
git: {
getRef: (...args: unknown[]) => mockGetRef(...args),
createRef: (...args: unknown[]) => mockCreateRef(...args),
updateRef: (...args: unknown[]) => mockUpdateRef(...args),
},
repos: {
getReleaseByTag: (...args: unknown[]) => mockGetReleaseByTag(...args),
listReleases: (...args: unknown[]) => mockListReleases(...args),
updateRelease: (...args: unknown[]) => mockUpdateRelease(...args),
createRelease: (...args: unknown[]) => mockCreateRelease(...args),
},
},
graphql: (...args: unknown[]) => mockGraphql(...args),
}),
get context() {
return mockContext;
},
}));
describe("ProjectRepository.isActorAllowedToModifyFiles", () => {
const repo = new ProjectRepository();
beforeEach(() => {
mockGetByUsername.mockReset();
mockCheckMembershipForUser.mockReset();
});
it("returns true when owner is User and actor equals owner", async () => {
mockGetByUsername.mockResolvedValue({
data: { type: "User", login: "alice" },
});
const result = await repo.isActorAllowedToModifyFiles("alice", "alice", "token");
expect(result).toBe(true);
expect(mockCheckMembershipForUser).not.toHaveBeenCalled();
});
it("returns false when owner is User and actor differs", async () => {
mockGetByUsername.mockResolvedValue({
data: { type: "User", login: "alice" },
});
const result = await repo.isActorAllowedToModifyFiles("alice", "bob", "token");
expect(result).toBe(false);
expect(mockCheckMembershipForUser).not.toHaveBeenCalled();
});
it("returns true when owner is Organization and actor is member", async () => {
mockGetByUsername.mockResolvedValue({
data: { type: "Organization", login: "my-org" },
});
mockCheckMembershipForUser.mockResolvedValue({ status: 204 });
const result = await repo.isActorAllowedToModifyFiles("my-org", "bob", "token");
expect(result).toBe(true);
expect(mockCheckMembershipForUser).toHaveBeenCalledWith({
org: "my-org",
username: "bob",
});
});
it("returns false when owner is Organization and actor is not member (404)", async () => {
mockGetByUsername.mockResolvedValue({
data: { type: "Organization", login: "my-org" },
});
mockCheckMembershipForUser.mockRejectedValue({ status: 404 });
const result = await repo.isActorAllowedToModifyFiles("my-org", "outsider", "token");
expect(result).toBe(false);
});
it("returns false when getByUsername throws", async () => {
mockGetByUsername.mockRejectedValue(new Error("Network error"));
const result = await repo.isActorAllowedToModifyFiles("org", "actor", "token");
expect(result).toBe(false);
});
it("returns false when Organization and checkMembershipForUser throws non-404", async () => {
mockGetByUsername.mockResolvedValue({
data: { type: "Organization", login: "my-org" },
});
mockCheckMembershipForUser.mockRejectedValue(new Error("Forbidden"));
const result = await repo.isActorAllowedToModifyFiles("my-org", "user", "token");
expect(result).toBe(false);
});
});
describe("ProjectRepository.getProjectDetail", () => {
const repo = new ProjectRepository();
beforeEach(() => {
mockGetByUsername.mockReset();
mockGraphql.mockReset();
mockContext.repo = { owner: "test-owner" };
});
it("throws when projectId is not a number", async () => {
await expect(repo.getProjectDetail("abc", "token")).rejects.toThrow("Invalid project ID");
expect(mockGraphql).not.toHaveBeenCalled();
});
it("returns ProjectDetail when owner is User", async () => {
mockGetByUsername.mockResolvedValue({ data: { type: "User", login: "test-owner" } });
mockGraphql.mockResolvedValue({
user: {
projectV2: {
id: "PVT_1",
title: "My Project",
url: "https://github.com/users/test-owner/projects/1",
},
},
});
const result = await repo.getProjectDetail("1", "token");
expect(result.id).toBe("PVT_1");
expect(result.title).toBe("My Project");
expect(result.type).toBe("user");
});
it("returns ProjectDetail when owner is Organization", async () => {
mockGetByUsername.mockResolvedValue({ data: { type: "Organization", login: "test-owner" } });
mockGraphql.mockResolvedValue({
organization: {
projectV2: {
id: "PVT_2",
title: "Org Project",
url: "https://github.com/orgs/test-owner/projects/2",
},
},
});
const result = await repo.getProjectDetail("2", "token");
expect(result.id).toBe("PVT_2");
expect(result.title).toBe("Org Project");
expect(result.type).toBe("organization");
});
it("throws when project not found", async () => {
mockGetByUsername.mockResolvedValue({ data: { type: "User", login: "test-owner" } });
mockGraphql.mockResolvedValue({ user: { projectV2: null } });
await expect(repo.getProjectDetail("99", "token")).rejects.toThrow("Project not found");
});
it("throws when getByUsername fails", async () => {
mockGetByUsername.mockRejectedValue(new Error("Not found"));
await expect(repo.getProjectDetail("1", "token")).rejects.toThrow("Failed to get owner");
});
it("throws when graphql project query fails", async () => {
mockGetByUsername.mockResolvedValue({ data: { type: "User", login: "test-owner" } });
mockGraphql.mockRejectedValue(new Error("Network error"));
await expect(repo.getProjectDetail("1", "token")).rejects.toThrow("Failed to fetch project data");
});
});
describe("ProjectRepository.getUserFromToken", () => {
const repo = new ProjectRepository();
beforeEach(() => {
mockGetAuthenticated.mockReset();
});
it("returns user login", async () => {
mockGetAuthenticated.mockResolvedValue({ data: { login: "octocat" } });
const result = await repo.getUserFromToken("token");
expect(result).toBe("octocat");
});
});
describe("ProjectRepository.getTokenUserDetails", () => {
const repo = new ProjectRepository();
beforeEach(() => {
mockGetAuthenticated.mockReset();
});
it("returns name and email from user", async () => {
mockGetAuthenticated.mockResolvedValue({
data: { login: "octocat", name: "Octo Cat", email: "octo@example.com" },
});
const result = await repo.getTokenUserDetails("token");
expect(result).toEqual({ name: "Octo Cat", email: "octo@example.com" });
});
it("uses login and noreply email when name/email missing", async () => {
mockGetAuthenticated.mockResolvedValue({
data: { login: "bot", name: null, email: null },
});
const result = await repo.getTokenUserDetails("token");
expect(result.name).toBe("bot");
expect(result.email).toBe("bot@users.noreply.github.com");
});
});
describe("ProjectRepository.isContentLinked", () => {
const repo = new ProjectRepository();
const project = new ProjectDetail({
id: "PVT_1",
title: "P",
url: "https://example.com",
type: "orgs",
owner: "org",
number: 1,
});
beforeEach(() => {
mockGraphql.mockReset();
});
it("returns true when content is in project items", async () => {
mockGraphql.mockResolvedValue({
node: {
items: {
nodes: [{ content: { id: "content-123" } }, { content: { id: "other" } }],
pageInfo: { hasNextPage: false, endCursor: null },
},
},
});
const result = await repo.isContentLinked(project, "content-123", "token");
expect(result).toBe(true);
});
it("returns false when content is not in project items", async () => {
mockGraphql.mockResolvedValue({
node: {
items: {
nodes: [{ content: { id: "other" } }],
pageInfo: { hasNextPage: false, endCursor: null },
},
},
});
const result = await repo.isContentLinked(project, "content-123", "token");
expect(result).toBe(false);
});
});
describe("ProjectRepository.linkContentId", () => {
const repo = new ProjectRepository();
const project = new ProjectDetail({
id: "PVT_1",
title: "P",
url: "https://example.com",
type: "orgs",
owner: "org",
number: 1,
});
beforeEach(() => {
mockGraphql.mockReset();
});
it("returns false when content already linked", async () => {
mockGraphql
.mockResolvedValueOnce({
node: {
items: {
nodes: [{ content: { id: "content-123" } }],
pageInfo: { hasNextPage: false, endCursor: null },
},
},
})
.mockResolvedValueOnce({ addProjectV2ItemById: { item: { id: "item-1" } } });
const result = await repo.linkContentId(project, "content-123", "token");
expect(result).toBe(false);
});
it("returns true and links when content not linked", async () => {
mockGraphql
.mockResolvedValueOnce({
node: {
items: { nodes: [], pageInfo: { hasNextPage: false, endCursor: null } },
},
})
.mockResolvedValueOnce({ addProjectV2ItemById: { item: { id: "item-1" } } });
const result = await repo.linkContentId(project, "content-123", "token");
expect(result).toBe(true);
});
});
describe("ProjectRepository.getRandomMembers", () => {
const repo = new ProjectRepository();
beforeEach(() => {
mockTeamsList.mockReset();
mockListMembersInOrg.mockReset();
});
it("returns empty array when membersToAdd is 0", async () => {
const result = await repo.getRandomMembers("org", 0, [], "token");
expect(result).toEqual([]);
expect(mockTeamsList).not.toHaveBeenCalled();
});
it("returns empty array when org has no teams", async () => {
mockTeamsList.mockResolvedValue({ data: [] });
const result = await repo.getRandomMembers("org", 2, [], "token");
expect(result).toEqual([]);
});
it("returns empty array when no available members (all in currentMembers)", async () => {
mockTeamsList.mockResolvedValue({ data: [{ slug: "team-a" }] });
mockListMembersInOrg.mockResolvedValue({
data: [{ login: "alice" }, { login: "bob" }],
});
const result = await repo.getRandomMembers("org", 2, ["alice", "bob"], "token");
expect(result).toEqual([]);
});
it("returns all available when membersToAdd >= availableMembers.length", async () => {
mockTeamsList.mockResolvedValue({ data: [{ slug: "team-a" }] });
mockListMembersInOrg.mockResolvedValue({
data: [{ login: "alice" }, { login: "bob" }],
});
const result = await repo.getRandomMembers("org", 5, [], "token");
expect(result).toHaveLength(2);
expect(result).toContain("alice");
expect(result).toContain("bob");
});
it("returns members not in currentMembers", async () => {
mockTeamsList.mockResolvedValue({ data: [{ slug: "team-a" }] });
mockListMembersInOrg.mockResolvedValue({
data: [{ login: "alice" }, { login: "bob" }],
});
const result = await repo.getRandomMembers("org", 1, ["alice"], "token");
expect(result).toHaveLength(1);
expect(result[0]).toBe("bob");
});
it("returns empty array when teams.list throws", async () => {
mockTeamsList.mockRejectedValue(new Error("API error"));
const result = await repo.getRandomMembers("org", 2, [], "token");
expect(result).toEqual([]);
});
});
describe("ProjectRepository.getAllMembers", () => {
const repo = new ProjectRepository();
beforeEach(() => {
mockTeamsList.mockReset();
mockListMembersInOrg.mockReset();
});
it("returns empty array when org has no teams", async () => {
mockTeamsList.mockResolvedValue({ data: [] });
const result = await repo.getAllMembers("org", "token");
expect(result).toEqual([]);
});
it("returns unique member logins from all teams", async () => {
mockTeamsList.mockResolvedValue({ data: [{ slug: "t1" }, { slug: "t2" }] });
mockListMembersInOrg
.mockResolvedValueOnce({ data: [{ login: "a" }, { login: "b" }] })
.mockResolvedValueOnce({ data: [{ login: "b" }, { login: "c" }] });
const result = await repo.getAllMembers("org", "token");
expect(result.sort()).toEqual(["a", "b", "c"]);
});
it("returns empty array when teams.list throws", async () => {
mockTeamsList.mockRejectedValue(new Error("API error"));
const result = await repo.getAllMembers("org", "token");
expect(result).toEqual([]);
});
});
describe("ProjectRepository.createRelease", () => {
const repo = new ProjectRepository();
beforeEach(() => {
mockCreateRelease.mockReset();
});
it("returns release html_url on success", async () => {
mockCreateRelease.mockResolvedValue({
data: { html_url: "https://github.com/o/r/releases/tag/v1.0" },
});
const result = await repo.createRelease(
"owner",
"repo",
"v1.0",
"First release",
"Changelog",
"token"
);
expect(result).toBe("https://github.com/o/r/releases/tag/v1.0");
expect(mockCreateRelease).toHaveBeenCalledWith(
expect.objectContaining({
owner: "owner",
repo: "repo",
tag_name: "v1.0",
name: "v1.0 - First release",
body: "Changelog",
})
);
});
it("returns undefined when create fails", async () => {
mockCreateRelease.mockRejectedValue(new Error("API error"));
const result = await repo.createRelease("o", "r", "1.0", "Title", "Body", "token");
expect(result).toBeUndefined();
});
});
describe("ProjectRepository.createTag", () => {
const repo = new ProjectRepository();
beforeEach(() => {
mockGetRef.mockReset();
mockCreateRef.mockReset();
});
it("returns existing tag sha when tag already exists", async () => {
mockGetRef.mockResolvedValue({
data: { object: { sha: "abc123" } },
});
const result = await repo.createTag("owner", "repo", "main", "v1.0", "token");
expect(result).toBe("abc123");
expect(mockCreateRef).not.toHaveBeenCalled();
});
it("creates tag from branch and returns sha", async () => {
mockGetRef
.mockRejectedValueOnce(new Error("Not found"))
.mockResolvedValueOnce({ data: { object: { sha: "branch-sha" } } });
mockCreateRef.mockResolvedValue(undefined);
const result = await repo.createTag("owner", "repo", "main", "v1.0", "token");
expect(result).toBe("branch-sha");
expect(mockCreateRef).toHaveBeenCalledWith(
expect.objectContaining({
owner: "owner",
repo: "repo",
ref: "refs/tags/v1.0",
sha: "branch-sha",
})
);
});
it("returns undefined when branch ref fails", async () => {
mockGetRef.mockRejectedValue(new Error("Not found"));
const result = await repo.createTag("owner", "repo", "main", "v1.0", "token");
expect(result).toBeUndefined();
});
});
describe("ProjectRepository.updateTag", () => {
const repo = new ProjectRepository();
beforeEach(() => {
mockGetRef.mockReset();
mockCreateRef.mockReset();
mockUpdateRef.mockReset();
});
it("updates existing target tag to point to source", async () => {
mockGetRef
.mockResolvedValueOnce({ data: { object: { sha: "source-sha" } } })
.mockResolvedValueOnce({ data: { object: { sha: "old-target-sha" } } });
mockUpdateRef.mockResolvedValue(undefined);
await repo.updateTag("owner", "repo", "v1.0", "latest", "token");
expect(mockUpdateRef).toHaveBeenCalledWith(
expect.objectContaining({
owner: "owner",
repo: "repo",
ref: "tags/latest",
sha: "source-sha",
force: true,
})
);
});
it("creates target ref when target tag does not exist", async () => {
mockGetRef
.mockResolvedValueOnce({ data: { object: { sha: "source-sha" } } })
.mockRejectedValueOnce(new Error("Not found"));
mockCreateRef.mockResolvedValue(undefined);
await repo.updateTag("owner", "repo", "v1.0", "latest", "token");
expect(mockCreateRef).toHaveBeenCalledWith(
expect.objectContaining({
ref: "refs/tags/latest",
sha: "source-sha",
})
);
});
it("returns early when source tag does not exist", async () => {
mockGetRef.mockRejectedValue(new Error("Not found"));
await repo.updateTag("owner", "repo", "missing-tag", "latest", "token");
expect(mockUpdateRef).not.toHaveBeenCalled();
expect(mockCreateRef).not.toHaveBeenCalled();
});
});
describe("ProjectRepository.updateRelease", () => {
const repo = new ProjectRepository();
beforeEach(() => {
mockGetReleaseByTag.mockReset();
mockListReleases.mockReset();
mockUpdateRelease.mockReset();
mockCreateRelease.mockReset();
});
it("updates existing release when target exists", async () => {
mockGetReleaseByTag.mockResolvedValue({
data: {
name: "v1.0",
body: "Changelog",
draft: false,
prerelease: false,
},
});
mockListReleases.mockResolvedValue({
data: [{ tag_name: "latest", id: 42 }],
});
mockUpdateRelease.mockResolvedValue({ data: { id: 42 } });
const result = await repo.updateRelease("owner", "repo", "v1.0", "latest", "token");
expect(result).toBe("42");
expect(mockUpdateRelease).toHaveBeenCalledWith(
expect.objectContaining({
owner: "owner",
repo: "repo",
release_id: 42,
name: "v1.0",
body: "Changelog",
})
);
});
it("creates new release when target tag has no release", async () => {
mockGetReleaseByTag.mockResolvedValue({
data: {
name: "v1.0",
body: "Changelog",
draft: false,
prerelease: false,
},
});
mockListReleases.mockResolvedValue({ data: [] });
mockCreateRelease.mockResolvedValue({ data: { id: 100 } });
const result = await repo.updateRelease("owner", "repo", "v1.0", "latest", "token");
expect(result).toBe("100");
expect(mockCreateRelease).toHaveBeenCalledWith(
expect.objectContaining({
tag_name: "latest",
name: "v1.0",
body: "Changelog",
})
);
});
it("returns undefined when source release has no name or body", async () => {
mockGetReleaseByTag.mockResolvedValue({
data: { name: "", body: null, draft: false, prerelease: false },
});
const result = await repo.updateRelease("owner", "repo", "v1.0", "latest", "token");
expect(result).toBeUndefined();
expect(mockListReleases).not.toHaveBeenCalled();
});
});
describe("ProjectRepository.setTaskPriority", () => {
const repo = new ProjectRepository();
const project = new ProjectDetail({
id: "PVT_1",
title: "P",
url: "https://example.com",
type: "orgs",
owner: "org",
number: 1,
});
beforeEach(() => {
mockGraphql.mockReset();
});
const fieldQueryResponseWithItem = {
node: {
fields: {
nodes: [
{
id: "f1",
name: "Priority",
options: [{ id: "opt_high", name: "High" }],
},
],
},
items: {
nodes: [
{
id: "item_1",
fieldValues: { nodes: [] },
},
],
pageInfo: { hasNextPage: false, endCursor: null },
},
},
};
it("sets priority and returns true when mutation succeeds", async () => {
mockGraphql
.mockResolvedValueOnce({
repository: { issueOrPullRequest: { id: "I_issue1" } },
})
.mockResolvedValueOnce({
node: {
items: {
nodes: [{ id: "item_1", content: { id: "I_issue1" } }],
pageInfo: { hasNextPage: false, endCursor: null },
},
},
})
.mockResolvedValueOnce(fieldQueryResponseWithItem)
.mockResolvedValueOnce(fieldQueryResponseWithItem)
.mockResolvedValueOnce({
updateProjectV2ItemFieldValue: { projectV2Item: { id: "item_1" } },
});
const result = await repo.setTaskPriority(
project,
"owner",
"repo",
1,
"High",
"token"
);
expect(result).toBe(true);
expect(mockGraphql).toHaveBeenCalledTimes(5);
});
it("finds issue on second page of project items (pagination)", async () => {
const firstPage = {
node: {
items: {
nodes: [
{ id: "item_other_1", content: { id: "I_other1" } },
{ id: "item_other_2", content: { id: "I_other2" } },
],
pageInfo: { hasNextPage: true, endCursor: "cursor_page1" },
},
},
};
const secondPage = {
node: {
items: {
nodes: [{ id: "item_issue_1", content: { id: "I_issue1" } }],
pageInfo: { hasNextPage: false, endCursor: null },
},
},
};
const fieldResponseWithIssueItem = {
node: {
fields: {
nodes: [
{
id: "f1",
name: "Priority",
options: [{ id: "opt_high", name: "High" }],
},
],
},
items: {
nodes: [
{
id: "item_issue_1",
fieldValues: { nodes: [] },
},
],
pageInfo: { hasNextPage: false, endCursor: null },
},
},
};
mockGraphql
.mockResolvedValueOnce({
repository: { issueOrPullRequest: { id: "I_issue1" } },
})
.mockResolvedValueOnce(firstPage)
.mockResolvedValueOnce(secondPage)
.mockResolvedValueOnce(fieldResponseWithIssueItem)
.mockResolvedValueOnce(fieldResponseWithIssueItem)
.mockResolvedValueOnce({
updateProjectV2ItemFieldValue: { projectV2Item: { id: "item_issue_1" } },
});
const result = await repo.setTaskPriority(
project,
"owner",
"repo",
1,
"High",
"token"
);
expect(result).toBe(true);
expect(mockGraphql).toHaveBeenCalledTimes(6);
const firstItemsCall = mockGraphql.mock.calls[1];
const secondItemsCall = mockGraphql.mock.calls[2];
expect((firstItemsCall[1] as { cursor?: string | null }).cursor).toBeNull();
expect((secondItemsCall[1] as { cursor?: string | null }).cursor).toBe("cursor_page1");
});
it("returns false when field already set to target value", async () => {
const fieldQueryResponseAlreadySet = {
node: {
fields: {
nodes: [
{
id: "f1",
name: "Priority",
options: [{ id: "opt_high", name: "High" }],
},
],
},
items: {
nodes: [
{
id: "item_1",
fieldValues: {
nodes: [
{
field: { name: "Priority" },
optionId: "opt_high",
},
],
},
},
],
pageInfo: { hasNextPage: false, endCursor: null },
},
},
};
mockGraphql
.mockResolvedValueOnce({
repository: { issueOrPullRequest: { id: "I_issue1" } },
})
.mockResolvedValueOnce({
node: {
items: {
nodes: [{ id: "item_1", content: { id: "I_issue1" } }],
pageInfo: { hasNextPage: false, endCursor: null },
},
},
})
.mockResolvedValueOnce(fieldQueryResponseAlreadySet)
.mockResolvedValueOnce(fieldQueryResponseAlreadySet);
const result = await repo.setTaskPriority(
project,
"owner",
"repo",
1,
"High",
"token"
);
expect(result).toBe(false);
expect(mockGraphql).toHaveBeenCalledTimes(4);
});
it("throws when content id not found for issue (issue/PR not in repo)", async () => {
const { logError } = require("../../../utils/logger");
(logError as jest.Mock).mockClear();
mockGraphql.mockResolvedValueOnce({
repository: { issueOrPullRequest: null },
});
await expect(
repo.setTaskPriority(project, "owner", "repo", 999, "High", "token")
).rejects.toThrow("Content ID not found");
expect(logError).toHaveBeenCalledWith(
expect.stringContaining("999 not found in repository")
);
});
it("throws when project node is null (invalid project ID)", async () => {
const { logError } = require("../../../utils/logger");
(logError as jest.Mock).mockClear();
mockGraphql
.mockResolvedValueOnce({
repository: { issueOrPullRequest: { id: "I_issue1" } },
})
.mockResolvedValueOnce({ node: null });
await expect(
repo.setTaskPriority(project, "owner", "repo", 1, "High", "token")
).rejects.toThrow("Project not found or invalid project ID");
expect(logError).toHaveBeenCalledWith(
expect.stringContaining("Project not found for ID")
);
});
it("throws when issue/PR not in project yet", async () => {
const { logError } = require("../../../utils/logger");
(logError as jest.Mock).mockClear();
mockGraphql
.mockResolvedValueOnce({
repository: { issueOrPullRequest: { id: "I_issue1" } },
})
.mockResolvedValueOnce({
node: {
items: {
nodes: [], // issue not in project
pageInfo: { hasNextPage: false, endCursor: null },
},
},
});
await expect(
repo.setTaskPriority(project, "owner", "repo", 1, "High", "token")
).rejects.toThrow("not in the project yet");
expect(logError).toHaveBeenCalledWith(
expect.stringContaining("not found in project after checking")
);
});
it("logs error when hasNextPage is true but endCursor is null", async () => {
const { logError } = require("../../../utils/logger");
(logError as jest.Mock).mockClear();
mockGraphql
.mockResolvedValueOnce({
repository: { issueOrPullRequest: { id: "I_issue1" } },
})
.mockResolvedValueOnce({
node: {
items: {
nodes: [{ id: "other", content: { id: "I_other" } }],
pageInfo: { hasNextPage: true, endCursor: null },
},
},
});
await expect(
repo.setTaskPriority(project, "owner", "repo", 1, "High", "token")
).rejects.toThrow("not in the project yet");
expect(logError).toHaveBeenCalledWith(
expect.stringContaining("hasNextPage is true but endCursor is null")
);
});
it("stops after maxPages and throws when item not found", async () => {
const { logError } = require("../../../utils/logger");
(logError as jest.Mock).mockClear();
const pageWithNext = {
node: {
items: {
nodes: Array.from({ length: 100 }, (_, i) => ({
id: `item_${i}`,
content: { id: `I_other_${i}` },
})),
pageInfo: { hasNextPage: true, endCursor: "next" },
},
},
};
mockGraphql.mockResolvedValueOnce({
repository: { issueOrPullRequest: { id: "I_issue1" } },
});
for (let p = 0; p < 100; p++) {
mockGraphql.mockResolvedValueOnce(pageWithNext);
}
await expect(
repo.setTaskPriority(project, "owner", "repo", 1, "High", "token")
).rejects.toThrow("not in the project yet");
expect(logError).toHaveBeenCalledWith(
expect.stringContaining("Stopped after 100 pages")
);
});
});