mirror of
https://github.com/fluencelabs/fluence-js.git
synced 2025-06-24 21:31:32 +00:00
Expose parse ast
method from AVM (#161)
This commit is contained in:
33
src/__test__/unit/ast.spec.ts
Normal file
33
src/__test__/unit/ast.spec.ts
Normal file
@ -0,0 +1,33 @@
|
|||||||
|
import { Fluence } from '../../index';
|
||||||
|
|
||||||
|
describe('Parse ast tests', () => {
|
||||||
|
beforeAll(async () => {
|
||||||
|
await Fluence.start();
|
||||||
|
});
|
||||||
|
|
||||||
|
afterAll(async () => {
|
||||||
|
await Fluence.stop();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('Correct ast should be parsed correctly', async function () {
|
||||||
|
const peer = Fluence.getPeer();
|
||||||
|
const air = `(null)`;
|
||||||
|
const res = await peer.internals.parseAst(air);
|
||||||
|
|
||||||
|
expect(res).toStrictEqual({
|
||||||
|
success: true,
|
||||||
|
data: { Null: null },
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
it('Incorrect ast should result in corresponding error', async function () {
|
||||||
|
const peer = Fluence.getPeer();
|
||||||
|
const air = `(null`;
|
||||||
|
const res = await peer.internals.parseAst(air);
|
||||||
|
|
||||||
|
expect(res).toStrictEqual({
|
||||||
|
success: false,
|
||||||
|
data: expect.stringContaining('error'),
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
@ -352,6 +352,34 @@ export class FluencePeer {
|
|||||||
*/
|
*/
|
||||||
get internals() {
|
get internals() {
|
||||||
return {
|
return {
|
||||||
|
parseAst: async (air: string): Promise<{ success: boolean; data: any }> => {
|
||||||
|
const status = this.getStatus();
|
||||||
|
|
||||||
|
if (!status.isInitialized) {
|
||||||
|
new Error("Can't use avm: peer is not initialized");
|
||||||
|
}
|
||||||
|
|
||||||
|
const args = JSON.stringify([air]);
|
||||||
|
const rawRes = await this._fluenceAppService!.callService('avm', 'ast', args, undefined);
|
||||||
|
let res;
|
||||||
|
try {
|
||||||
|
res = JSON.parse(rawRes);
|
||||||
|
res = res.result as string;
|
||||||
|
if (res.startsWith('error')) {
|
||||||
|
return {
|
||||||
|
success: false,
|
||||||
|
data: res,
|
||||||
|
};
|
||||||
|
} else {
|
||||||
|
return {
|
||||||
|
success: true,
|
||||||
|
data: JSON.parse(res),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
} catch (err) {
|
||||||
|
throw new Error('Failed to call avm. Raw result: ' + rawRes + '. Error: ' + err);
|
||||||
|
}
|
||||||
|
},
|
||||||
createNewParticle: (script: string, ttl: number = this._defaultTTL) => {
|
createNewParticle: (script: string, ttl: number = this._defaultTTL) => {
|
||||||
const status = this.getStatus();
|
const status = this.getStatus();
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user