|
| 1 | +/** |
| 2 | + * Copyright 2026 GitProxy Contributors |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest'; |
| 18 | +import * as repoModule from '../../src/db/file/repo'; |
| 19 | +import { Repo } from '../../src/db/types'; |
| 20 | + |
| 21 | +describe('Repo dateCreated / lastModified (#1486)', () => { |
| 22 | + beforeEach(() => { |
| 23 | + vi.clearAllMocks(); |
| 24 | + }); |
| 25 | + |
| 26 | + afterEach(() => { |
| 27 | + vi.restoreAllMocks(); |
| 28 | + }); |
| 29 | + |
| 30 | + it('createRepo persists dateCreated and lastModified as ISO-8601', async () => { |
| 31 | + const inserted: Repo[] = []; |
| 32 | + vi.spyOn(repoModule.db, 'insert').mockImplementation((doc: unknown, cb: any) => { |
| 33 | + const stored = { ...(doc as Repo), _id: 'new-id' }; |
| 34 | + inserted.push(stored); |
| 35 | + cb(null, stored); |
| 36 | + }); |
| 37 | + |
| 38 | + const before = Date.now(); |
| 39 | + const result = await repoModule.createRepo( |
| 40 | + new Repo('finos', 'sample', 'https://github.com/finos/sample.git'), |
| 41 | + ); |
| 42 | + const after = Date.now(); |
| 43 | + |
| 44 | + expect(result.dateCreated).toEqual(expect.any(String)); |
| 45 | + expect(result.lastModified).toEqual(expect.any(String)); |
| 46 | + expect(Date.parse(result.dateCreated!)).toBeGreaterThanOrEqual(before); |
| 47 | + expect(Date.parse(result.dateCreated!)).toBeLessThanOrEqual(after); |
| 48 | + expect(result.dateCreated).toBe(result.lastModified); |
| 49 | + expect(inserted[0].dateCreated).toBe(result.dateCreated); |
| 50 | + }); |
| 51 | + |
| 52 | + it('addUserCanPush bumps lastModified but leaves dateCreated unchanged', async () => { |
| 53 | + const existing: Repo = { |
| 54 | + project: 'finos', |
| 55 | + name: 'sample', |
| 56 | + url: 'https://github.com/finos/sample.git', |
| 57 | + users: { canPush: [], canAuthorise: [] }, |
| 58 | + dateCreated: '2020-01-01T00:00:00.000Z', |
| 59 | + lastModified: '2020-01-01T00:00:00.000Z', |
| 60 | + _id: 'abc', |
| 61 | + }; |
| 62 | + |
| 63 | + vi.spyOn(repoModule.db, 'findOne').mockImplementation((_: unknown, cb: any) => |
| 64 | + cb(null, { ...existing }), |
| 65 | + ); |
| 66 | + let updatedDoc: Repo | null = null; |
| 67 | + vi.spyOn(repoModule.db, 'update').mockImplementation( |
| 68 | + (_q: unknown, doc: any, _o: unknown, cb: any) => { |
| 69 | + updatedDoc = doc; |
| 70 | + cb(null, 1); |
| 71 | + }, |
| 72 | + ); |
| 73 | + |
| 74 | + await repoModule.addUserCanPush('abc', 'alice'); |
| 75 | + |
| 76 | + expect(updatedDoc!.dateCreated).toBe('2020-01-01T00:00:00.000Z'); |
| 77 | + expect(updatedDoc!.lastModified).not.toBe('2020-01-01T00:00:00.000Z'); |
| 78 | + expect(Date.parse(updatedDoc!.lastModified!)).toBeGreaterThan( |
| 79 | + Date.parse('2020-01-01T00:00:00.000Z'), |
| 80 | + ); |
| 81 | + expect(updatedDoc!.users.canPush).toContain('alice'); |
| 82 | + }); |
| 83 | + |
| 84 | + it('removeUserCanAuthorise bumps lastModified but leaves dateCreated unchanged', async () => { |
| 85 | + const existing: Repo = { |
| 86 | + project: 'finos', |
| 87 | + name: 'sample', |
| 88 | + url: 'https://github.com/finos/sample.git', |
| 89 | + users: { canPush: [], canAuthorise: ['bob'] }, |
| 90 | + dateCreated: '2020-01-01T00:00:00.000Z', |
| 91 | + lastModified: '2020-01-01T00:00:00.000Z', |
| 92 | + _id: 'abc', |
| 93 | + }; |
| 94 | + |
| 95 | + vi.spyOn(repoModule.db, 'findOne').mockImplementation((_: unknown, cb: any) => |
| 96 | + cb(null, { ...existing }), |
| 97 | + ); |
| 98 | + let updatedDoc: Repo | null = null; |
| 99 | + vi.spyOn(repoModule.db, 'update').mockImplementation( |
| 100 | + (_q: unknown, doc: any, _o: unknown, cb: any) => { |
| 101 | + updatedDoc = doc; |
| 102 | + cb(null, 1); |
| 103 | + }, |
| 104 | + ); |
| 105 | + |
| 106 | + await repoModule.removeUserCanAuthorise('abc', 'bob'); |
| 107 | + |
| 108 | + expect(updatedDoc!.dateCreated).toBe('2020-01-01T00:00:00.000Z'); |
| 109 | + expect(updatedDoc!.lastModified).not.toBe('2020-01-01T00:00:00.000Z'); |
| 110 | + expect(updatedDoc!.users.canAuthorise).not.toContain('bob'); |
| 111 | + }); |
| 112 | + |
| 113 | + it('sort by dateCreated orders oldest → newest (asc)', () => { |
| 114 | + const repos: Pick<Repo, 'name' | 'dateCreated'>[] = [ |
| 115 | + { name: 'zeta', dateCreated: '2024-06-01T00:00:00.000Z' }, |
| 116 | + { name: 'alpha', dateCreated: '2023-01-01T00:00:00.000Z' }, |
| 117 | + { name: 'mid', dateCreated: '2023-12-01T00:00:00.000Z' }, |
| 118 | + ]; |
| 119 | + |
| 120 | + const sorted = [...repos].sort( |
| 121 | + (a, b) => new Date(a.dateCreated || 0).getTime() - new Date(b.dateCreated || 0).getTime(), |
| 122 | + ); |
| 123 | + |
| 124 | + expect(sorted.map((r) => r.name)).toEqual(['alpha', 'mid', 'zeta']); |
| 125 | + }); |
| 126 | + |
| 127 | + it('sort by lastModified orders oldest → newest (asc)', () => { |
| 128 | + const repos: Pick<Repo, 'name' | 'lastModified'>[] = [ |
| 129 | + { name: 'fresh', lastModified: '2025-01-01T00:00:00.000Z' }, |
| 130 | + { name: 'stale', lastModified: '2022-01-01T00:00:00.000Z' }, |
| 131 | + { name: 'mid', lastModified: '2024-01-01T00:00:00.000Z' }, |
| 132 | + ]; |
| 133 | + |
| 134 | + const sorted = [...repos].sort( |
| 135 | + (a, b) => new Date(a.lastModified || 0).getTime() - new Date(b.lastModified || 0).getTime(), |
| 136 | + ); |
| 137 | + |
| 138 | + expect(sorted.map((r) => r.name)).toEqual(['stale', 'mid', 'fresh']); |
| 139 | + }); |
| 140 | +}); |
0 commit comments