|
4 | 4 | import { describe, expect, it, vi } from 'vitest' |
5 | 5 | import { atlassianActivity } from '@/lib/sim-search/live/atlassian' |
6 | 6 | import { githubActivity } from '@/lib/sim-search/live/github' |
| 7 | +import { gitLabActivity, gitLabMemberActivity } from '@/lib/sim-search/live/gitlab-activity' |
7 | 8 | import { calendarActivity, driveActivity, gmailActivity } from '@/lib/sim-search/live/google' |
8 | 9 | import { linearActivity } from '@/lib/sim-search/live/linear' |
9 | 10 | import { slackActivity } from '@/lib/sim-search/live/slack' |
@@ -627,3 +628,151 @@ describe('Linear activity format', () => { |
627 | 628 | await expect(linearActivity(api, WINDOW)).rejects.toThrow('unsupported result format') |
628 | 629 | }) |
629 | 630 | }) |
| 631 | + |
| 632 | +describe('GitLab activity', () => { |
| 633 | + const event = (id: number, at: string, project: number, fields: Record<string, unknown>) => ({ |
| 634 | + id, |
| 635 | + project_id: project, |
| 636 | + created_at: at, |
| 637 | + ...fields, |
| 638 | + }) |
| 639 | + /** Answers the member's events a page at a time and each project's lookup. */ |
| 640 | + const gitlab = (pages: Record<string, unknown>[][], projects: number[] = [42]) => |
| 641 | + client((path, options) => { |
| 642 | + if (path === '/api/v4/user') return { id: 7 } |
| 643 | + if (path === '/api/v4/users/7/events') return pages[Number(options?.query?.page) - 1] ?? [] |
| 644 | + const project = /^\/api\/v4\/projects\/(\d+)$/.exec(path)?.[1] |
| 645 | + if (project && projects.includes(Number(project))) |
| 646 | + return { web_url: `https://git.example.com/team/p${project}` } |
| 647 | + return undefined |
| 648 | + }) |
| 649 | + |
| 650 | + it('reports what the member opened, commented on, approved and pushed, leaving out internal notes and diff comment text', async () => { |
| 651 | + const api = gitlab([ |
| 652 | + [ |
| 653 | + event(5, '2026-11-07T10:00:00Z', 42, { |
| 654 | + action_name: 'opened', |
| 655 | + target_type: 'MergeRequest', |
| 656 | + target_iid: 3, |
| 657 | + target_title: 'Retry uploads', |
| 658 | + }), |
| 659 | + event(4, '2026-11-06T10:00:00Z', 42, { |
| 660 | + action_name: 'commented on', |
| 661 | + target_type: 'DiffNote', |
| 662 | + target_title: 'Retry uploads', |
| 663 | + note: { body: 'Rename this file', noteable_type: 'MergeRequest', noteable_iid: 3 }, |
| 664 | + }), |
| 665 | + event(3, '2026-11-05T10:00:00Z', 42, { |
| 666 | + action_name: 'commented on', |
| 667 | + target_type: 'Note', |
| 668 | + note: { body: 'Secret', noteable_type: 'Issue', noteable_iid: 9, internal: true }, |
| 669 | + }), |
| 670 | + event(2, '2026-11-04T10:00:00Z', 42, { |
| 671 | + action_name: 'approved', |
| 672 | + target_type: 'MergeRequest', |
| 673 | + target_iid: 3, |
| 674 | + }), |
| 675 | + event(1, '2026-11-03T10:00:00Z', 42, { |
| 676 | + action_name: 'pushed', |
| 677 | + push_data: { ref_type: 'branch', commit_to: 'a'.repeat(40), commit_title: 'Fix' }, |
| 678 | + }), |
| 679 | + ], |
| 680 | + ]) |
| 681 | + const [stream] = (await gitLabMemberActivity(api, WINDOW)).streams |
| 682 | + expect( |
| 683 | + stream?.events.map(({ document, action }) => [ |
| 684 | + document.kind, |
| 685 | + document.id, |
| 686 | + action, |
| 687 | + document.url, |
| 688 | + ]) |
| 689 | + ).toEqual([ |
| 690 | + ['merge_requests', '3', 'created', 'https://git.example.com/team/p42/-/merge_requests/3'], |
| 691 | + ['merge_requests', '3', 'commented', 'https://git.example.com/team/p42/-/merge_requests/3'], |
| 692 | + ['merge_requests', '3', 'involved', 'https://git.example.com/team/p42/-/merge_requests/3'], |
| 693 | + [ |
| 694 | + 'commits', |
| 695 | + 'a'.repeat(40), |
| 696 | + 'committed', |
| 697 | + `https://git.example.com/team/p42/-/commit/${'a'.repeat(40)}`, |
| 698 | + ], |
| 699 | + ]) |
| 700 | + expect(stream?.events[1]?.document.content).not.toContain('Rename this file') |
| 701 | + expect(stream?.more).toBe(false) |
| 702 | + }) |
| 703 | + |
| 704 | + it('reads past events newer than the bound and reports where it stopped when it reaches none', async () => { |
| 705 | + const newer = Array.from({ length: 20 }, (_, index) => |
| 706 | + event( |
| 707 | + 100 - index, |
| 708 | + `2026-11-08T0${Math.floor(index / 10)}:00:${String(index % 10).padStart(2, '0')}Z`, |
| 709 | + 42, |
| 710 | + { |
| 711 | + action_name: 'updated', |
| 712 | + target_type: 'Issue', |
| 713 | + target_iid: index, |
| 714 | + } |
| 715 | + ) |
| 716 | + ) |
| 717 | + const api = gitlab([newer.slice(0, 15), newer.slice(15, 20)]) |
| 718 | + const [stream] = ( |
| 719 | + await gitLabMemberActivity(api, { ...WINDOW, end: '2026-11-07T23:59:59.000Z', limit: 15 }) |
| 720 | + ).streams |
| 721 | + expect(stream?.events).toEqual([]) |
| 722 | + expect(stream?.more).toBe(false) |
| 723 | + const full = gitlab([newer.slice(0, 15), newer.slice(0, 15), newer.slice(0, 15)]) |
| 724 | + const [capped] = ( |
| 725 | + await gitLabMemberActivity(full, { ...WINDOW, end: '2026-11-07T23:59:59.000Z', limit: 15 }) |
| 726 | + ).streams |
| 727 | + expect(capped?.more).toBe(true) |
| 728 | + expect(Date.parse(capped?.scannedTo ?? '')).toBeGreaterThan(Date.parse('2026-11-07T23:59:59Z')) |
| 729 | + }) |
| 730 | + |
| 731 | + it('stops at an event whose project it cannot look up yet, so the next page resumes there', async () => { |
| 732 | + const rows = [42, 43, 44, 45, 46, 47].map((project, index) => |
| 733 | + event(10 - index, `2026-11-0${7 - index}T10:00:00Z`, project, { |
| 734 | + action_name: 'opened', |
| 735 | + target_type: 'Issue', |
| 736 | + target_iid: index + 1, |
| 737 | + }) |
| 738 | + ) |
| 739 | + const api = gitlab([rows], [42, 43, 44, 45, 46, 47]) |
| 740 | + const [stream] = (await gitLabMemberActivity(api, WINDOW)).streams |
| 741 | + expect(stream?.events).toHaveLength(5) |
| 742 | + expect(stream).toMatchObject({ more: true, scannedTo: '2026-11-02T10:00:00.000Z' }) |
| 743 | + }) |
| 744 | + |
| 745 | + it('leaves out events in projects or collections an administrator source does not serve', async () => { |
| 746 | + const api = client((path) => |
| 747 | + path === '/api/v4/users/7/events' |
| 748 | + ? [ |
| 749 | + event(3, '2026-11-06T10:00:00Z', 42, { |
| 750 | + action_name: 'opened', |
| 751 | + target_type: 'Issue', |
| 752 | + target_iid: 1, |
| 753 | + }), |
| 754 | + event(2, '2026-11-05T10:00:00Z', 99, { |
| 755 | + action_name: 'opened', |
| 756 | + target_type: 'MergeRequest', |
| 757 | + target_iid: 2, |
| 758 | + }), |
| 759 | + event(1, '2026-11-04T10:00:00Z', 42, { |
| 760 | + action_name: 'opened', |
| 761 | + target_type: 'MergeRequest', |
| 762 | + target_iid: 3, |
| 763 | + }), |
| 764 | + ] |
| 765 | + : undefined |
| 766 | + ) |
| 767 | + const [stream] = ( |
| 768 | + await gitLabActivity(api, WINDOW, { |
| 769 | + path: '/api/v4/users/7/events', |
| 770 | + kinds: new Set(['merge_requests']), |
| 771 | + projectUrl: async (id) => (id === '42' ? 'https://git.example.com/team/p42' : undefined), |
| 772 | + }) |
| 773 | + ).streams |
| 774 | + expect(stream?.events.map(({ document }) => [document.container, document.id])).toEqual([ |
| 775 | + ['42', '3'], |
| 776 | + ]) |
| 777 | + }) |
| 778 | +}) |
0 commit comments