| 1 | import { Test, type TestingModule } from '@nestjs/testing'; |
| 2 | import { createDatabase, schema, type Database, type Pool } from '@ghost/db'; |
| 3 | import { eq } from 'drizzle-orm'; |
| 4 | import { migrate } from 'drizzle-orm/node-postgres/migrator'; |
| 5 | import { execFileSync } from 'node:child_process'; |
| 6 | import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from 'node:fs'; |
| 7 | import { tmpdir } from 'node:os'; |
| 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 } from '../../../database/database.module.js'; |
| 20 | import { RepositoryLanguageService } from './repository-language.service.js'; |
| 21 | |
| 22 | const CONNECTION = process.env.TEST_DATABASE_URL; |
| 23 | const REF = 'refs/heads/main'; |
| 24 | const MIGRATIONS = path.resolve( |
| 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)('RepositoryLanguageService', () => { |
| 31 | let db: Database; |
| 32 | let pool: Pool; |
| 33 | let service: RepositoryLanguageService; |
| 34 | let root: string; |
| 35 | let gitDir: string; |
| 36 | let repositoryId: string; |
| 37 | |
| 38 | function git(...args: string[]) { |
| 39 | const env = { ...process.env }; |
| 40 | delete env.GIT_DIR; |
| 41 | return execFileSync('git', args, { |
| 42 | cwd: root, |
| 43 | encoding: 'utf8', |
| 44 | env, |
| 45 | }).trim(); |
| 46 | } |
| 47 | |
| 48 | function commit(file: string, contents: string, message: string) { |
| 49 | mkdirSync(path.dirname(path.join(root, file)), { recursive: true }); |
| 50 | writeFileSync(path.join(root, file), contents); |
| 51 | git('add', file); |
| 52 | git('commit', '-m', message); |
| 53 | return git('rev-parse', 'HEAD'); |
| 54 | } |
| 55 | |
| 56 | const languages = () => |
| 57 | service.getLanguages({ repositoryId, repoDirectory: gitDir, ref: REF }); |
| 58 | |
| 59 | const bytesOf = async (language: string) => |
| 60 | (await languages()).find((row) => row.language === language)?.bytes; |
| 61 | |
| 62 | beforeAll(async () => { |
| 63 | ({ db, pool } = createDatabase({ connectionString: CONNECTION })); |
| 64 | await migrate(db, { migrationsFolder: MIGRATIONS }); |
| 65 | |
| 66 | const module: TestingModule = await Test.createTestingModule({ |
| 67 | providers: [ |
| 68 | RepositoryLanguageService, |
| 69 | { provide: DATABASE, useValue: db }, |
| 70 | ], |
| 71 | }).compile(); |
| 72 | service = module.get(RepositoryLanguageService); |
| 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.user.id, 'user_language_spec')); |
| 88 | await pool.end(); |
| 89 | }); |
| 90 | |
| 91 | beforeEach(async () => { |
| 92 | root = mkdtempSync(path.join(tmpdir(), 'ghost-languages-')); |
| 93 | gitDir = path.join(root, '.git'); |
| 94 | execFileSync('git', ['init', '-q', '-b', 'main', root]); |
| 95 | git('config', 'user.email', 'test@example.com'); |
| 96 | git('config', 'user.name', 'Test'); |
| 97 | |
| 98 | const [repository] = await db |
| 99 | .insert(schema.repository) |
| 100 | .values({ |
| 101 | name: 'spec', |
| 102 | slug: `spec-${Date.now()}`, |
| 103 | ownerId: 'user_language_spec', |
| 104 | }) |
| 105 | .returning(); |
| 106 | repositoryId = repository.id; |
| 107 | }); |
| 108 | |
| 109 | afterEach(async () => { |
| 110 | rmSync(root, { recursive: true, force: true }); |
| 111 | // cascades to both language tables |
| 112 | await db |
| 113 | .delete(schema.repository) |
| 114 | .where(eq(schema.repository.id, repositoryId)); |
| 115 | }); |
| 116 | |
| 117 | it('returns nothing when the ref does not exist', async () => { |
| 118 | expect(await languages()).toEqual([]); |
| 119 | }); |
| 120 | |
| 121 | it('counts bytes per language, largest first, ignoring unknown files', async () => { |
| 122 | commit('src/a.ts', 'x'.repeat(30), 'add a'); |
| 123 | commit('src/b.py', 'y'.repeat(10), 'add b'); |
| 124 | commit('LICENSE', 'z'.repeat(500), 'add a file with no language'); |
| 125 | |
| 126 | expect(await languages()).toEqual([ |
| 127 | { language: 'TypeScript', bytes: 30 }, |
| 128 | { language: 'Python', bytes: 10 }, |
| 129 | ]); |
| 130 | }); |
| 131 | |
| 132 | it('applies a fast-forward as a delta', async () => { |
| 133 | commit('src/a.ts', 'x'.repeat(30), 'add a'); |
| 134 | await languages(); |
| 135 | |
| 136 | commit('src/a.ts', 'x'.repeat(50), 'grow a'); |
| 137 | commit('src/c.go', 'g'.repeat(7), 'add c'); |
| 138 | |
| 139 | expect(await languages()).toEqual([ |
| 140 | { language: 'TypeScript', bytes: 50 }, |
| 141 | { language: 'Go', bytes: 7 }, |
| 142 | ]); |
| 143 | }); |
| 144 | |
| 145 | it('drops a language once its last file is deleted', async () => { |
| 146 | commit('src/a.ts', 'x'.repeat(30), 'add a'); |
| 147 | commit('src/b.py', 'y'.repeat(10), 'add b'); |
| 148 | await languages(); |
| 149 | |
| 150 | git('rm', '-q', 'src/b.py'); |
| 151 | git('commit', '-m', 'remove b'); |
| 152 | |
| 153 | expect(await bytesOf('Python')).toBeUndefined(); |
| 154 | expect(await bytesOf('TypeScript')).toBe(30); |
| 155 | }); |
| 156 | |
| 157 | it('recounts from the tree after a force push', async () => { |
| 158 | const first = commit('src/a.ts', 'x'.repeat(30), 'add a'); |
| 159 | commit('src/gone.py', 'y'.repeat(10), 'add a file to discard'); |
| 160 | await languages(); |
| 161 | expect(await bytesOf('Python')).toBe(10); |
| 162 | |
| 163 | git('reset', '-q', '--hard', first); |
| 164 | commit('src/kept.go', 'g'.repeat(4), 'rewritten history'); |
| 165 | |
| 166 | expect(await languages()).toEqual([ |
| 167 | { language: 'TypeScript', bytes: 30 }, |
| 168 | { language: 'Go', bytes: 4 }, |
| 169 | ]); |
| 170 | }); |
| 171 | |
| 172 | it('leaves the stored rows alone when the ref has not moved', async () => { |
| 173 | commit('src/a.ts', 'x'.repeat(30), 'add a'); |
| 174 | await languages(); |
| 175 | |
| 176 | const before = await db |
| 177 | .select() |
| 178 | .from(schema.repositoryLanguageIndex) |
| 179 | .where(eq(schema.repositoryLanguageIndex.repositoryId, repositoryId)); |
| 180 | await languages(); |
| 181 | const after = await db |
| 182 | .select() |
| 183 | .from(schema.repositoryLanguageIndex) |
| 184 | .where(eq(schema.repositoryLanguageIndex.repositoryId, repositoryId)); |
| 185 | |
| 186 | expect(after).toEqual(before); |
| 187 | }); |
| 188 | |
| 189 | it('skips symlinks', async () => { |
| 190 | commit('src/a.ts', 'x'.repeat(30), 'add a'); |
| 191 | execFileSync('ln', ['-s', 'a.ts', path.join(root, 'src/link.ts')]); |
| 192 | git('add', 'src/link.ts'); |
| 193 | git('commit', '-m', 'add a symlink'); |
| 194 | |
| 195 | expect(await bytesOf('TypeScript')).toBe(30); |
| 196 | }); |
| 197 | }); |