Optimize gateway comms reload behavior and strengthen regression coverage (#496)

This commit is contained in:
Lingxuan Zuo
2026-03-15 20:36:48 +08:00
committed by GitHub
Unverified
parent 08960d700f
commit 1dbe4a8466
36 changed files with 1511 additions and 197 deletions

View File

@@ -9,6 +9,7 @@ vi.mock('@/lib/api-client', () => ({
describe('host-api', () => {
beforeEach(() => {
vi.resetAllMocks();
window.localStorage.removeItem('clawx:allow-localhost-fallback');
});
it('uses IPC proxy and returns unified envelope json', async () => {
@@ -51,6 +52,7 @@ describe('host-api', () => {
json: async () => ({ fallback: true }),
});
vi.stubGlobal('fetch', fetchMock);
window.localStorage.setItem('clawx:allow-localhost-fallback', '1');
invokeIpcMock.mockResolvedValueOnce({
ok: false,
@@ -86,6 +88,7 @@ describe('host-api', () => {
json: async () => ({ fallback: true }),
});
vi.stubGlobal('fetch', fetchMock);
window.localStorage.setItem('clawx:allow-localhost-fallback', '1');
invokeIpcMock.mockRejectedValueOnce(new Error('Invalid IPC channel: hostapi:fetch'));
@@ -98,4 +101,19 @@ describe('host-api', () => {
expect.objectContaining({ headers: expect.any(Object) }),
);
});
it('does not use localhost fallback when policy flag is disabled', async () => {
const fetchMock = vi.fn().mockResolvedValue({
ok: true,
status: 200,
json: async () => ({ fallback: true }),
});
vi.stubGlobal('fetch', fetchMock);
invokeIpcMock.mockRejectedValueOnce(new Error('Invalid IPC channel: hostapi:fetch'));
const { hostApiFetch } = await import('@/lib/host-api');
await expect(hostApiFetch('/api/test')).rejects.toThrow('Invalid IPC channel: hostapi:fetch');
expect(fetchMock).not.toHaveBeenCalled();
});
});