|
| 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, vi } from 'vitest'; |
| 18 | +import { populateRepoDates } from '../../../src/db/migrations/populateRepoDates'; |
| 19 | +import type { Sink } from '../../../src/db/types'; |
| 20 | + |
| 21 | +type RepoRecord = Record<string, unknown> & { _id: string }; |
| 22 | + |
| 23 | +const makeSink = (repos: RepoRecord[], deriveCreatedAt: (id: string) => string | undefined) => { |
| 24 | + const store = new Map(repos.map((r) => [r._id, { ...r }])); |
| 25 | + const updateRepo = vi.fn(async (repo: Record<string, unknown>) => { |
| 26 | + const { _id, ...fields } = repo; |
| 27 | + Object.assign(store.get(_id as string) as RepoRecord, fields); |
| 28 | + }); |
| 29 | + const sink = { |
| 30 | + getRepos: async () => [...store.values()], |
| 31 | + updateRepo, |
| 32 | + deriveCreatedAt, |
| 33 | + } as unknown as Sink; |
| 34 | + return { sink, updateRepo, get: (id: string) => store.get(id) }; |
| 35 | +}; |
| 36 | + |
| 37 | +describe('populateRepoDates migration', () => { |
| 38 | + it('is registered under a sortable, timestamped id', () => { |
| 39 | + expect(populateRepoDates.id).toBe('20260729-populate-repo-dates'); |
| 40 | + }); |
| 41 | + |
| 42 | + it('backfills dateCreated from the backend-derived value', async () => { |
| 43 | + const { sink, get } = makeSink([{ _id: 'r1' }], () => '2020-01-01T00:00:00.000Z'); |
| 44 | + |
| 45 | + await populateRepoDates.up(sink); |
| 46 | + |
| 47 | + expect(get('r1')?.dateCreated).toBe('2020-01-01T00:00:00.000Z'); |
| 48 | + expect(get('r1')?.lastModified).toBe('2020-01-01T00:00:00.000Z'); |
| 49 | + }); |
| 50 | + |
| 51 | + it('falls back to the run time when the backend cannot derive one', async () => { |
| 52 | + // the file backend always returns undefined - NeDB ids carry no timestamp |
| 53 | + const before = Date.now(); |
| 54 | + const { sink, get } = makeSink([{ _id: 'r1' }], () => undefined); |
| 55 | + |
| 56 | + await populateRepoDates.up(sink); |
| 57 | + |
| 58 | + const created = get('r1')?.dateCreated as string; |
| 59 | + expect(Date.parse(created)).toBeGreaterThanOrEqual(before); |
| 60 | + expect(get('r1')?.lastModified).toBe(created); |
| 61 | + }); |
| 62 | + |
| 63 | + it('preserves an existing lastModified instead of overwriting it', async () => { |
| 64 | + // role mutations bump lastModified on repos that never had a dateCreated |
| 65 | + const { sink, get } = makeSink( |
| 66 | + [{ _id: 'r1', lastModified: '2024-06-01T00:00:00.000Z' }], |
| 67 | + () => '2020-01-01T00:00:00.000Z', |
| 68 | + ); |
| 69 | + |
| 70 | + await populateRepoDates.up(sink); |
| 71 | + |
| 72 | + expect(get('r1')?.dateCreated).toBe('2020-01-01T00:00:00.000Z'); |
| 73 | + expect(get('r1')?.lastModified).toBe('2024-06-01T00:00:00.000Z'); |
| 74 | + }); |
| 75 | + |
| 76 | + it('skips repos that already have a dateCreated, so it is safe to re-run', async () => { |
| 77 | + const { sink, updateRepo, get } = makeSink( |
| 78 | + [{ _id: 'r1', dateCreated: '2019-05-05T00:00:00.000Z' }], |
| 79 | + () => '2020-01-01T00:00:00.000Z', |
| 80 | + ); |
| 81 | + |
| 82 | + await populateRepoDates.up(sink); |
| 83 | + |
| 84 | + expect(updateRepo).not.toHaveBeenCalled(); |
| 85 | + expect(get('r1')?.dateCreated).toBe('2019-05-05T00:00:00.000Z'); |
| 86 | + }); |
| 87 | + |
| 88 | + it('backfills only the repos that need it', async () => { |
| 89 | + const { sink, updateRepo, get } = makeSink( |
| 90 | + [{ _id: 'r1' }, { _id: 'r2', dateCreated: '2019-05-05T00:00:00.000Z' }, { _id: 'r3' }], |
| 91 | + (id) => `2020-01-0${id === 'r1' ? 1 : 3}T00:00:00.000Z`, |
| 92 | + ); |
| 93 | + |
| 94 | + await populateRepoDates.up(sink); |
| 95 | + |
| 96 | + expect(updateRepo).toHaveBeenCalledTimes(2); |
| 97 | + expect(get('r1')?.dateCreated).toBe('2020-01-01T00:00:00.000Z'); |
| 98 | + expect(get('r2')?.dateCreated).toBe('2019-05-05T00:00:00.000Z'); |
| 99 | + expect(get('r3')?.dateCreated).toBe('2020-01-03T00:00:00.000Z'); |
| 100 | + }); |
| 101 | + |
| 102 | + it('down clears both timestamps', async () => { |
| 103 | + const { sink, get } = makeSink( |
| 104 | + [ |
| 105 | + { |
| 106 | + _id: 'r1', |
| 107 | + dateCreated: '2020-01-01T00:00:00.000Z', |
| 108 | + lastModified: '2020-01-01T00:00:00.000Z', |
| 109 | + }, |
| 110 | + ], |
| 111 | + () => undefined, |
| 112 | + ); |
| 113 | + |
| 114 | + await populateRepoDates.down!(sink); |
| 115 | + |
| 116 | + expect(get('r1')?.dateCreated).toBeUndefined(); |
| 117 | + expect(get('r1')?.lastModified).toBeUndefined(); |
| 118 | + }); |
| 119 | +}); |
0 commit comments