Files
Sanaei f727d04f65
CI / go-test (push) Canceled after 0s
CI / postgres-durable-first (push) Canceled after 0s
CI / codegen (push) Canceled after 0s
CI / govulncheck (push) Canceled after 0s
CI / race (push) Canceled after 0s
CI / fuzz-smoke (push) Canceled after 0s
CI / golangci (push) Canceled after 0s
CI / frontend (push) Canceled after 0s
CodeQL Advanced / Analyze (go) (push) Canceled after 0s
CodeQL Advanced / Analyze (actions) (push) Canceled after 0s
CodeQL Advanced / Analyze (javascript-typescript) (push) Canceled after 0s
Docs CI / build (push) Canceled after 0s
Docs Deploy (GitHub Pages) / build (push) Canceled after 0s
Release 3X-UI / build (386) (push) Canceled after 0s
Release 3X-UI / build (amd64) (push) Canceled after 0s
Release 3X-UI / build (arm64) (push) Canceled after 0s
Release 3X-UI / build (armv5) (push) Canceled after 0s
Release 3X-UI / build (armv6) (push) Canceled after 0s
Release 3X-UI / build (armv7) (push) Canceled after 0s
Release 3X-UI / build (s390x) (push) Canceled after 0s
Release 3X-UI / Build for Windows (amd64) (push) Canceled after 0s
Deploy Smoke Tests / noninteractive-install (ubuntu-24.04-arm) (push) Canceled after 0s
Deploy Smoke Tests / noninteractive-install (ubuntu-latest) (push) Canceled after 0s
Deploy Smoke Tests / release-tag-install (ubuntu-24.04-arm) (push) Canceled after 0s
Deploy Smoke Tests / release-tag-install (ubuntu-latest) (push) Canceled after 0s
Docs Deploy (GitHub Pages) / deploy (push) Canceled after 0s
Release 3X-UI / Publish rolling dev release (push) Canceled after 0s
v3.7.0
2026-08-24 15:07:15 +02:00

82 lines
2.5 KiB
TypeScript

import { describe, it, expect } from 'vitest';
import { normalizeBase, joinUrl, buildCurl, buildFetchSnippet } from './api-client';
describe('normalizeBase', () => {
it('strips a trailing slash', () => {
expect(normalizeBase('https://panel.example.com:2053/')).toBe('https://panel.example.com:2053');
});
it('leaves a clean base unchanged', () => {
expect(normalizeBase('https://panel.example.com:2053')).toBe('https://panel.example.com:2053');
});
});
describe('joinUrl', () => {
it('joins with exactly one slash regardless of input slashes', () => {
expect(joinUrl('https://x.com/', 'panel/api/inbounds/list')).toBe(
'https://x.com/panel/api/inbounds/list',
);
expect(joinUrl('https://x.com', '/panel/api/inbounds/list')).toBe(
'https://x.com/panel/api/inbounds/list',
);
});
});
const base = {
baseUrl: 'https://panel.example.com:2053',
token: 'TKN',
path: '/panel/api/inbounds/list',
};
describe('buildCurl', () => {
it('GET emits the Bearer header, a single-quoted URL, and no body flag', () => {
const cmd = buildCurl({ ...base, method: 'GET' });
expect(cmd).toContain('-X GET');
expect(cmd).toContain("-H 'Authorization: Bearer TKN'");
expect(cmd).toContain("'https://panel.example.com:2053/panel/api/inbounds/list'");
expect(cmd).not.toContain('--data');
expect(cmd).not.toContain('-d ');
});
it('POST with a body emits --data and a JSON content type', () => {
const cmd = buildCurl({
...base,
method: 'POST',
path: '/panel/api/inbounds/add',
body: '{"up":0}',
});
expect(cmd).toContain('-X POST');
expect(cmd).toContain('--data \'{"up":0}\'');
expect(cmd).toContain('Content-Type: application/json');
});
it('POST without a body omits --data', () => {
const cmd = buildCurl({
...base,
method: 'POST',
path: '/panel/api/inbounds/resetAllTraffics',
});
expect(cmd).not.toContain('--data');
});
});
describe('buildFetchSnippet', () => {
it('GET sets method + Authorization and no body', () => {
const snip = buildFetchSnippet({ ...base, method: 'GET' });
expect(snip).toContain("method: 'GET'");
expect(snip).toContain("'Authorization': 'Bearer TKN'");
expect(snip).not.toContain('body:');
});
it('POST with a body includes a JSON.stringify body', () => {
const snip = buildFetchSnippet({
...base,
method: 'POST',
path: '/panel/api/inbounds/add',
body: '{"up":0}',
});
expect(snip).toContain("method: 'POST'");
expect(snip).toContain('body: JSON.stringify(');
});
});