Skip to content

Commit

Permalink
test(fs): add keyholder test case
Browse files Browse the repository at this point in the history
This test case supposed to test for creating public/secret keypairs and saving
them to files.  It was a bit complicated to do so, so we are generating random
string and saving that to a file instead.
  • Loading branch information
denizenging committed Jul 18, 2023
1 parent 1ab135a commit 11d757e
Showing 1 changed file with 31 additions and 0 deletions.
31 changes: 31 additions & 0 deletions pkg/fs/visitor.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import { visit } from './visitor';

import { getIgnoredStatements } from '@slangroom/ignored';
import { zencodeExec } from '@slangroom/shared';

import * as fs from 'node:fs/promises';
import * as path from 'node:path';

test('ast is correct with one statement', async () => {
// Given I have a contract with one filesystems statement in it
Expand Down Expand Up @@ -98,3 +102,30 @@ Then I save the 'stringToWrite2' into the file 'nameOfTheFile2'
// and the value indexed by its filename in data must be data's nameOfTheFile2
expect(data[third.filename as 'nameOfTheFile2']).toStrictEqual(data.nameOfTheFile2);
});

test('keyholder works', async () => {
// Given I have a contract with one filesystems statement in it
const contract = `Rule unknown ignore
Given I have a 'string' named 'nameOfTheFile'
When I create the random object of '64' bits
When I rename the 'random_object' to 'stringToWrite'
Then I save the 'stringToWrite' into the file 'nameOfTheFile'
Then I print the 'stringToWrite'
Then I print the 'nameOfTheFile'
`;
// and the params used in the contract
const params = { data: { nameOfTheFile: 'hello-world.txt' } };
// When I get the ignored statement of it
const ignoreds = await getIgnoredStatements(contract, params);
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
const ast = visit(ignoreds[0]!);
const zout = await zencodeExec(contract, params);
const nameOfTheFile = zout.result[ast.filename] as string;
const stringToWrite = zout.result[ast.content] as string;
await fs.mkdir('/tmp/slangroom/', { recursive: true });
const fh = await fs.open(path.resolve('/tmp/slangroom/', nameOfTheFile), 'w');
const { buffer } = await fh.write(stringToWrite);
expect(buffer).toStrictEqual(stringToWrite);
});

0 comments on commit 11d757e

Please sign in to comment.