| 1 | import { Test, type TestingModule } from '@nestjs/testing'; |
| 2 | import { createDatabase, schema |
| 3 | import { eq } |
| 4 | import { migrate } |
| 5 | import { execFileSync } |
| 6 | import { mkdirSync, mkdtempSync |
| 7 | import { tmpdir } |
| 8 | import path from 'node:path |
| 9 | import { |
| 10 | afterAll, |
| 11 | afterEach, |
| 12 | beforeAll, |
| 13 | beforeEach, |
| 14 | describe, |
| 15 | expect, |
| 16 | it, |
| 17 | } from 'vitest'; |
| 18 | |
| 19 | import { DATABASE } |
| 20 | import { RepositoryLanguageService } |
| 21 | |
| 22 | const CONNECTION = process |
| 23 | const REF = ' |
| 24 | const MIGRATIONS = path |
| 25 | import.meta.dirname, |
| 26 | '../../../../../../packages/db/drizzle', |
| 27 | ); |
| 28 | |
| 29 | // Needs a throwaway Postgres; the suite is skipped without one. |
| 30 | describe.skipIf(!CONNECTION |
| 31 | let db: |
| 32 | let pool: |
| 33 | let service: |
| 34 | let root: |
| 35 | let gitDir: |
| 36 | let repositoryId: |
| 37 | |
| 38 | function git(... |
| 39 | const env = |
| 40 | delete env.GIT_DIR; |
| 41 | return execFileSync('git |
| 42 | cwd: root, |
| 43 | encoding: 'utf8', |
| 44 | env, |
| 45 | }).trim(); |
| 46 | } |
| 47 | |
| 48 | function commit(file |
| 49 | mkdirSync(path.dirname |
| 50 | writeFileSync(path.join |
| 51 | git('add', |
| 52 | git('commit', |
| 53 | return git('rev-parse |
| 54 | } |
| 55 | |
| 56 | const languages = |
| 57 | service.getLanguages({ |
| 58 | |
| 59 | const bytesOf = |
| 60 | (await languages()). |
| 61 | |
| 62 | beforeAll(async () |
| 63 | ({ db, pool |
| 64 | await migrate(db |
| 65 | |
| 66 | const module: |
| 67 | providers: [ |
| 68 | RepositoryLanguageService, |
| 69 | { provide: DATABASE, |
| 70 | ], |
| 71 | }).compile(); |
| 72 | service = module. |
| 73 | |
| 74 | await db |
| 75 | .insert(schema.user |
| 76 | .values({ |
| 77 | id: 'user_language_spec', |
| 78 | name: 'Test', |
| 79 | email: 'languages@example.com', |
| 80 | }) |
| 81 | .onConflictDoNothing(); |
| 82 | }); |
| 83 | |
| 84 | afterAll(async () |
| 85 | await db |
| 86 | .delete(schema.user |
| 87 | .where(eq(schema |
| 88 | await pool.end |
| 89 | }); |
| 90 | |
| 91 | beforeEach(async () |
| 92 | root = mkdtempSync( |
| 93 | gitDir = path. |
| 94 | execFileSync('git', |
| 95 | git('config', |
| 96 | git('config', |
| 97 | |
| 98 | const [repository] |
| 99 | .insert(schema.repository |
| 100 | .values({ |
| 101 | name: 'spec', |
| 102 | slug: `spec-${Date |
| 103 | ownerId: 'user_language_spec', |
| 104 | }) |
| 105 | .returning(); |
| 106 | repositoryId = repository. |
| 107 | }); |
| 108 | |
| 109 | afterEach(async () |
| 110 | rmSync(root, { |
| 111 | // cascades to both language tables |
| 112 | await db |
| 113 | .delete(schema.repository |
| 114 | .where(eq(schema |
| 115 | }); |
| 116 | |
| 117 | it('returns nothing when the ref does not exist', |
| 118 | expect(await languages |
| 119 | }); |
| 120 | |
| 121 | it('counts bytes per language, largest first, ignoring unknown files', |
| 122 | commit('src/a.ts', |
| 123 | commit('src/b.py', |
| 124 | commit('LICENSE', |
| 125 | |
| 126 | expect(await languages |
| 127 | { language: 'TypeScript', |
| 128 | { language: 'Python', |
| 129 | ]); |
| 130 | }); |
| 131 | |
| 132 | it('applies a fast-forward as a delta', |
| 133 | commit('src/a.ts', |
| 134 | await languages(); |
| 135 | |
| 136 | commit('src/a.ts', |
| 137 | commit('src/c.go', |
| 138 | |
| 139 | expect(await languages |
| 140 | { language: 'TypeScript', |
| 141 | { language: 'Go', |
| 142 | ]); |
| 143 | }); |
| 144 | |
| 145 | it('drops a language once its last file is deleted', |
| 146 | commit('src/a.ts', |
| 147 | commit('src/b.py', |
| 148 | await languages(); |
| 149 | |
| 150 | git('rm', |
| 151 | git('commit', |
| 152 | |
| 153 | expect(await bytesOf |
| 154 | expect(await bytesOf |
| 155 | }); |
| 156 | |
| 157 | it('recounts from the tree after a force push', |
| 158 | const first = |
| 159 | commit('src/gone.py', |
| 160 | await languages(); |
| 161 | expect(await bytesOf |
| 162 | |
| 163 | git('reset', |
| 164 | commit('src/kept.go', |
| 165 | |
| 166 | expect(await languages |
| 167 | { language: 'TypeScript', |
| 168 | { language: 'Go', |
| 169 | ]); |
| 170 | }); |
| 171 | |
| 172 | it('leaves the stored rows alone when the ref has not moved', |
| 173 | commit('src/a.ts', |
| 174 | await languages(); |
| 175 | |
| 176 | const before = |
| 177 | .select() |
| 178 | .from(schema.repositoryLanguageIndex |
| 179 | .where(eq(schema |
| 180 | await languages(); |
| 181 | const after = |
| 182 | .select() |
| 183 | .from(schema.repositoryLanguageIndex |
| 184 | .where(eq(schema |
| 185 | |
| 186 | expect(after).toEqual |
| 187 | }); |
| 188 | |
| 189 | it('skips symlinks', |
| 190 | commit('src/a.ts', |
| 191 | execFileSync('ln', |
| 192 | git('add', |
| 193 | git('commit', |
| 194 | |
| 195 | expect(await bytesOf |
| 196 | }); |
| 197 | }); |