515
});
516
});
517
518
+ describe('COMPOSIO_MULTI_EXECUTE_TOOL — detailed split/merge behavior', () => {
519
+ /** Helper: set up provider + session + capture executeFn + get latest Tools mock */
520
+ const setupMultiExecute = async (
521
+ client: ReturnType<typeof createMockClient>,
522
+ customTools: CustomToolHandle[]
523
+ ) => {
524
+ const provider = new MockProvider();
525
+ captureExecuteFn(provider);
526
+ const session = createSessionWithProvider(client, provider, customTools);
527
+ await session.tools();
528
+ const executeFn = (provider as any)._capturedExecuteFn;
529
+ const toolsInstance = (Tools as any).mock.results[
530
+ (Tools as any).mock.results.length - 1
531
+ ].value;
532
+ return { executeFn, toolsInstance, provider, session };
533
+ };
534
+
535
+ it('should only send remote items in the backend payload (not local ones)', async () => {
536
+ const { executeFn, toolsInstance } = await setupMultiExecute(mockClient, [customToolHandle]);
537
+
538
+ toolsInstance.executeMetaTool.mockResolvedValueOnce({
539
+ data: { GMAIL_SEND_EMAIL: { sent: true } },
540
+ error: null,
541
+ successful: true,
542
+ });
543
+
544
+ await executeFn('COMPOSIO_MULTI_EXECUTE_TOOL', {
545
+ tools: [
546
+ { tool_slug: 'LOCAL_GET_USER_CONTEXT', arguments: { category: 'a' } },
547
+ { tool_slug: 'GMAIL_SEND_EMAIL', arguments: { to: 'x@y.com' } },
548
+ ],
549
+ sync_response_to_workbench: false,
550
+ });
551
+
552
+ // Backend should only receive the remote tool
553
+ const metaCall = toolsInstance.executeMetaTool.mock.calls[0];
554
+ const backendTools = metaCall[1].arguments.tools;
555
+ expect(backendTools).toHaveLength(1);
556
+ expect(backendTools[0].tool_slug).toBe('GMAIL_SEND_EMAIL');
557
+ });
558
+
559
+ it('should merge results with remote keys first, local keys last', async () => {
560
+ const { executeFn, toolsInstance } = await setupMultiExecute(mockClient, [customToolHandle]);
561
+
562
+ toolsInstance.executeMetaTool.mockResolvedValueOnce({
563
+ data: { GMAIL_SEND_EMAIL: { sent: true } },
564
+ error: null,
565
+ successful: true,
566
+ });
567
+
568
+ const result = await executeFn('COMPOSIO_MULTI_EXECUTE_TOOL', {
569
+ tools: [
570
+ { tool_slug: 'LOCAL_GET_USER_CONTEXT', arguments: { category: 'a' } },
571
+ { tool_slug: 'GMAIL_SEND_EMAIL', arguments: { to: 'x@y.com' } },
572
+ ],
573
+ sync_response_to_workbench: false,
574
+ });
575
+
576
+ // Verify ordering: remote keys come before local keys
577
+ const keys = Object.keys(result.data);
578
+ const remoteIdx = keys.indexOf('GMAIL_SEND_EMAIL');
579
+ const localIdx = keys.indexOf('LOCAL_GET_USER_CONTEXT');
580
+ expect(remoteIdx).toBeLessThan(localIdx);
581
+ });
582
+
583
+ it('should handle multiple local tools in same batch', async () => {
584
+ const { executeFn } = await setupMultiExecute(mockClient, [customToolHandle, sessionToolHandle]);
585
+
586
+ const result = await executeFn('COMPOSIO_MULTI_EXECUTE_TOOL', {
587
+ tools: [
588
+ { tool_slug: 'LOCAL_GET_USER_CONTEXT', arguments: { category: 'batch' } },
589
+ { tool_slug: 'LOCAL_GET_AD_ACCOUNTS', arguments: { fields: 'id,name' } },
590
+ ],
591
+ sync_response_to_workbench: false,
592
+ });
593
+
594
+ expect(localExecute).toHaveBeenCalledWith(
595
+ { category: 'batch' },
596
+ expect.objectContaining({ userId: 'user_1' })
597
+ );
598
+ expect(sessionExecute).toHaveBeenCalledWith(
599
+ { fields: 'id,name' },
600
+ expect.objectContaining({ userId: 'user_1' })
601
+ );
602
+ expect(result.data).toHaveProperty('LOCAL_GET_USER_CONTEXT');
603
+ expect(result.data).toHaveProperty('LOCAL_GET_AD_ACCOUNTS');
604
+ });
605
+
606
+ it('should handle mixed batch with multiple locals + multiple remotes', async () => {
607
+ const { executeFn, toolsInstance } = await setupMultiExecute(
608
+ mockClient,
609
+ [customToolHandle, sessionToolHandle]
610
+ );
611
+
612
+ toolsInstance.executeMetaTool.mockResolvedValueOnce({
613
+ data: {
614
+ GMAIL_SEND_EMAIL: { sent: true },
615
+ SLACK_POST_MESSAGE: { ts: '999' },
616
+ },
617
+ error: null,
618
+ successful: true,
619
+ });
620
+
621
+ const result = await executeFn('COMPOSIO_MULTI_EXECUTE_TOOL', {
622
+ tools: [
623
+ { tool_slug: 'LOCAL_GET_USER_CONTEXT', arguments: { category: 'x' } },
624
+ { tool_slug: 'GMAIL_SEND_EMAIL', arguments: { to: 'a@b.com' } },
625
+ { tool_slug: 'LOCAL_GET_AD_ACCOUNTS', arguments: { fields: 'id' } },
626
+ { tool_slug: 'SLACK_POST_MESSAGE', arguments: { channel: '#dev' } },
627
+ ],
628
+ sync_response_to_workbench: false,
629
+ });
630
+
631
+ // All 4 tools should appear in result
632
+ expect(Object.keys(result.data)).toHaveLength(4);
633
+ expect(result.data).toHaveProperty('LOCAL_GET_USER_CONTEXT');
634
+ expect(result.data).toHaveProperty('LOCAL_GET_AD_ACCOUNTS');
635
+ expect(result.data).toHaveProperty('GMAIL_SEND_EMAIL');
636
+ expect(result.data).toHaveProperty('SLACK_POST_MESSAGE');
637
+
638
+ // Backend only got the 2 remote tools
639
+ const backendTools = toolsInstance.executeMetaTool.mock.calls[0][1].arguments.tools;
640
+ expect(backendTools).toHaveLength(2);
641
+ expect(backendTools.map((t: any) => t.tool_slug)).toEqual([
642
+ 'GMAIL_SEND_EMAIL',
643
+ 'SLACK_POST_MESSAGE',
644
+ ]);
645
+
646
+ // Both local execute fns were called
647
+ expect(localExecute).toHaveBeenCalled();
648
+ expect(sessionExecute).toHaveBeenCalled();
649
+ });
650
+
651
+ it('should still succeed when one local tool errors in a mixed batch', async () => {
652
+ const throwingHandle = CustomTool({
653
+ slug: 'MIXED_THROWER',
654
+ name: 'Throws in batch',
655
+ description: 'Throws',
656
+ inputParams: z.object({}),
657
+ execute: async () => { throw new Error('batch-boom'); },
658
+ });
659
+
660
+ const { executeFn, toolsInstance } = await setupMultiExecute(
661
+ mockClient,
662
+ [customToolHandle, throwingHandle]
663
+ );
664
+
665
+ toolsInstance.executeMetaTool.mockResolvedValueOnce({
666
+ data: { GMAIL_SEND_EMAIL: { sent: true } },
667
+ error: null,
668
+ successful: true,
669
+ });
670
+
671
+ const result = await executeFn('COMPOSIO_MULTI_EXECUTE_TOOL', {
672
+ tools: [
673
+ { tool_slug: 'LOCAL_GET_USER_CONTEXT', arguments: { category: 'ok' } },
674
+ { tool_slug: 'LOCAL_MIXED_THROWER', arguments: {} },
675
+ { tool_slug: 'GMAIL_SEND_EMAIL', arguments: { to: 'a@b.com' } },
676
+ ],
677
+ sync_response_to_workbench: false,
678
+ });
679
+
680
+ // Successful local tool still has its data
681
+ expect(result.data).toHaveProperty('LOCAL_GET_USER_CONTEXT');
682
+ // Throwing tool has empty data
683
+ expect(result.data.LOCAL_MIXED_THROWER).toEqual({});
684
+ // Remote tool still ran
685
+ expect(result.data).toHaveProperty('GMAIL_SEND_EMAIL');
686
+ // Error is surfaced
687
+ expect(result.error).toBe('batch-boom');
688
+ expect(result.successful).toBe(false);
689
+ });
690
+
691
+ it('should forward to backend when tools array is empty', async () => {
692
+ const { executeFn, toolsInstance } = await setupMultiExecute(mockClient, [customToolHandle]);
693
+
694
+ await executeFn('COMPOSIO_MULTI_EXECUTE_TOOL', {
695
+ tools: [],
696
+ sync_response_to_workbench: false,
697
+ });
698
+
699
+ // Empty array → fallback to backend with full original input
700
+ expect(toolsInstance.executeMetaTool).toHaveBeenCalledWith(
701
+ 'COMPOSIO_MULTI_EXECUTE_TOOL',
702
+ expect.objectContaining({
703
+ sessionId: 'sess_123',
704
+ arguments: expect.objectContaining({ tools: [] }),
705
+ }),
706
+ undefined
707
+ );
708
+ });
709
+
710
+ it('should handle non-object items in tools array gracefully', async () => {
711
+ const { executeFn, toolsInstance } = await setupMultiExecute(mockClient, [customToolHandle]);
712
+
713
+ toolsInstance.executeMetaTool.mockResolvedValueOnce({
714
+ data: {},
715
+ error: null,
716
+ successful: true,
717
+ });
718
+
719
+ // Malformed: string instead of object — should not crash
720
+ const result = await executeFn('COMPOSIO_MULTI_EXECUTE_TOOL', {
721
+ tools: ['not-an-object', null, { tool_slug: 'LOCAL_GET_USER_CONTEXT', arguments: { category: 'ok' } }],
722
+ sync_response_to_workbench: false,
723
+ });
724
+
725
+ // The valid local tool should still execute
726
+ expect(localExecute).toHaveBeenCalledWith(
727
+ { category: 'ok' },
728
+ expect.objectContaining({ userId: 'user_1' })
729
+ );
730
+ expect(result.data).toHaveProperty('LOCAL_GET_USER_CONTEXT');
731
+ });
732
+
733
+ it('should execute local and remote in parallel (not sequentially)', async () => {
734
+ // Track call timing to verify parallelism
735
+ const callOrder: string[] = [];
736
+
737
+ const slowLocalHandle = CustomTool({
738
+ slug: 'SLOW_LOCAL',
739
+ name: 'Slow local',
740
+ description: 'Slow',
741
+ inputParams: z.object({}),
742
+ execute: async () => {
743
+ callOrder.push('local-start');
744
+ await new Promise(r => setTimeout(r, 50));
745
+ callOrder.push('local-end');
746
+ return { slow: true };
747
+ },
748
+ });
749
+
750
+ const provider = new MockProvider();
751
+ captureExecuteFn(provider);
752
+ const session = createSessionWithProvider(mockClient, provider, [slowLocalHandle]);
753
+ await session.tools();
754
+ const executeFn = (provider as any)._capturedExecuteFn;
755
+ const toolsInstance = (Tools as any).mock.results[
756
+ (Tools as any).mock.results.length - 1
757
+ ].value;
758
+
759
+ toolsInstance.executeMetaTool.mockImplementation(async () => {
760
+ callOrder.push('remote-start');
761
+ await new Promise(r => setTimeout(r, 50));
762
+ callOrder.push('remote-end');
763
+ return { data: { GMAIL_SEND_EMAIL: { sent: true } }, error: null, successful: true };
764
+ });
765
+
766
+ await executeFn('COMPOSIO_MULTI_EXECUTE_TOOL', {
767
+ tools: [
768
+ { tool_slug: 'LOCAL_SLOW_LOCAL', arguments: {} },
769
+ { tool_slug: 'GMAIL_SEND_EMAIL', arguments: { to: 'x@y.com' } },
770
+ ],
771
+ sync_response_to_workbench: false,
772
+ });
773
+
774
+ // Both should start before either ends (parallel execution)
775
+ const localStartIdx = callOrder.indexOf('local-start');
776
+ const remoteStartIdx = callOrder.indexOf('remote-start');
777
+ const localEndIdx = callOrder.indexOf('local-end');
778
+ const remoteEndIdx = callOrder.indexOf('remote-end');
779
+
780
+ // Both started
781
+ expect(localStartIdx).toBeGreaterThanOrEqual(0);
782
+ expect(remoteStartIdx).toBeGreaterThanOrEqual(0);
783
+ // Both started before either finished
784
+ expect(Math.max(localStartIdx, remoteStartIdx)).toBeLessThan(
785
+ Math.min(localEndIdx, remoteEndIdx)
786
+ );
787
+ });
788
+ });
789
+
790
describe('multiple custom tools', () => {
791
it('should route each tool to its correct execute function', async () => {
792
const session = createSession(mockClient, [customToolHandle, sessionToolHandle]);