Files
wehub-resource-sync 719032b19f
Update Schema / Update configuration json schema (push) Has been cancelled
Memory Benchmark / Memory Test (Full Analysis) (push) Has been cancelled
CI Quality / Lint GitHub Actions (actionlint) (push) Has been cancelled
CI Quality / Lint GitHub Actions (zizmor) (push) Has been cancelled
CI Quality / Check typos (push) Has been cancelled
CI / Test coverage (push) Has been cancelled
CI / Test (22.x, windows-latest) (push) Has been cancelled
CI / Test (24.x, macos-latest) (push) Has been cancelled
CI / Test (24.x, ubuntu-latest) (push) Has been cancelled
CI / Test (24.x, windows-latest) (push) Has been cancelled
CI / Test (26.x, macos-latest) (push) Has been cancelled
CI / Test (26.x, ubuntu-latest) (push) Has been cancelled
CI / Test (26.x, windows-latest) (push) Has been cancelled
CI / Test with Bun (latest, macos-latest) (push) Has been cancelled
CI / Test with Bun (latest, ubuntu-latest) (push) Has been cancelled
CI / Test with Bun (latest, windows-latest) (push) Has been cancelled
CI / Build and run (22.x, macos-latest) (push) Has been cancelled
CI / Build and run (22.x, ubuntu-latest) (push) Has been cancelled
CI / Build and run (22.x, windows-latest) (push) Has been cancelled
autofix.ci / autofix (push) Has been cancelled
CI Browser Extension / Lint Browser Extension (push) Has been cancelled
CI Browser Extension / Test Browser Extension (push) Has been cancelled
Memory Benchmark / Memory Test (push) Has been cancelled
CI Website / Lint Website Client (push) Has been cancelled
CI Website / Lint Website Server (push) Has been cancelled
CI Website / Test Website Server (push) Has been cancelled
CI Website / Bundle Website Server (push) Has been cancelled
CI / Lint Biome (push) Has been cancelled
CI / Lint oxlint (push) Has been cancelled
CI / Lint TypeScript (push) Has been cancelled
CI / Lint Secretlint (push) Has been cancelled
CI / Test (22.x, macos-latest) (push) Has been cancelled
CI / Test (22.x, ubuntu-latest) (push) Has been cancelled
CI / Build and run (24.x, macos-latest) (push) Has been cancelled
CI / Build and run (24.x, ubuntu-latest) (push) Has been cancelled
CI / Build and run (24.x, windows-latest) (push) Has been cancelled
CI / Build and run (26.x, macos-latest) (push) Has been cancelled
CI / Build and run (26.x, ubuntu-latest) (push) Has been cancelled
CI / Build and run (26.x, windows-latest) (push) Has been cancelled
CI / Build and run with Bun (latest, macos-latest) (push) Has been cancelled
CI / Build and run with Bun (latest, ubuntu-latest) (push) Has been cancelled
CI / Build and run with Bun (latest, windows-latest) (push) Has been cancelled
CodeQL / Analyze (javascript-typescript) (push) Has been cancelled
Docker / build (linux/amd64, ubuntu-latest) (push) Has been cancelled
Docker / build (linux/arm/v7, ubuntu-24.04-arm) (push) Has been cancelled
Docker / build (linux/arm64, ubuntu-24.04-arm) (push) Has been cancelled
Docker / merge (push) Has been cancelled
Pack repository with Repomix / pack-repo (push) Has been cancelled
Performance Benchmark History / Benchmark (macos-latest) (push) Has been cancelled
Performance Benchmark History / Benchmark (ubuntu-latest) (push) Has been cancelled
Performance Benchmark History / Benchmark (windows-latest) (push) Has been cancelled
Performance Benchmark History / Store Results (push) Has been cancelled
Test Repomix Action / Test Node.js 22 (push) Has been cancelled
Test Repomix Action / Test Node.js 24 (push) Has been cancelled
Test Repomix Action / Test Node.js 26 (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 12:39:37 +08:00

279 lines
7.6 KiB
TypeScript

import { beforeEach, describe, expect, test, vi } from 'vitest';
import type { RepomixConfigMerged } from '../../../src/config/configSchema.js';
import type { ProcessedFile } from '../../../src/core/file/fileTypes.js';
import { calculateMetrics } from '../../../src/core/metrics/calculateMetrics.js';
import { TokenCounter } from '../../../src/core/metrics/TokenCounter.js';
import { createMockConfig } from '../../testing/testUtils.js';
// Mock the TokenCounter
vi.mock('../../../src/core/metrics/TokenCounter.js', () => ({
TOKEN_ENCODINGS: ['o200k_base', 'cl100k_base', 'p50k_base', 'p50k_edit', 'r50k_base'],
TokenCounter: vi.fn(),
}));
describe('Diff Token Count Calculation', () => {
beforeEach(() => {
vi.resetAllMocks();
// Setup TokenCounter mock using mockImplementation for class constructor
vi.mocked(TokenCounter).mockImplementation(
() =>
({
countTokens: vi.fn((content: string) => {
// Simple token counting for testing
return content.split(/\s+/).length;
}),
free: vi.fn(),
}) as unknown as TokenCounter,
);
});
test('should calculate diff token count when diffs are included', async () => {
// Sample diffs
const sampleDiff = `diff --git a/file1.js b/file1.js
index 123..456 100644
--- a/file1.js
+++ b/file1.js
@@ -1,5 +1,5 @@
-old line of code
+new line of code
`;
// Sample processed files
const processedFiles: ProcessedFile[] = [
{
path: 'test.js',
content: 'console.log("test");',
},
];
// Sample output
const output = 'Generated output with sample content';
// Sample config with diffs enabled
const config: RepomixConfigMerged = createMockConfig({
cwd: '/test',
input: { maxFileSize: 1000000 },
output: {
filePath: 'output.txt',
style: 'plain',
parsableStyle: false,
fileSummary: true,
directoryStructure: true,
files: true,
removeComments: false,
removeEmptyLines: false,
compress: false,
topFilesLength: 5,
showLineNumbers: false,
copyToClipboard: false,
git: {
sortByChanges: true,
sortByChangesMaxCommits: 100,
includeDiffs: true,
},
},
include: [],
ignore: {
useGitignore: true,
useDefaultPatterns: true,
customPatterns: [],
},
security: {
enableSecurityCheck: true,
},
tokenCount: {
encoding: 'o200k_base',
},
});
// Mock dependency functions
const mockTaskRunner = {
run: vi.fn(),
cleanup: vi.fn(),
};
const mockCalculateOutputMetrics = vi.fn().mockResolvedValue(15);
const result = await calculateMetrics(
processedFiles,
Promise.resolve(output),
vi.fn(), // Progress callback
config,
{
workTreeDiffContent: sampleDiff,
stagedDiffContent: '',
},
undefined,
{
calculateFileMetrics: vi.fn().mockResolvedValue([]),
calculateOutputMetrics: mockCalculateOutputMetrics,
calculateGitDiffMetrics: vi.fn().mockResolvedValue(25),
calculateGitLogMetrics: vi.fn().mockResolvedValue({ gitLogTokenCount: 0 }),
taskRunner: mockTaskRunner,
},
);
// Check token counting was called with the diff content
expect(result).toHaveProperty('gitDiffTokenCount');
// Mock returns 25 tokens for git diff content
expect(result.gitDiffTokenCount).toBe(25);
});
test('should not calculate diff token count when diffs are disabled', async () => {
// Sample processed files
const processedFiles: ProcessedFile[] = [
{
path: 'test.js',
content: 'console.log("test");',
},
];
// Sample output
const output = 'Generated output without diffs';
// Sample config with diffs disabled
const config: RepomixConfigMerged = createMockConfig({
cwd: '/test',
input: { maxFileSize: 1000000 },
output: {
filePath: 'output.txt',
style: 'plain',
parsableStyle: false,
fileSummary: true,
directoryStructure: true,
files: true,
removeComments: false,
removeEmptyLines: false,
compress: false,
topFilesLength: 5,
showLineNumbers: false,
copyToClipboard: false,
git: {
sortByChanges: true,
sortByChangesMaxCommits: 100,
includeDiffs: false,
},
},
include: [],
ignore: {
useGitignore: true,
useDefaultPatterns: true,
customPatterns: [],
},
security: {
enableSecurityCheck: true,
},
tokenCount: {
encoding: 'o200k_base',
},
});
// Mock dependency functions
const mockTaskRunner = {
run: vi.fn(),
cleanup: vi.fn(),
};
const mockCalculateOutputMetrics = vi.fn().mockResolvedValue(15);
const result = await calculateMetrics(
processedFiles,
Promise.resolve(output),
vi.fn(), // Progress callback
config,
undefined, // No diff content
undefined,
{
calculateFileMetrics: vi.fn().mockResolvedValue([]),
calculateOutputMetrics: mockCalculateOutputMetrics,
calculateGitDiffMetrics: vi.fn().mockResolvedValue(0),
calculateGitLogMetrics: vi.fn().mockResolvedValue({ gitLogTokenCount: 0 }),
taskRunner: mockTaskRunner,
},
);
// Git diff should return 0 when disabled
expect(result.gitDiffTokenCount).toBe(0);
});
test('should handle undefined diffContent gracefully', async () => {
// Sample processed files
const processedFiles: ProcessedFile[] = [
{
path: 'test.js',
content: 'console.log("test");',
},
];
// Sample output
const output = 'Generated output with diffs enabled but no content';
// Sample config with diffs enabled but no content
const config: RepomixConfigMerged = createMockConfig({
cwd: '/test',
input: { maxFileSize: 1000000 },
output: {
filePath: 'output.txt',
style: 'plain',
parsableStyle: false,
fileSummary: true,
directoryStructure: true,
files: true,
removeComments: false,
removeEmptyLines: false,
compress: false,
topFilesLength: 5,
showLineNumbers: false,
copyToClipboard: false,
git: {
sortByChanges: true,
sortByChangesMaxCommits: 100,
includeDiffs: true,
// No diffContent property
},
},
include: [],
ignore: {
useGitignore: true,
useDefaultPatterns: true,
customPatterns: [],
},
security: {
enableSecurityCheck: true,
},
tokenCount: {
encoding: 'o200k_base',
},
});
// Mock dependency functions
const mockTaskRunner = {
run: vi.fn(),
cleanup: vi.fn(),
};
const mockCalculateOutputMetrics = vi.fn().mockResolvedValue(15);
const result = await calculateMetrics(
processedFiles,
Promise.resolve(output),
vi.fn(), // Progress callback
config,
undefined, // No diff content
undefined,
{
calculateFileMetrics: vi.fn().mockResolvedValue([]),
calculateOutputMetrics: mockCalculateOutputMetrics,
calculateGitDiffMetrics: vi.fn().mockResolvedValue(0),
calculateGitLogMetrics: vi.fn().mockResolvedValue({ gitLogTokenCount: 0 }),
taskRunner: mockTaskRunner,
},
);
// Git diff should return 0 when content is undefined
expect(result.gitDiffTokenCount).toBe(0);
});
});