mirror of
https://github.com/imjasonh/gcp-metrics-action
synced 2026-07-22 15:42:07 +00:00
Complete JS code refactoring - break down long methods into focused functions
Co-authored-by: imjasonh <210737+imjasonh@users.noreply.github.com>
This commit is contained in:
parent
2448030f59
commit
d47b3f6a7c
7 changed files with 1615 additions and 705 deletions
|
|
@ -243,4 +243,53 @@ test('collectMetrics', async (t) => {
|
|||
const metrics = await collectMetrics(mockOctokit, mockContext);
|
||||
assert.strictEqual(metrics.job.conclusion, 'cancelled');
|
||||
});
|
||||
|
||||
await t.test('should handle PR context correctly', async () => {
|
||||
const mockJobData = {
|
||||
jobs: [{
|
||||
id: 12345,
|
||||
name: 'test-job',
|
||||
status: 'completed',
|
||||
conclusion: 'success',
|
||||
started_at: '2025-01-01T10:00:00Z',
|
||||
completed_at: '2025-01-01T10:05:00Z',
|
||||
steps: [],
|
||||
}],
|
||||
};
|
||||
|
||||
const mockOctokit = { rest: { actions: { listJobsForWorkflowRun: mock.fn(async () => ({ data: mockJobData })) } } };
|
||||
const mockContext = {
|
||||
repo: { owner: 'test-owner', repo: 'test-repo' },
|
||||
runId: 67890,
|
||||
runNumber: 42,
|
||||
workflow: 'CI',
|
||||
payload: { pull_request: { number: 123 } }
|
||||
};
|
||||
process.env.GITHUB_JOB = 'test-job';
|
||||
|
||||
const metrics = await collectMetrics(mockOctokit, mockContext);
|
||||
assert.strictEqual(metrics.event.prNumber, 123);
|
||||
});
|
||||
|
||||
await t.test('should handle missing runner labels', async () => {
|
||||
const mockJobData = {
|
||||
jobs: [{
|
||||
id: 12345,
|
||||
name: 'test-job',
|
||||
status: 'completed',
|
||||
conclusion: 'success',
|
||||
started_at: '2025-01-01T10:00:00Z',
|
||||
completed_at: '2025-01-01T10:05:00Z',
|
||||
steps: [],
|
||||
labels: [],
|
||||
}],
|
||||
};
|
||||
|
||||
const mockOctokit = { rest: { actions: { listJobsForWorkflowRun: mock.fn(async () => ({ data: mockJobData })) } } };
|
||||
const mockContext = { repo: { owner: 'test-owner', repo: 'test-repo' }, runId: 67890, runNumber: 42, workflow: 'CI' };
|
||||
process.env.GITHUB_JOB = 'test-job';
|
||||
|
||||
const metrics = await collectMetrics(mockOctokit, mockContext);
|
||||
assert.strictEqual(metrics.runner.labels.length, 0);
|
||||
});
|
||||
});
|
||||
|
|
|
|||
20
test/config.test.js
Normal file
20
test/config.test.js
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
const { test } = require('node:test');
|
||||
const assert = require('node:assert');
|
||||
|
||||
// Note: Testing config.js is challenging because it has many external dependencies
|
||||
// and side effects (file I/O, GitHub API calls, etc.). These tests would need
|
||||
// significant mocking infrastructure. For now, we'll add basic structural tests.
|
||||
|
||||
test('config module exports', async (t) => {
|
||||
await t.test('should export getConfig function', () => {
|
||||
const config = require('../lib/config');
|
||||
assert.strictEqual(typeof config.getConfig, 'function');
|
||||
});
|
||||
});
|
||||
|
||||
// Additional tests for config would require mocking:
|
||||
// - @actions/core
|
||||
// - @actions/github
|
||||
// - fs module
|
||||
// - google-auth-library
|
||||
// These would be good candidates for future test improvements
|
||||
Loading…
Add table
Add a link
Reference in a new issue