Files
RSSHub/lib/container.test.ts
Tony 6697901e29 style: use cloudflare/oxlint-config (#23968)
* style: use cloudflare/oxlint-config

* style: fix no-known-value-widening

* style: fix typescript/only-throw-error

* style: fix typescript/no-floating-promises

* style: fix typescript/use-unknown-in-catch-callback-variable

* style: fix typescript/no-misused-promises

* style: fix typescript/no-unsafe-unary-minus

* style: fix typescript/no-for-in-array

* style: fix react/purity

* style: fix typescript/switch-exhaustiveness-check

* style: fix typescript/unbound-method

* style: fix react/rules-of-hooks

* style: fix typescript/return-await

* style: fix react/button-has-type

* style: fix react/no-unescaped-entities

* style: fix import/no-namespace

* style: fix typescript/no-import-type-side-effects

* style: fix no-promise-executor-return

* style: fix no-useless-assignment

* style: fix typescript/no-misused-spread

* style: fix react/jsx-no-target-blank

* style: fix typescript/await-thenable

* style: fix typescript/no-unnecessary-type-assertion

* style: fix import/consistent-type-specifier-style

* style: fix react/no-unknown-property

* style: remove unused disable directive

* style: merge region

* style: enable prefer-smaller-scope

* style: add react/no-danger

* style: add unicorn-js/prefer-uint8array-base64/hex

* style: move non-recommended unicorn rules to custom region
2026-11-21 03:38:53 +08:00

62 lines
2.7 KiB
TypeScript

import { describe, expect, it, vi } from 'vitest';
const containerFetch = vi.fn((_request: Request, _port?: number) => Promise.resolve(new Response('ok')));
// Stands in for the real class: like it, startAndWaitForPorts() leaves a running container alone
vi.mock('@cloudflare/containers', () => ({
Container: class {
containerFetch = containerFetch;
envVars?: Record<string, string>;
runningWith?: Record<string, string>;
destroy() {
this.runningWith = undefined;
return Promise.resolve();
}
startAndWaitForPorts() {
this.runningWith ??= this.envVars;
return Promise.resolve();
}
},
}));
describe('container worker', () => {
it('replaces a running container when the config in KV changes', async () => {
const { RSSHubContainer } = await import('@/container');
const container = new RSSHubContainer({}, {}) as InstanceType<typeof RSSHubContainer> & { runningWith?: Record<string, string> };
const destroy = vi.spyOn(container, 'destroy');
await container.startWithEnvVars({ NODE_ENV: 'production', CACHE_EXPIRE: '300' });
expect(container.runningWith).toEqual({ NODE_ENV: 'production', CACHE_EXPIRE: '300' });
destroy.mockClear();
// unchanged (order does not matter)
await container.startWithEnvVars({ CACHE_EXPIRE: '300', NODE_ENV: 'production' });
expect(destroy).not.toHaveBeenCalled();
// added
await container.startWithEnvVars({ NODE_ENV: 'production', CACHE_EXPIRE: '300', ACCESS_KEY: 'old' });
expect(destroy).toHaveBeenCalledTimes(1);
expect(container.runningWith).toEqual({ NODE_ENV: 'production', CACHE_EXPIRE: '300', ACCESS_KEY: 'old' });
// replaced
await container.startWithEnvVars({ NODE_ENV: 'production', CACHE_EXPIRE: '300', ACCESS_KEY: 'new' });
expect(destroy).toHaveBeenCalledTimes(2);
expect(container.runningWith).toEqual({ NODE_ENV: 'production', CACHE_EXPIRE: '300', ACCESS_KEY: 'new' });
// removed
await container.startWithEnvVars({ NODE_ENV: 'production', ACCESS_KEY: 'new' });
expect(destroy).toHaveBeenCalledTimes(3);
expect(container.runningWith).toEqual({ NODE_ENV: 'production', ACCESS_KEY: 'new' });
});
it('does not let the client choose the container port', async () => {
const { RSSHubContainer } = await import('@/container');
const container = new RSSHubContainer({}, {});
const request = new Request('https://rsshub.test/metrics', { headers: { 'cf-container-target-port': '9464' } });
await container.fetch(request);
expect(containerFetch).toHaveBeenCalledExactlyOnceWith(request, 1200);
});
});