hoppscotch-backend-git/tests/git-workspace-nested.test.ts
tlemesle 6c686122b6 Set workspace git author from local OS identity
Co-authored-by: Cursor <cursoragent@cursor.com>
2026-07-24 17:04:50 +02:00

78 lines
3.1 KiB
TypeScript

import { afterAll, describe, expect, test } from 'bun:test';
import { access, mkdtemp, mkdir, rm, writeFile } from 'node:fs/promises';
import { tmpdir } from 'node:os';
import { join } from 'node:path';
import { createSampleCollection } from '../fixtures/sample.ts';
import { loadConfig } from '../src/config.ts';
import { createGitWorkspace } from '../src/store/git-workspace.ts';
describe('git workspace nested under a parent repo', () => {
const roots: string[] = [];
afterAll(async () => {
for(const root of roots)
await rm(root, { recursive: true, force: true });
});
test('inits its own repo when path sits under an ignored parent folder', async () => {
const parent = await mkdtemp(join(tmpdir(), 'hbg-parent-git-'));
roots.push(parent);
// Parent project repo with `.data` ignored (same layout as this project).
await Bun.$`git init`.cwd(parent).quiet();
await Bun.$`git config user.email test@example.com`.cwd(parent).quiet();
await Bun.$`git config user.name test`.cwd(parent).quiet();
await writeFile(join(parent, '.gitignore'), '.data\n');
await Bun.$`git add .gitignore`.cwd(parent).quiet();
await Bun.$`git commit -m init`.cwd(parent).quiet();
const workspacePath = join(parent, '.data', 'workspace-git');
await mkdir(workspacePath, { recursive: true });
const stateDir = await mkdtemp(join(tmpdir(), 'hbg-nested-state-'));
roots.push(stateDir);
const config = await loadConfig({
GIT_REPO_PATH: workspacePath,
HOPP_STATE_DIR: stateDir,
WEBAPP_FRONTEND_PATH: join(import.meta.dir, '../fixtures/webapp-frontend'),
});
const author = {
email: 'ada@example.com',
displayName: 'Ada Lovelace',
};
const workspace = createGitWorkspace(config, author);
const collection = createSampleCollection('http://example.com/');
const collectionId = collection.id ?? 'sample-echo';
await expect(workspace.upsertCollection({
...collection,
id: collectionId,
})).resolves.toMatchObject({ id: collectionId });
await access(join(workspacePath, '.git'));
const gitEmail = (await Bun.$`git config --get user.email`
.cwd(workspacePath)
.text()).trim();
const gitName = (await Bun.$`git config --get user.name`
.cwd(workspacePath)
.text()).trim();
expect(gitEmail).toBe(author.email);
expect(gitName).toBe(author.displayName);
const parentTop = (await Bun.$`git rev-parse --show-toplevel`
.cwd(parent)
.text()).trim();
const workspaceTop = (await Bun.$`git rev-parse --show-toplevel`
.cwd(workspacePath)
.text()).trim();
expect(workspaceTop).not.toBe(parentTop);
const tracked = (await Bun.$`git ls-files -- collections/${collectionId}.json`
.cwd(workspacePath)
.text()).trim();
expect(tracked).toBe(`collections/${collectionId}.json`);
});
});