|
| 1 | +import { |
| 2 | + addBlueprint, |
| 3 | + addProvider, |
| 4 | + addScript, |
| 5 | + createService, |
| 6 | + getBlueprints, |
| 7 | + getInterfaces, |
| 8 | + getModules, |
| 9 | + getProviders, |
| 10 | + removeScript, |
| 11 | + uploadModule, |
| 12 | +} from '../../internal/builtins'; |
| 13 | +import { ModuleConfig } from '../../internal/moduleConfig'; |
| 14 | +import { createConnectedClient } from '../util'; |
| 15 | + |
| 16 | +const dev2multiaddr = '/dns4/dev.fluence.dev/tcp/19003/wss/p2p/12D3KooWBUJifCTgaxAUrcM9JysqCcS4CS8tiYH5hExbdWCAoNwb'; |
| 17 | +const dev3multiaddr = '/dns4/dev.fluence.dev/tcp/19004/wss/p2p/12D3KooWJbJFaZ3k5sNd8DjQgg3aERoKtBAnirEvPV8yp76kEXHB'; |
| 18 | + |
| 19 | +const dev2peerId = '12D3KooWEXNUbCXooUwHrHBbrmjsrpHXoEphPwbjQXEGyzbqKnE9'; |
| 20 | + |
| 21 | +describe('Builtins usage suite', () => { |
| 22 | + it('get_modules', async function () { |
| 23 | + const client = await createConnectedClient(dev2multiaddr); |
| 24 | + |
| 25 | + let modulesList = await getModules(client); |
| 26 | + |
| 27 | + expect(modulesList).not.toBeUndefined; |
| 28 | + }); |
| 29 | + |
| 30 | + it('get_interfaces', async function () { |
| 31 | + const client = await createConnectedClient(dev2multiaddr); |
| 32 | + |
| 33 | + let interfaces = await getInterfaces(client); |
| 34 | + |
| 35 | + expect(interfaces).not.toBeUndefined; |
| 36 | + }); |
| 37 | + |
| 38 | + it('get_blueprints', async function () { |
| 39 | + const client = await createConnectedClient(dev2multiaddr); |
| 40 | + |
| 41 | + let bpList = await getBlueprints(client); |
| 42 | + |
| 43 | + expect(bpList).not.toBeUndefined; |
| 44 | + }); |
| 45 | + |
| 46 | + it('upload_modules', async function () { |
| 47 | + const client = await createConnectedClient(dev2multiaddr); |
| 48 | + |
| 49 | + console.log('peerid: ' + client.selfPeerId); |
| 50 | + |
| 51 | + let config: ModuleConfig = { |
| 52 | + name: 'test_broken_module', |
| 53 | + mem_pages_count: 100, |
| 54 | + logger_enabled: true, |
| 55 | + wasi: { |
| 56 | + envs: { a: 'b' }, |
| 57 | + preopened_files: ['a', 'b'], |
| 58 | + mapped_dirs: { c: 'd' }, |
| 59 | + }, |
| 60 | + mounted_binaries: { e: 'f' }, |
| 61 | + }; |
| 62 | + |
| 63 | + let base64 = 'MjNy'; |
| 64 | + |
| 65 | + await uploadModule(client, 'test_broken_module', base64, config); |
| 66 | + }); |
| 67 | + |
| 68 | + it('add_blueprint', async function () { |
| 69 | + const client = await createConnectedClient(dev2multiaddr); |
| 70 | + |
| 71 | + let bpId = 'some'; |
| 72 | + |
| 73 | + let bpIdReturned = await addBlueprint(client, 'test_broken_blueprint', ['test_broken_module'], bpId); |
| 74 | + |
| 75 | + expect(bpIdReturned).toEqual(bpId); |
| 76 | + }); |
| 77 | + |
| 78 | + // FIXME:: there is no error on broken blueprint from a node |
| 79 | + it.skip('create_service', async function () { |
| 80 | + const client = await createConnectedClient(dev2multiaddr); |
| 81 | + |
| 82 | + let serviceId = await createService(client, 'test_broken_blueprint'); |
| 83 | + |
| 84 | + // TODO there is no error on broken blueprint from a node |
| 85 | + expect(serviceId).not.toBeUndefined; |
| 86 | + }); |
| 87 | + |
| 88 | + it('add_provider', async function () { |
| 89 | + const client = await createConnectedClient(dev2multiaddr); |
| 90 | + |
| 91 | + let key = Math.random().toString(36).substring(7); |
| 92 | + let buf = Buffer.from(key); |
| 93 | + |
| 94 | + let r = Math.random().toString(36).substring(7); |
| 95 | + await addProvider(client, buf, dev2peerId, r); |
| 96 | + |
| 97 | + let pr = await getProviders(client, buf); |
| 98 | + console.log(pr); |
| 99 | + console.log(r); |
| 100 | + expect(r).toEqual(pr[0][0].service_id); |
| 101 | + }); |
| 102 | + |
| 103 | + it('add and remove script', async function () { |
| 104 | + const client = await createConnectedClient(dev3multiaddr); |
| 105 | + |
| 106 | + console.log('peerid: ' + client.selfPeerId); |
| 107 | + |
| 108 | + let script = ` |
| 109 | + (seq |
| 110 | + (call "${client.relayPeerId}" ("op" "identity") []) |
| 111 | + (call "${client.selfPeerId}" ("test" "test1") ["1" "2" "3"] result) |
| 112 | + ) |
| 113 | + `; |
| 114 | + |
| 115 | + let resMakingPromise = new Promise((resolve) => { |
| 116 | + client.registerCallback('test', 'test1', (args, _) => { |
| 117 | + resolve([...args]); |
| 118 | + return {}; |
| 119 | + }); |
| 120 | + }); |
| 121 | + |
| 122 | + let scriptId = await addScript(client, script); |
| 123 | + |
| 124 | + await resMakingPromise |
| 125 | + .then((args) => { |
| 126 | + console.log('final!'); |
| 127 | + expect(args as string[]).toEqual(['1', '2', '3']); |
| 128 | + }) |
| 129 | + .finally(() => { |
| 130 | + removeScript(client, scriptId); |
| 131 | + }); |
| 132 | + |
| 133 | + expect(scriptId).not.toBeUndefined; |
| 134 | + }); |
| 135 | +}); |
0 commit comments