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

225 lines
7.1 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 type { GitDiffResult } from '../../../src/core/git/gitDiffHandle.js';
import * as gitDiffModule from '../../../src/core/git/gitDiffHandle.js';
import * as gitRepositoryModule from '../../../src/core/git/gitRepositoryHandle.js';
import { buildOutputGeneratorContext, generateOutput } from '../../../src/core/output/outputGenerate.js';
import type { RenderContext } from '../../../src/core/output/outputGeneratorTypes.js';
import { createMockConfig } from '../../testing/testUtils.js';
// Mock the git modules
vi.mock('../../../src/core/git/gitDiffHandle.js', () => ({
getWorkTreeDiff: vi.fn(),
}));
vi.mock('../../../src/core/git/gitRepositoryHandle.js', () => ({
isGitRepository: vi.fn(),
}));
describe('Git Diffs in Output', () => {
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
+new line`;
let mockConfig: RepomixConfigMerged;
beforeEach(() => {
vi.resetAllMocks();
// Mock the git command
vi.mocked(gitDiffModule.getWorkTreeDiff).mockResolvedValue(sampleDiff);
vi.mocked(gitRepositoryModule.isGitRepository).mockResolvedValue(true);
// Sample minimal config using createMockConfig utility
mockConfig = createMockConfig({
cwd: '/test/repo',
input: {
maxFileSize: 1000000,
},
output: {
filePath: 'output.txt',
style: 'plain',
git: {
includeDiffs: false,
},
},
});
});
test('buildOutputGeneratorContext should include diffs when enabled', async () => {
// Enable diffs
if (mockConfig.output.git) {
mockConfig.output.git.includeDiffs = true;
}
const rootDirs = ['/test/repo'];
const allFilePaths = ['file1.js', 'file2.js'];
const processedFiles: ProcessedFile[] = [
{
path: 'file1.js',
content: 'console.log("file1");',
},
{
path: 'file2.js',
content: 'console.log("file2");',
},
];
const sampleDiff = 'diff --git a/file1.js b/file1.js';
const context = await buildOutputGeneratorContext(rootDirs, mockConfig, allFilePaths, processedFiles, {
workTreeDiffContent: sampleDiff,
stagedDiffContent: '',
});
// Context should include gitDiffs
expect(context.gitDiffResult?.workTreeDiffContent).toBe(sampleDiff);
});
test('generateOutput should include diffs in XML output via object', async () => {
// Enable diffs
mockConfig.output.style = 'xml';
if (mockConfig.output.git) {
mockConfig.output.git.includeDiffs = true;
}
const rootDirs = ['/test/repo'];
const processedFiles: ProcessedFile[] = [
{
path: 'file1.js',
content: 'console.log("file1");',
},
];
// Create a modified generateOutput with mocked deps
const mockBuildOutputGeneratorContext = vi.fn().mockImplementation(async () => {
return {
generationDate: '2025-05-06T00:00:00.000Z',
treeString: '.\n└── file1.js',
processedFiles,
config: mockConfig,
instruction: '',
gitDiffs: sampleDiff,
};
});
const mockGenerateHandlebarOutput = vi.fn().mockResolvedValue('<xml>output with diffs</xml>');
const mockGenerateParsableXmlOutput = vi.fn().mockImplementation(async (renderContext: RenderContext) => {
// Check that renderContext has gitDiffs
expect(renderContext.gitDiffWorkTree).toBe(sampleDiff);
return '<xml>parsable output with diffs object</xml>';
});
const mockSortOutputFiles = vi.fn().mockImplementation((files) => Promise.resolve(files));
const gitDiffResult: GitDiffResult = {
workTreeDiffContent: sampleDiff,
stagedDiffContent: '',
};
// Call generateOutput with mocked deps
const _output = await generateOutput(
rootDirs,
mockConfig,
processedFiles,
['file1.js'],
gitDiffResult,
undefined,
undefined,
undefined,
{
buildOutputGeneratorContext: mockBuildOutputGeneratorContext,
generateHandlebarOutput: mockGenerateHandlebarOutput,
generateParsableXmlOutput: mockGenerateParsableXmlOutput,
generateParsableJsonOutput: vi.fn(),
sortOutputFiles: mockSortOutputFiles,
},
);
// Check that the output was generated with the correct template
expect(mockBuildOutputGeneratorContext).toHaveBeenCalled();
// For non-parsable XML, should use Handlebars
if (!mockConfig.output.parsableStyle) {
expect(mockGenerateHandlebarOutput).toHaveBeenCalled();
} else {
// For parsable XML, should use XML generator
expect(mockGenerateParsableXmlOutput).toHaveBeenCalled();
}
});
test('generateOutput should include diffs in Markdown output', async () => {
// Enable diffs with markdown style
mockConfig.output.style = 'markdown';
if (mockConfig.output.git) {
mockConfig.output.git.includeDiffs = true;
}
const rootDirs = ['/test/repo'];
const processedFiles: ProcessedFile[] = [
{
path: 'file1.js',
content: 'console.log("file1");',
},
];
// Create a modified generateOutput with mocked deps
const mockBuildOutputGeneratorContext = vi.fn().mockImplementation(async () => {
return {
generationDate: '2025-05-06T00:00:00.000Z',
treeString: '.\n└── file1.js',
processedFiles,
config: mockConfig,
instruction: '',
gitDiffResult: {
workTreeDiffContent: sampleDiff,
stagedDiffContent: '',
},
};
});
const mockGenerateHandlebarOutput = vi.fn().mockImplementation(async (_config, renderContext: RenderContext) => {
// Check that renderContext has gitDiffs for markdown template
expect(renderContext.gitDiffWorkTree).toBe(sampleDiff);
return `# Markdown output with diffs\n\`\`\`diff\n${sampleDiff}\n\`\`\``;
});
const mockGenerateParsableXmlOutput = vi.fn();
const mockSortOutputFiles = vi.fn().mockImplementation((files) => Promise.resolve(files));
const gitDiffResult: GitDiffResult = {
workTreeDiffContent: sampleDiff,
stagedDiffContent: '',
};
// Call generateOutput with mocked deps
const _output = await generateOutput(
rootDirs,
mockConfig,
processedFiles,
['file1.js'],
gitDiffResult,
undefined,
undefined,
undefined,
{
buildOutputGeneratorContext: mockBuildOutputGeneratorContext,
generateHandlebarOutput: mockGenerateHandlebarOutput,
generateParsableXmlOutput: mockGenerateParsableXmlOutput,
generateParsableJsonOutput: vi.fn(),
sortOutputFiles: mockSortOutputFiles,
},
);
// For markdown output, should use Handlebars
expect(mockGenerateHandlebarOutput).toHaveBeenCalled();
// XML generator should not be called for markdown
expect(mockGenerateParsableXmlOutput).not.toHaveBeenCalled();
});
});