import { jest } from '@jest/globals'; import { HttpClient } from '../dist/index.js'; import * as fetcher from '@cloudpss/fetch'; test('e2e', () => { expect(new HttpClient().config).toEqual({ token: undefined, altTokens: [], apiUrl: 'https://cloudpss.net/api', apiTunnel: false, gqlUrl: 'https://cloudpss.net/graphql', fetcher: fetcher, webSocket: fetcher.WebSocket, dev: false, }); expect( new HttpClient({ apiUrl: '/xx', gqlUrl: 'http://example.com/graphql', // @ts-expect-error alterTokens 不能包含 null alterTokens: ['token1.x.y', 'token2.x.y', '', null], }).config, ).toEqual({ token: undefined, altTokens: ['token1.x.y', 'token2.x.y'], apiUrl: 'https://cloudpss.net/xx', apiTunnel: false, gqlUrl: 'http://example.com/graphql', fetcher: fetcher, webSocket: fetcher.WebSocket, dev: false, }); const fakeFetcher = {}; const fakeWs = jest.fn(); expect( new HttpClient({ apiUrl: '/xx', gqlUrl: 'http://example.com/graphql', token: 'xx.yy.zz', // @ts-expect-error alterTokens 不能包含 null altTokens: ['token1.x.y', 'token2.x.y', '', null], // @ts-expect-error apiTunnel apiTunnel: '1', // @ts-expect-error fetcher 需要为特定签名的函数 fetcher: fakeFetcher, // @ts-expect-error webSocket 需要为特定签名的函数 webSocket: fakeWs, // @ts-expect-error dev 需要为 boolean dev: 1, }).config, ).toEqual({ token: 'xx.yy.zz', altTokens: ['token1.x.y', 'token2.x.y'], apiUrl: 'https://cloudpss.net/xx', apiTunnel: true, gqlUrl: 'http://example.com/graphql', fetcher: fakeFetcher, webSocket: fakeWs, dev: true, }); expect(() => new HttpClient({ token: 'xxx' })).toThrow(/Invalid JWT token/); // @ts-expect-error token 需要为 string expect(() => new HttpClient({ token: 1 })).toThrow(/token must be a string/); // @ts-expect-error token 需要为 string expect(() => new HttpClient({ token: false })).toThrow(/token must be a string/); });