T-23: tests
This commit is contained in:
parent
f3fc80daa5
commit
f712c3619b
1 changed files with 242 additions and 0 deletions
242
test/unit/benchmark-identity-edge-cases.test.ts
Normal file
242
test/unit/benchmark-identity-edge-cases.test.ts
Normal file
|
|
@ -0,0 +1,242 @@
|
|||
import { createHash } from 'node:crypto';
|
||||
import {
|
||||
chmodSync,
|
||||
mkdirSync,
|
||||
mkdtempSync,
|
||||
readFileSync,
|
||||
rmSync,
|
||||
symlinkSync,
|
||||
writeFileSync,
|
||||
} from 'node:fs';
|
||||
import { tmpdir } from 'node:os';
|
||||
import { join } from 'node:path';
|
||||
import { describe, expect, test } from 'bun:test';
|
||||
import { identity } from '../../scripts/benchmark-research';
|
||||
|
||||
// Guards T-23: benchmark provenance must tolerate unreadable *untracked* files (sandbox
|
||||
// placeholder dotfiles) without masking read failures on tracked sources, and must keep
|
||||
// readable/deleted digests identical to the pre-fix algorithm.
|
||||
|
||||
const runningAsRoot = process.getuid?.() === 0;
|
||||
|
||||
function git(cwd: string, args: string[]): string {
|
||||
const result = Bun.spawnSync(
|
||||
['git', '-c', 'user.name=QA', '-c', 'user.email=qa@example.com', ...args],
|
||||
{
|
||||
cwd,
|
||||
stdout: 'pipe',
|
||||
stderr: 'pipe',
|
||||
env: { ...process.env, GIT_CONFIG_NOSYSTEM: '1', GIT_CONFIG_GLOBAL: '/dev/null' },
|
||||
}
|
||||
);
|
||||
if (result.exitCode !== 0) {
|
||||
throw new Error(`git ${args.join(' ')} failed: ${result.stderr.toString()}`);
|
||||
}
|
||||
return result.stdout.toString().trim();
|
||||
}
|
||||
|
||||
function sha256(data: Uint8Array | string): string {
|
||||
return createHash('sha256').update(data).digest('hex');
|
||||
}
|
||||
|
||||
// Mirrors the digest identity() builds: sorted paths, each hashed as JSON [path, entry].
|
||||
function expectedDigest(entries: Array<[string, string]>): string {
|
||||
const hash = createHash('sha256');
|
||||
for (const pair of [...entries].sort(([a], [b]) => (a < b ? -1 : a > b ? 1 : 0))) {
|
||||
hash.update(JSON.stringify(pair));
|
||||
}
|
||||
return hash.digest('hex');
|
||||
}
|
||||
|
||||
function withRepo(
|
||||
files: Record<string, string>,
|
||||
fn: (repo: string, restoreLater: (path: string, mode: number) => void) => void
|
||||
): void {
|
||||
const repo = mkdtempSync(join(tmpdir(), 'stock-bot-identity-edge-'));
|
||||
const restores: Array<[string, number]> = [];
|
||||
try {
|
||||
git(repo, ['init', '-q']);
|
||||
const excludes = join(repo, '.git', 'info', 'exclude');
|
||||
writeFileSync(excludes, '');
|
||||
git(repo, ['config', 'core.excludesFile', excludes]);
|
||||
for (const [path, content] of Object.entries(files)) {
|
||||
mkdirSync(join(repo, path, '..'), { recursive: true });
|
||||
writeFileSync(join(repo, path), content);
|
||||
}
|
||||
git(repo, ['add', '-A']);
|
||||
git(repo, ['commit', '-qm', 'fixture']);
|
||||
fn(repo, (path, mode) => restores.push([path, mode]));
|
||||
} finally {
|
||||
for (const [path, mode] of restores.reverse()) {
|
||||
try {
|
||||
chmodSync(path, mode);
|
||||
} catch {
|
||||
// Already removed by the test.
|
||||
}
|
||||
}
|
||||
rmSync(repo, { recursive: true, force: true });
|
||||
}
|
||||
}
|
||||
|
||||
describe('benchmark identity() edge cases (T-23)', () => {
|
||||
test('readable-only repo digest is byte-for-byte the pre-fix algorithm, including awkward names', () => {
|
||||
const files = { 'name with space.txt': 'a\n', 'ünïcode.txt': 'b\n', 'dir/nested.txt': 'c\n' };
|
||||
withRepo(files, repo => {
|
||||
writeFileSync(join(repo, 'untracked.txt'), 'u\n');
|
||||
|
||||
const result = identity({ sourceRoot: repo });
|
||||
|
||||
expect(result.source.contentSha256).toBe(
|
||||
expectedDigest([
|
||||
...Object.entries(files).map(([p, c]): [string, string] => [p, sha256(c)]),
|
||||
['untracked.txt', sha256('u\n')],
|
||||
])
|
||||
);
|
||||
expect(result.source.files).toBe(4);
|
||||
});
|
||||
});
|
||||
|
||||
test('sourceRoot drives every git query, so commit and dirty describe the temp repo', () => {
|
||||
withRepo({ 'a.txt': 'a\n' }, repo => {
|
||||
const head = git(repo, ['rev-parse', 'HEAD']);
|
||||
|
||||
const clean = identity({ sourceRoot: repo });
|
||||
writeFileSync(join(repo, 'a.txt'), 'changed\n');
|
||||
const dirty = identity({ sourceRoot: repo });
|
||||
|
||||
expect(clean.source.commit).toBe(head);
|
||||
expect(clean.source.dirty).toBe(false);
|
||||
expect(dirty.source.commit).toBe(head);
|
||||
expect(dirty.source.dirty).toBe(true);
|
||||
});
|
||||
});
|
||||
|
||||
test('dangling untracked symlink still hashes as deleted, as before the fix', () => {
|
||||
withRepo({ 'a.txt': 'a\n' }, repo => {
|
||||
symlinkSync('missing-target', join(repo, 'dangling'));
|
||||
|
||||
const result = identity({ sourceRoot: repo });
|
||||
|
||||
expect(result.source.contentSha256).toBe(
|
||||
expectedDigest([
|
||||
['a.txt', sha256('a\n')],
|
||||
['dangling', 'deleted'],
|
||||
])
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
test.skipIf(runningAsRoot)(
|
||||
'untracked symlink to an unreadable target hashes as unreadable instead of throwing',
|
||||
() => {
|
||||
withRepo({ 'a.txt': 'a\n' }, (repo, restoreLater) => {
|
||||
const secret = join(repo, 'secret');
|
||||
writeFileSync(secret, 's\n');
|
||||
chmodSync(secret, 0o000);
|
||||
restoreLater(secret, 0o600);
|
||||
symlinkSync('secret', join(repo, 'link'));
|
||||
|
||||
const first = identity({ sourceRoot: repo });
|
||||
const second = identity({ sourceRoot: repo });
|
||||
|
||||
const expected = expectedDigest([
|
||||
['a.txt', sha256('a\n')],
|
||||
['link', 'unreadable'],
|
||||
['secret', 'unreadable'],
|
||||
]);
|
||||
expect(first.source.contentSha256).toBe(expected);
|
||||
expect(second.source.contentSha256).toBe(expected);
|
||||
});
|
||||
}
|
||||
);
|
||||
|
||||
test.skipIf(runningAsRoot)(
|
||||
'unreadable untracked directory does not break identity (git cannot list its contents)',
|
||||
() => {
|
||||
withRepo({ 'a.txt': 'a\n' }, (repo, restoreLater) => {
|
||||
const dir = join(repo, 'locked');
|
||||
mkdirSync(dir);
|
||||
writeFileSync(join(dir, 'inside.txt'), 'x\n');
|
||||
chmodSync(dir, 0o000);
|
||||
restoreLater(dir, 0o755);
|
||||
|
||||
const result = identity({ sourceRoot: repo });
|
||||
|
||||
expect(result.source.contentSha256).toBe(expectedDigest([['a.txt', sha256('a\n')]]));
|
||||
expect(result.source.files).toBe(1);
|
||||
});
|
||||
}
|
||||
);
|
||||
|
||||
test.skipIf(runningAsRoot)(
|
||||
'tracked file under a non-searchable directory throws EACCES instead of hashing as deleted',
|
||||
() => {
|
||||
withRepo({ 'a.txt': 'a\n', 'src/core.ts': 'export {};\n' }, (repo, restoreLater) => {
|
||||
const dir = join(repo, 'src');
|
||||
chmodSync(dir, 0o600);
|
||||
restoreLater(dir, 0o755);
|
||||
|
||||
expect(() => identity({ sourceRoot: repo })).toThrow(/EACCES.*src\/core\.ts/);
|
||||
});
|
||||
}
|
||||
);
|
||||
|
||||
test.skipIf(runningAsRoot)(
|
||||
'file removed from the index with git rm --cached is treated as untracked and tolerated when unreadable',
|
||||
() => {
|
||||
withRepo({ 'a.txt': 'a\n', 'was-tracked.txt': 'w\n' }, (repo, restoreLater) => {
|
||||
const path = join(repo, 'was-tracked.txt');
|
||||
git(repo, ['rm', '-q', '--cached', 'was-tracked.txt']);
|
||||
chmodSync(path, 0o000);
|
||||
restoreLater(path, 0o600);
|
||||
|
||||
const result = identity({ sourceRoot: repo });
|
||||
|
||||
expect(result.source.contentSha256).toBe(
|
||||
expectedDigest([
|
||||
['a.txt', sha256('a\n')],
|
||||
['was-tracked.txt', 'unreadable'],
|
||||
])
|
||||
);
|
||||
});
|
||||
}
|
||||
);
|
||||
|
||||
test('staged-but-uncommitted file counts as tracked, so a permission error on it still throws', () => {
|
||||
withRepo({ 'a.txt': 'a\n' }, repo => {
|
||||
const staged = join(repo, 'staged.txt');
|
||||
writeFileSync(staged, 's\n');
|
||||
git(repo, ['add', 'staged.txt']);
|
||||
|
||||
const readSourceFile = (path: string): Uint8Array => {
|
||||
if (path === staged) {
|
||||
throw Object.assign(new Error(`EACCES: ${path}`), { code: 'EACCES' });
|
||||
}
|
||||
return readFileSync(path);
|
||||
};
|
||||
|
||||
expect(() => identity({ sourceRoot: repo, readSourceFile })).toThrow('EACCES');
|
||||
});
|
||||
});
|
||||
|
||||
test('errors without a string code are rethrown, never mapped to a marker', () => {
|
||||
withRepo({ 'a.txt': 'a\n' }, repo => {
|
||||
writeFileSync(join(repo, 'untracked.txt'), 'u\n');
|
||||
const target = join(repo, 'untracked.txt');
|
||||
|
||||
for (const thrown of [
|
||||
new Error('plain failure'),
|
||||
Object.assign(new Error('numeric'), { code: 13 }),
|
||||
]) {
|
||||
const readSourceFile = (path: string): Uint8Array => {
|
||||
if (path === target) {
|
||||
throw thrown;
|
||||
}
|
||||
return readFileSync(path);
|
||||
};
|
||||
|
||||
expect(() => identity({ sourceRoot: repo, readSourceFile })).toThrow(thrown.message);
|
||||
}
|
||||
});
|
||||
});
|
||||
});
|
||||
Loading…
Add table
Add a link
Reference in a new issue