Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .github/workflows/ci_node16.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,9 @@ jobs:
- name: NPM install
run: |
npm install
- name: typecheck
run: |
npm run typecheck
- name: run base ci ut and it
run: |
npm run test
Expand Down
83 changes: 0 additions & 83 deletions __tests__/ut/core/downloadFile_test.ts

This file was deleted.

5 changes: 2 additions & 3 deletions __tests__/ut/local/impl/baseLocal_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import _ from 'lodash';
import path from 'path';
import * as fs from 'fs-extra';
import { v4 as uuidV4 } from 'uuid';
import tmpDir from 'temp-dir';
import { getTempDir } from '../../../../src/utils';

// Mock external dependencies
jest.mock('../../../../src/logger', () => ({
Expand All @@ -21,7 +21,6 @@ jest.mock('lodash');
jest.mock('path');
jest.mock('fs-extra');
jest.mock('uuid');
jest.mock('temp-dir');

describe('BaseLocal', () => {
let mockInputs: IInputs;
Expand All @@ -34,7 +33,7 @@ describe('BaseLocal', () => {
(uuidV4 as jest.Mock).mockReturnValue('test-uuid');

// Mock tempDir
(tmpDir as any) = '/tmp';
(getTempDir as jest.Mock).mockReturnValue('/tmp');

// Mock inputs
mockInputs = {
Expand Down
67 changes: 1 addition & 66 deletions __tests__/ut/local/impl/utils_test.ts
Original file line number Diff line number Diff line change
@@ -1,44 +1,10 @@
import {
getTimeZone,
formatJsonString,
downloadFile,
} from '../../../../src/subCommands/local/impl/utils';
import logger from '../../../../src/logger';
import http from 'http';
import fs from 'fs';
import { promisify } from 'util';

// Mock external dependencies
jest.mock('../../../../src/logger', () => ({
debug: jest.fn(),
info: jest.fn(),
warn: jest.fn(),
error: jest.fn(),
}));
jest.mock('http');
jest.mock('fs');
jest.mock('util');
import { formatJsonString } from '../../../../src/subCommands/local/impl/utils';

describe('utils', () => {
beforeEach(() => {
jest.clearAllMocks();
});

describe('getTimeZone', () => {
it('should return correct timezone string', () => {
// Mock Date.getTimezoneOffset to return a fixed value
const originalGetTimezoneOffset = Date.prototype.getTimezoneOffset;
Date.prototype.getTimezoneOffset = jest.fn(() => -480); // UTC+8

const result = getTimeZone();

expect(result).toBe('UTC+8');

// Restore original function
Date.prototype.getTimezoneOffset = originalGetTimezoneOffset;
});
});

describe('formatJsonString', () => {
it('should format valid JSON string', () => {
const input = '{"key": "value", "number": 42}';
Expand All @@ -65,35 +31,4 @@ describe('utils', () => {
expect(result).toBe(input);
});
});

describe('downloadFile', () => {
it('should download file successfully', async () => {
const mockFile = {};
const mockResponse = {};
const mockPipeline = jest.fn().mockResolvedValue(undefined);

(fs.createWriteStream as jest.Mock).mockReturnValue(mockFile);
(promisify as unknown as jest.Mock).mockReturnValue(mockPipeline);
(http.get as jest.Mock).mockResolvedValue(mockResponse);

await downloadFile('http://example.com/file.txt', '/tmp/file.txt');

expect(fs.createWriteStream).toHaveBeenCalledWith('/tmp/file.txt');
expect(promisify).toHaveBeenCalled();
expect(http.get).toHaveBeenCalledWith('http://example.com/file.txt');
expect(mockPipeline).toHaveBeenCalledWith(mockResponse, mockFile);
expect(logger.info).toHaveBeenCalledWith(
'http://example.com/file.txt ==> /tmp/file.txt has been downloaded.',
);
});

it('should handle download error', async () => {
const mockError = new Error('Network error');
(http.get as jest.Mock).mockRejectedValue(mockError);

await downloadFile('http://example.com/file.txt', '/tmp/file.txt');

expect(logger.error).toHaveBeenCalledWith(`Error downloading file: ${mockError}`);
});
});
});
9 changes: 1 addition & 8 deletions __tests__/ut/utils/utils_functions_test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { isAuto, isAutoVpcConfig, getTimeZone, sleep } from '../../../src/utils/index';
import { isAuto, isAutoVpcConfig, sleep } from '../../../src/utils/index';
import { computeLocalAuto } from '../../../src/resources/fc/impl/utils';
import log from '../../../src/logger';
log._set(console);
Expand Down Expand Up @@ -147,13 +147,6 @@ describe('Utils functions', () => {
});
});

describe('getTimeZone', () => {
it('should return a valid timezone string', () => {
const tz = getTimeZone();
expect(tz).toMatch(/^UTC[+-]\d+$/);
});
});

describe('sleep', () => {
it('should resolve after specified time', async () => {
const start = Date.now();
Expand Down
7 changes: 0 additions & 7 deletions __tests__/ut/utils/utils_test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import {
isAuto,
getTimeZone,
isAutoVpcConfig,
removeNullValues,
getFileSize,
Expand Down Expand Up @@ -97,12 +96,6 @@ describe('isAutoVpcConfig', () => {
});
});

test('getTimeZone', () => {
const t = getTimeZone();
console.log(t);
expect(t === 'UTC+0' || t === 'UTC+8').toBe(true);
});

describe('removeNullValues', () => {
// 测试用例1:空对象
it('should not modify an empty object', () => {
Expand Down
3 changes: 2 additions & 1 deletion docs/architecture.md
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,8 @@ src/
**核心工具函数**:

- `isAuto()` - 检查是否为自动配置
- `getTimeZone()` - 获取时区
- `getTempDir()` - 获取临时目录(realpath 解析,供 Docker 挂载)
- `getLocalIpAddress()` - 获取本机首个非 loopback IPv4
- `removeNullValues()` - 移除空值
- `getFileSize()` - 获取文件大小
- `promptForConfirmOrDetails()` - 用户确认提示
Expand Down
2 changes: 0 additions & 2 deletions docs/testing-plan.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@

5. **其他工具模块**
- `transformCustomDomainProps_test.ts` - 自定义域名转换测试
- `downloadFile_test.ts` - 文件下载测试
- `crc64_test.ts` - CRC64 测试

## 需要补充测试的模块
Expand Down Expand Up @@ -128,7 +127,6 @@
- `instance/index.ts` 测试
- `version/index.ts` 测试
- `model/index.ts` 测试
- `trigger-template/index.ts` 测试

### 3. 资源管理模块测试

Expand Down
32 changes: 12 additions & 20 deletions jestconfig.json
Original file line number Diff line number Diff line change
@@ -1,21 +1,13 @@
{
"transformIgnorePatterns": [
"/example/*.*"
],
"transform": {
"^.+\\.(ts|tsx)$": "ts-jest"
},
"testRegex": "(/__tests__/(ut|it)/.*|(\\.|/)(test|spec))\\.(jsx?|tsx?)$",
"moduleFileExtensions": [
"ts",
"tsx",
"js",
"jsx",
"json",
"node"
],
"collectCoverage": true,
"coverageReporters": ["html", "text"],
"testTimeout": 30000
}

"transformIgnorePatterns": ["/example/*.*"],
"transform": {
"^.+\\.(ts|tsx)$": ["ts-jest", { "diagnostics": { "exclude": ["**/node_modules/**"] } }]
},
"testRegex": "(/__tests__/(ut|it)/.*|(\\.|/)(test|spec))\\.(jsx?|tsx?)$",
"moduleFileExtensions": ["ts", "tsx", "js", "jsx", "json", "node"],
"roots": ["<rootDir>/__tests__", "<rootDir>/src"],
"testPathIgnorePatterns": ["/node_modules/", "/__tests__/e2e/"],
"collectCoverage": true,
"coverageReporters": ["html", "text"],
"testTimeout": 30000
}
Loading
Loading