Skip to content

Commit 8860500

Browse files
committed
fix: catch unhandled rejections in SDK and poll tests
1 parent 60ce237 commit 8860500

4 files changed

Lines changed: 31 additions & 34 deletions

File tree

test/polling/poll.test.ts

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,22 @@ function jsonRes(body: unknown): Response {
3232

3333
const originalFetch = globalThis.fetch;
3434

35+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
36+
function setFetch(fn: any): void {
37+
globalThis.fetch = fn as typeof globalThis.fetch;
38+
}
39+
3540
afterEach(() => {
3641
globalThis.fetch = originalFetch;
3742
});
3843

3944
describe('poll', () => {
4045
it('completes when isComplete returns true', async () => {
4146
let callCount = 0;
42-
globalThis.fetch = mock(() => { callCount++; return Promise.resolve(jsonRes({ status: 'Success', task_id: 'task-1' })); });
47+
setFetch(mock(() => {
48+
callCount++;
49+
return Promise.resolve(jsonRes({ status: 'Success', task_id: 'task-1' }));
50+
}));
4351

4452
const { poll } = await import('../../src/polling/poll');
4553
const result = await poll(baseConfig, {
@@ -55,9 +63,9 @@ describe('poll', () => {
5563
});
5664

5765
it('throws on isFailed with status message', async () => {
58-
globalThis.fetch = mock(() =>
59-
Promise.resolve(jsonRes({ status: 'Failed', base_resp: { status_code: 1, status_msg: 'Task error' } })),
60-
);
66+
setFetch(mock(() => Promise.resolve(
67+
jsonRes({ status: 'Failed', base_resp: { status_code: 1, status_msg: 'Task error' } }),
68+
)));
6169

6270
const { poll } = await import('../../src/polling/poll');
6371
await expect(
@@ -73,7 +81,7 @@ describe('poll', () => {
7381
});
7482

7583
it('throws on timeout', async () => {
76-
globalThis.fetch = mock(() => Promise.resolve(jsonRes({ status: 'Processing' })));
84+
setFetch(mock(() => Promise.resolve(jsonRes({ status: 'Processing' }))));
7785

7886
const { poll } = await import('../../src/polling/poll');
7987
await expect(
@@ -89,7 +97,10 @@ describe('poll', () => {
8997

9098
it('returns immediately when first request succeeds', async () => {
9199
let callCount = 0;
92-
globalThis.fetch = mock(() => { callCount++; return Promise.resolve(jsonRes({ status: 'Success' })); });
100+
setFetch(mock(() => {
101+
callCount++;
102+
return Promise.resolve(jsonRes({ status: 'Success' }));
103+
}));
93104

94105
const { poll } = await import('../../src/polling/poll');
95106
const result = await poll(baseConfig, {

test/sdk/image.test.ts

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -146,11 +146,8 @@ describe('ImageSDK.validateParams', () => {
146146
});
147147

148148
it('accepts valid dimensions (passes validation, fails on network)', async () => {
149-
try {
150-
await sdk.generate({ prompt: 'test', width: 1024, height: 1024 });
151-
} catch (err) {
152-
expect((err as Error).message).not.toContain('width');
153-
expect((err as Error).message).not.toContain('height');
154-
}
149+
await expect(
150+
sdk.generate({ prompt: 'test', width: 1024, height: 1024 }),
151+
).rejects.not.toThrow(/width|height/);
155152
});
156153
});

test/sdk/text.test.ts

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -83,11 +83,8 @@ describe('TextSDK.validateParams', () => {
8383
});
8484

8585
it('applies defaults for model and max_tokens', async () => {
86-
// Valid input passes validation → fails on network, not validation
87-
try {
88-
await sdk.chat({ messages: [{ role: 'user', content: 'Hi' }] });
89-
} catch (err) {
90-
expect((err as Error).message).not.toContain('At least one message');
91-
}
86+
await expect(
87+
sdk.chat({ messages: [{ role: 'user', content: 'Hi' }] }),
88+
).rejects.not.toThrow('At least one message');
9289
});
9390
});

test/sdk/video.test.ts

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -87,29 +87,21 @@ describe('VideoSDK.validateParams', () => {
8787

8888
it('auto-selects SEF model when last_frame_image is provided', async () => {
8989
// Validation passes → tries network → fails with non-validation error
90-
try {
91-
await sdk.generate({
90+
await expect(
91+
sdk.generate({
9292
prompt: 'test',
9393
first_frame_image: 'data:image/png;base64,xxx',
9494
last_frame_image: 'data:image/png;base64,yyy',
95-
});
96-
} catch (err) {
97-
const msg = (err as Error).message;
98-
expect(msg).not.toContain('prompt');
99-
expect(msg).not.toContain('last_frame');
100-
}
95+
}),
96+
).rejects.not.toThrow(/prompt|last_frame/);
10197
});
10298

10399
it('auto-selects S2V model when subject_reference is provided', async () => {
104-
try {
105-
await sdk.generate({
100+
await expect(
101+
sdk.generate({
106102
prompt: 'test',
107103
subject_reference: [{ type: 'character', image: ['data:image/png;base64,zzz'] }],
108-
});
109-
} catch (err) {
110-
const msg = (err as Error).message;
111-
expect(msg).not.toContain('prompt');
112-
expect(msg).not.toContain('subject');
113-
}
104+
}),
105+
).rejects.not.toThrow(/prompt|subject/);
114106
});
115107
});

0 commit comments

Comments
 (0)