790
expect(result.arguments?.fileInput).toEqual(mockFileData);
791
});
792
793
+ it('should upload each file when anyOf accepts a single file or an array of files', async () => {
794
+ const mockFileData1 = {
795
+ name: 'file1.txt',
796
+ mimetype: 'text/plain',
797
+ s3key: 'uploads/file1.txt',
798
+ };
799
+ const mockFileData2 = {
800
+ name: 'file2.txt',
801
+ mimetype: 'text/plain',
802
+ s3key: 'uploads/file2.txt',
803
+ };
804
+ vi.mocked(fileUtils.getFileDataAfterUploadingToS3)
805
+ .mockResolvedValueOnce(mockFileData1)
806
+ .mockResolvedValueOnce(mockFileData2);
807
+
808
+ const toolWithSingleOrArrayFile: Tool = {
809
+ slug: 'test-tool',
810
+ name: 'Test Tool',
811
+ description: 'A test tool',
812
+ tags: ['test'],
813
+ inputParameters: {
814
+ type: 'object',
815
+ properties: {
816
+ attachment: {
817
+ anyOf: [
818
+ { type: 'string', file_uploadable: true },
819
+ {
820
+ type: 'array',
821
+ items: { type: 'string', file_uploadable: true },
822
+ },
823
+ ],
824
+ },
825
+ },
826
+ },
827
+ version: '20251201_01',
828
+ availableVersions: ['20251201_01'],
829
+ };
830
+
831
+ const result = await fileToolModifier.fileUploadModifier(toolWithSingleOrArrayFile, {
832
+ toolSlug: 'test-tool',
833
+ toolkitSlug: 'test-toolkit',
834
+ params: {
835
+ arguments: {
836
+ attachment: ['/path/to/file1.txt', '/path/to/file2.txt'],
837
+ },
838
+ userId: 'test-user',
839
+ },
840
+ });
841
+
842
+ expect(fileUtils.getFileDataAfterUploadingToS3).toHaveBeenCalledTimes(2);
843
+ expect(fileUtils.getFileDataAfterUploadingToS3).toHaveBeenNthCalledWith(
844
+ 1,
845
+ '/path/to/file1.txt',
846
+ {
847
+ toolSlug: 'test-tool',
848
+ toolkitSlug: 'test-toolkit',
849
+ client: mockClient,
850
+ }
851
+ );
852
+ expect(fileUtils.getFileDataAfterUploadingToS3).toHaveBeenNthCalledWith(
853
+ 2,
854
+ '/path/to/file2.txt',
855
+ {
856
+ toolSlug: 'test-tool',
857
+ toolkitSlug: 'test-toolkit',
858
+ client: mockClient,
859
+ }
860
+ );
861
+ expect(result.arguments?.attachment).toEqual([mockFileData1, mockFileData2]);
862
+ });
863
+
864
+ it('should keep single-file behavior when anyOf also accepts an array of files', async () => {
865
+ const mockFileData = {
866
+ name: 'file.txt',
867
+ mimetype: 'text/plain',
868
+ s3key: 'uploads/file.txt',
869
+ };
870
+ vi.mocked(fileUtils.getFileDataAfterUploadingToS3).mockResolvedValue(mockFileData);
871
+
872
+ const toolWithSingleOrArrayFile: Tool = {
873
+ slug: 'test-tool',
874
+ name: 'Test Tool',
875
+ description: 'A test tool',
876
+ tags: ['test'],
877
+ inputParameters: {
878
+ type: 'object',
879
+ properties: {
880
+ attachment: {
881
+ anyOf: [
882
+ { type: 'string', file_uploadable: true },
883
+ {
884
+ type: 'array',
885
+ items: { type: 'string', file_uploadable: true },
886
+ },
887
+ ],
888
+ },
889
+ },
890
+ },
891
+ version: '20251201_01',
892
+ availableVersions: ['20251201_01'],
893
+ };
894
+
895
+ const result = await fileToolModifier.fileUploadModifier(toolWithSingleOrArrayFile, {
896
+ toolSlug: 'test-tool',
897
+ toolkitSlug: 'test-toolkit',
898
+ params: {
899
+ arguments: {
900
+ attachment: '/path/to/file.txt',
901
+ },
902
+ userId: 'test-user',
903
+ },
904
+ });
905
+
906
+ expect(fileUtils.getFileDataAfterUploadingToS3).toHaveBeenCalledTimes(1);
907
+ expect(fileUtils.getFileDataAfterUploadingToS3).toHaveBeenCalledWith('/path/to/file.txt', {
908
+ toolSlug: 'test-tool',
909
+ toolkitSlug: 'test-toolkit',
910
+ client: mockClient,
911
+ });
912
+ expect(result.arguments?.attachment).toEqual(mockFileData);
913
+ });
914
+
915
+ it('should upload file array items whose item schema is an anyOf file branch', async () => {
916
+ const mockFileData1 = {
917
+ name: 'file1.txt',
918
+ mimetype: 'text/plain',
919
+ s3key: 'uploads/file1.txt',
920
+ };
921
+ const mockFileData2 = {
922
+ name: 'file2.txt',
923
+ mimetype: 'text/plain',
924
+ s3key: 'uploads/file2.txt',
925
+ };
926
+ vi.mocked(fileUtils.getFileDataAfterUploadingToS3)
927
+ .mockResolvedValueOnce(mockFileData1)
928
+ .mockResolvedValueOnce(mockFileData2);
929
+
930
+ const toolWithArrayItemAnyOf: Tool = {
931
+ slug: 'test-tool',
932
+ name: 'Test Tool',
933
+ description: 'A test tool',
934
+ tags: ['test'],
935
+ inputParameters: {
936
+ type: 'object',
937
+ properties: {
938
+ attachments: {
939
+ type: 'array',
940
+ items: {
941
+ anyOf: [{ type: 'string', file_uploadable: true }, { type: 'null' }],
942
+ },
943
+ },
944
+ },
945
+ },
946
+ version: '20251201_01',
947
+ availableVersions: ['20251201_01'],
948
+ };
949
+
950
+ const result = await fileToolModifier.fileUploadModifier(toolWithArrayItemAnyOf, {
951
+ toolSlug: 'test-tool',
952
+ toolkitSlug: 'test-toolkit',
953
+ params: {
954
+ arguments: {
955
+ attachments: ['/path/to/file1.txt', null, '/path/to/file2.txt'],
956
+ },
957
+ userId: 'test-user',
958
+ },
959
+ });
960
+
961
+ expect(fileUtils.getFileDataAfterUploadingToS3).toHaveBeenCalledTimes(2);
962
+ expect(result.arguments?.attachments).toEqual([mockFileData1, null, mockFileData2]);
963
+ });
964
+
965
it('should not upload when value is null for anyOf with null variant', async () => {
966
const toolWithAnyOf: Tool = {
967
slug: 'test-tool',
1570
});
1571
});
1572
1573
+ it('should download each file when anyOf accepts a single file or an array of files', async () => {
1574
+ const mockDownloadResult1 = {
1575
+ name: 'file1.txt',
1576
+ mimeType: 'text/plain',
1577
+ s3Url: 'downloads/file1.txt',
1578
+ filePath: '/downloaded/file1.txt',
1579
+ };
1580
+ const mockDownloadResult2 = {
1581
+ name: 'file2.txt',
1582
+ mimeType: 'text/plain',
1583
+ s3Url: 'downloads/file2.txt',
1584
+ filePath: '/downloaded/file2.txt',
1585
+ };
1586
+ vi.mocked(fileUtils.downloadFileFromS3)
1587
+ .mockResolvedValueOnce(mockDownloadResult1)
1588
+ .mockResolvedValueOnce(mockDownloadResult2);
1589
+
1590
+ const toolWithSingleOrArrayFile: Tool = {
1591
+ slug: 'test-tool',
1592
+ name: 'Test Tool',
1593
+ description: 'A test tool',
1594
+ tags: ['test'],
1595
+ outputParameters: {
1596
+ type: 'object',
1597
+ properties: {
1598
+ attachment: {
1599
+ anyOf: [
1600
+ {
1601
+ type: 'object',
1602
+ file_downloadable: true,
1603
+ properties: {
1604
+ s3url: { type: 'string' },
1605
+ mimetype: { type: 'string' },
1606
+ },
1607
+ },
1608
+ {
1609
+ type: 'array',
1610
+ items: {
1611
+ type: 'object',
1612
+ file_downloadable: true,
1613
+ properties: {
1614
+ s3url: { type: 'string' },
1615
+ mimetype: { type: 'string' },
1616
+ },
1617
+ },
1618
+ },
1619
+ ],
1620
+ },
1621
+ },
1622
+ },
1623
+ version: '20251201_01',
1624
+ availableVersions: ['20251201_01'],
1625
+ };
1626
+
1627
+ const modifiedResult = await fileToolModifier.fileDownloadModifier(
1628
+ toolWithSingleOrArrayFile,
1629
+ {
1630
+ toolSlug: 'test-tool',
1631
+ toolkitSlug: 'test-toolkit',
1632
+ result: {
1633
+ data: {
1634
+ attachment: [
1635
+ {
1636
+ s3url: 'https://s3.example.com/file1.txt',
1637
+ mimetype: 'text/plain',
1638
+ },
1639
+ {
1640
+ s3url: 'https://s3.example.com/file2.txt',
1641
+ mimetype: 'text/plain',
1642
+ },
1643
+ ],
1644
+ },
1645
+ error: null,
1646
+ successful: true,
1647
+ },
1648
+ }
1649
+ );
1650
+
1651
+ expect(fileUtils.downloadFileFromS3).toHaveBeenCalledTimes(2);
1652
+ expect(modifiedResult.data.attachment).toEqual([
1653
+ {
1654
+ uri: '/downloaded/file1.txt',
1655
+ file_downloaded: true,
1656
+ s3url: 'https://s3.example.com/file1.txt',
1657
+ mimeType: 'text/plain',
1658
+ },
1659
+ {
1660
+ uri: '/downloaded/file2.txt',
1661
+ file_downloaded: true,
1662
+ s3url: 'https://s3.example.com/file2.txt',
1663
+ mimeType: 'text/plain',
1664
+ },
1665
+ ]);
1666
+ });
1667
+
1668
it('should not download when value is null for anyOf with null variant', async () => {
1669
const toolWithAnyOf: Tool = {
1670
slug: 'test-tool',