Skip to content

Commit e974e81

Browse files
committed
refactor tests
1 parent 7f94f93 commit e974e81

File tree

1 file changed

+96
-121
lines changed

1 file changed

+96
-121
lines changed

src/kernels/helpers.unit.test.ts

Lines changed: 96 additions & 121 deletions
Original file line numberDiff line numberDiff line change
@@ -648,19 +648,57 @@ suite('Kernel Connection Helpers', () => {
648648
});
649649

650650
suite('executeSilently', () => {
651-
test('Returns outputs from kernel execution', async () => {
652-
const mockKernel = {
651+
interface MockKernelOptions {
652+
status: 'ok' | 'error';
653+
messages?: Array<{
654+
msg_type: 'stream' | 'error' | 'display_data';
655+
content: any;
656+
}>;
657+
errorContent?: {
658+
ename: string;
659+
evalue: string;
660+
traceback: string[];
661+
};
662+
}
663+
664+
function createMockKernel(options: MockKernelOptions) {
665+
let iopubCallback: ((msg: any) => void) | undefined;
666+
667+
return {
653668
requestExecute: () => ({
654669
done: Promise.resolve({
655-
content: {
656-
status: 'ok' as const
657-
}
670+
content:
671+
options.status === 'ok'
672+
? { status: 'ok' as const }
673+
: {
674+
status: 'error' as const,
675+
...options.errorContent
676+
}
658677
}),
659-
onIOPub: () => {
660-
// noop
678+
onIOPub: (cb: (msg: any) => void) => {
679+
iopubCallback = cb;
680+
// Dispatch messages asynchronously to preserve async behavior
681+
if (options.messages && options.messages.length > 0) {
682+
setTimeout(() => {
683+
if (iopubCallback) {
684+
options.messages!.forEach((msg) => {
685+
iopubCallback!({
686+
header: { msg_type: msg.msg_type },
687+
content: msg.content
688+
});
689+
});
690+
}
691+
}, 0);
692+
}
661693
}
662694
})
663695
};
696+
}
697+
698+
test('Returns outputs from kernel execution', async () => {
699+
const mockKernel = createMockKernel({
700+
status: 'ok'
701+
});
664702

665703
const code = 'print("hello")';
666704
const { executeSilently } = await import('./helpers');
@@ -671,18 +709,9 @@ suite('Kernel Connection Helpers', () => {
671709
});
672710

673711
test('Handles empty code', async () => {
674-
const mockKernel = {
675-
requestExecute: () => ({
676-
done: Promise.resolve({
677-
content: {
678-
status: 'ok' as const
679-
}
680-
}),
681-
onIOPub: () => {
682-
// noop
683-
}
684-
})
685-
};
712+
const mockKernel = createMockKernel({
713+
status: 'ok'
714+
});
686715

687716
const code = '';
688717
const { executeSilently } = await import('./helpers');
@@ -693,32 +722,18 @@ suite('Kernel Connection Helpers', () => {
693722
});
694723

695724
test('Collects stream outputs', async () => {
696-
let iopubCallback: ((msg: any) => void) | undefined;
697-
698-
const mockKernel = {
699-
requestExecute: () => ({
700-
done: Promise.resolve({
725+
const mockKernel = createMockKernel({
726+
status: 'ok',
727+
messages: [
728+
{
729+
msg_type: 'stream',
701730
content: {
702-
status: 'ok' as const
731+
name: 'stdout',
732+
text: 'test output'
703733
}
704-
}),
705-
onIOPub: (cb: (msg: any) => void) => {
706-
iopubCallback = cb;
707-
// Simulate stream output
708-
setTimeout(() => {
709-
if (iopubCallback) {
710-
iopubCallback({
711-
header: { msg_type: 'stream' },
712-
content: {
713-
name: 'stdout',
714-
text: 'test output'
715-
}
716-
});
717-
}
718-
}, 0);
719734
}
720-
})
721-
};
735+
]
736+
});
722737

723738
const code = 'print("test")';
724739
const { executeSilently } = await import('./helpers');
@@ -732,36 +747,24 @@ suite('Kernel Connection Helpers', () => {
732747
});
733748

734749
test('Collects error outputs', async () => {
735-
let iopubCallback: ((msg: any) => void) | undefined;
736-
737-
const mockKernel = {
738-
requestExecute: () => ({
739-
done: Promise.resolve({
750+
const mockKernel = createMockKernel({
751+
status: 'error',
752+
errorContent: {
753+
ename: 'NameError',
754+
evalue: 'name not defined',
755+
traceback: ['Traceback...']
756+
},
757+
messages: [
758+
{
759+
msg_type: 'error',
740760
content: {
741-
status: 'error' as const,
742761
ename: 'NameError',
743762
evalue: 'name not defined',
744763
traceback: ['Traceback...']
745764
}
746-
}),
747-
onIOPub: (cb: (msg: any) => void) => {
748-
iopubCallback = cb;
749-
// Simulate error output
750-
setTimeout(() => {
751-
if (iopubCallback) {
752-
iopubCallback({
753-
header: { msg_type: 'error' },
754-
content: {
755-
ename: 'NameError',
756-
evalue: 'name not defined',
757-
traceback: ['Traceback...']
758-
}
759-
});
760-
}
761-
}, 0);
762765
}
763-
})
764-
};
766+
]
767+
});
765768

766769
const code = 'undefined_variable';
767770
const { executeSilently } = await import('./helpers');
@@ -775,34 +778,20 @@ suite('Kernel Connection Helpers', () => {
775778
});
776779

777780
test('Collects display_data outputs', async () => {
778-
let iopubCallback: ((msg: any) => void) | undefined;
779-
780-
const mockKernel = {
781-
requestExecute: () => ({
782-
done: Promise.resolve({
781+
const mockKernel = createMockKernel({
782+
status: 'ok',
783+
messages: [
784+
{
785+
msg_type: 'display_data',
783786
content: {
784-
status: 'ok' as const
787+
data: {
788+
'text/plain': 'some data'
789+
},
790+
metadata: {}
785791
}
786-
}),
787-
onIOPub: (cb: (msg: any) => void) => {
788-
iopubCallback = cb;
789-
// Simulate display_data output
790-
setTimeout(() => {
791-
if (iopubCallback) {
792-
iopubCallback({
793-
header: { msg_type: 'display_data' },
794-
content: {
795-
data: {
796-
'text/plain': 'some data'
797-
},
798-
metadata: {}
799-
}
800-
});
801-
}
802-
}, 0);
803792
}
804-
})
805-
};
793+
]
794+
});
806795

807796
const code = 'display("data")';
808797
const { executeSilently } = await import('./helpers');
@@ -816,39 +805,25 @@ suite('Kernel Connection Helpers', () => {
816805
});
817806

818807
test('Handles multiple outputs', async () => {
819-
let iopubCallback: ((msg: any) => void) | undefined;
820-
821-
const mockKernel = {
822-
requestExecute: () => ({
823-
done: Promise.resolve({
808+
const mockKernel = createMockKernel({
809+
status: 'ok',
810+
messages: [
811+
{
812+
msg_type: 'stream',
824813
content: {
825-
status: 'ok' as const
814+
name: 'stdout',
815+
text: 'output 1'
816+
}
817+
},
818+
{
819+
msg_type: 'stream',
820+
content: {
821+
name: 'stdout',
822+
text: 'output 2'
826823
}
827-
}),
828-
onIOPub: (cb: (msg: any) => void) => {
829-
iopubCallback = cb;
830-
// Simulate multiple outputs
831-
setTimeout(() => {
832-
if (iopubCallback) {
833-
iopubCallback({
834-
header: { msg_type: 'stream' },
835-
content: {
836-
name: 'stdout',
837-
text: 'output 1'
838-
}
839-
});
840-
iopubCallback({
841-
header: { msg_type: 'stream' },
842-
content: {
843-
name: 'stdout',
844-
text: 'output 2'
845-
}
846-
});
847-
}
848-
}, 0);
849824
}
850-
})
851-
};
825+
]
826+
});
852827

853828
const code = 'print("1"); print("2")';
854829
const { executeSilently } = await import('./helpers');

0 commit comments

Comments
 (0)