/* eslint-disable @typescript-eslint/no-non-null-assertion */ import type express from 'express'; import * as client from './license.js'; import {expect} from 'chai'; import {suite, test, before, after} from 'mocha'; import {mustSimpleFetch} from './simple-fetch-node.js'; import { LICENSE_STATUS_PATH, GetLicenseStatusRequest, GetLicenseStatusResponse, LicenseStatus, LicenseActiveRequest, LicenseActiveResponse, LICENSE_ACTIVE_PATH, } from '../server/api-types.js'; import {newFakeAppServer} from './test-helpers.js'; import type {FakeAppServer} from './test-helpers.js'; import {consoleLogSink, LogContext} from '@rocicorp/logger'; // These client calls run in Replicache, so use a logger from there. const lc = new LogContext('info', undefined, consoleLogSink); suite('Client: License', () => { suite('licenseActive', () => { let fakeAppServer: FakeAppServer; before(async () => { fakeAppServer = await newFakeAppServer(LICENSE_ACTIVE_PATH); }); after(() => { fakeAppServer.server!.close(); }); type LicenseActiveTestCase = { key: string; browserProfile: string; expReq: LicenseActiveRequest; expError: boolean; status: number; resp: LicenseActiveResponse; }; async function licenseActiveTest(tc: LicenseActiveTestCase) { fakeAppServer.handler = (req: express.Request, res: express.Response) => { expect(req.method).to.equal('POST'); expect(req.header('accept')).to.match(/json/); expect(req.header('content-type')).to.match(/json/); expect(req.originalUrl).to.equal(LICENSE_ACTIVE_PATH); expect(req.body).to.deep.equal( tc.expReq, `expected ${JSON.stringify(tc.expReq)}, got ${JSON.stringify( req.body, )}`, ); res.status(tc.status).json(tc.resp); }; // eslint-disable-next-line @typescript-eslint/no-explicit-any let gotError: any; try { await client.licenseActive( mustSimpleFetch, fakeAppServer.url!, tc.key, tc.browserProfile, lc, ); } catch (e) { gotError = e; } expect(gotError !== undefined).to.equal( tc.expError, `expected error: ${tc.expError}, got ${gotError}`, ); } test('faithfully passes args', async () => { await licenseActiveTest({ key: 'key', browserProfile: 'browserProfile', expReq: { licenseKey: 'key', profileID: 'browserProfile', }, expError: false, status: 200, resp: {}, }); }); test('throws on non-200', async () => { await licenseActiveTest({ key: 'key', browserProfile: 'browserProfile', expReq: { licenseKey: 'key', profileID: 'browserProfile', }, expError: true, status: 500, resp: {}, }); }); }); suite('getLicenseStatus', () => { const goodReq: GetLicenseStatusRequest = { licenseKey: 'abc-123', }; // Test cases type TestCase = { desc: string; key: string; expReq: GetLicenseStatusRequest; status: number; resp: GetLicenseStatusResponse | undefined; expError: RegExp | undefined; }; const tests: TestCase[] = [ { desc: 'good request', key: 'abc-123', expReq: goodReq, status: 200, resp: { status: LicenseStatus.Valid, disable: false, pleaseUpdate: false, }, expError: undefined, }, // TODO more / error cases ]; let fakeAppServer: FakeAppServer; before(async () => { fakeAppServer = await newFakeAppServer(LICENSE_STATUS_PATH); }); after(() => { fakeAppServer.server!.close(); }); tests.forEach(t => { test(t.desc, async () => { fakeAppServer.handler = ( req: express.Request, res: express.Response, ) => { expect(req.method).to.equal('POST'); expect(req.header('accept')).to.match(/json/); expect(req.header('content-type')).to.match(/json/); expect(req.originalUrl).to.equal(LICENSE_STATUS_PATH); expect(req.body).to.deep.equal(t.expReq); res.status(t.status).json(t.resp); }; let got: GetLicenseStatusResponse | undefined; // eslint-disable-next-line @typescript-eslint/no-explicit-any let gotError: any; try { got = await client.getLicenseStatus( mustSimpleFetch, fakeAppServer.url!, t.key, lc, ); } catch (e) { gotError = e; } if (t.expError) { expect(got).undefined; expect(gotError).instanceOf(Error); expect(gotError.message).to.match(t.expError); } else { expect(gotError).undefined; expect(got).to.deep.equal(t.resp); } }); }); }); });