-
Notifications
You must be signed in to change notification settings - Fork 70
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #263 from petarvujovic98/develop
[RFC] feat: Middlewares addition
- Loading branch information
Showing
12 changed files
with
268 additions
and
15 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,35 @@ | ||
import childProcess from "child_process"; | ||
import { promisify } from "util"; | ||
import signal from "signale" | ||
|
||
const {Signale} = signal; | ||
|
||
const exec = promisify(childProcess.exec); | ||
|
||
export async function executeCommand( | ||
command: string, | ||
verbose = false | ||
): Promise<string> { | ||
const signale = new Signale({scope: "exec", interactive: !verbose}) | ||
|
||
if (verbose) { | ||
console.log(command); | ||
signale.info(`Running command: ${command}`); | ||
} | ||
|
||
try { | ||
const { stdout, stderr } = await exec(command); | ||
|
||
if (stderr && verbose) { | ||
console.error(stderr); | ||
signale.error(stderr); | ||
} | ||
|
||
if (verbose) { | ||
console.log(stdout); | ||
signale.info(`Command output: ${stdout}`); | ||
} | ||
|
||
return stdout.trim(); | ||
} catch (error) { | ||
console.log(error); | ||
signale.error(error); | ||
process.exit(1); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
import { Worker } from "near-workspaces"; | ||
import test from "ava"; | ||
|
||
test.beforeEach(async (t) => { | ||
// Init the worker and start a Sandbox server | ||
const worker = await Worker.init(); | ||
|
||
// Prepare sandbox for tests, create accounts, deploy contracts, etx. | ||
const root = worker.rootAccount; | ||
|
||
// Deploy the contract. | ||
const middlewares = await root.devDeploy("build/middlewares.wasm"); | ||
|
||
// Create the init args. | ||
const args = JSON.stringify({ randomData: "anything" }); | ||
// Capture the result of the init function call. | ||
const result = await middlewares.callRaw(middlewares, "init", args); | ||
|
||
// Extract the logs. | ||
const { logs } = result.result.receipts_outcome[0].outcome; | ||
// Create the expected logs. | ||
const expectedLogs = [`Log from middleware: ${args}`]; | ||
|
||
// Check for correct logs. | ||
t.deepEqual(logs, expectedLogs); | ||
|
||
// Create test users | ||
const ali = await root.createSubAccount("ali"); | ||
|
||
// Save state for test runs | ||
t.context.worker = worker; | ||
t.context.accounts = { root, middlewares, ali }; | ||
}); | ||
|
||
test.afterEach.always(async (t) => { | ||
await t.context.worker.tearDown().catch((error) => { | ||
console.log("Failed to tear down the worker:", error); | ||
}); | ||
}); | ||
|
||
test("The middleware logs with call functions", async (t) => { | ||
const { ali, middlewares } = t.context.accounts; | ||
|
||
// Create the arguments which will be passed to the function. | ||
const args = JSON.stringify({ id: "1", text: "hello" }); | ||
// Call the function. | ||
const result = await ali.callRaw(middlewares, "add", args); | ||
// Extract the logs. | ||
const { logs } = result.result.receipts_outcome[0].outcome; | ||
// Create the expected logs. | ||
const expectedLogs = [`Log from middleware: ${args}`]; | ||
|
||
t.deepEqual(logs, expectedLogs); | ||
}); | ||
|
||
test("The middleware logs with view functions", async (t) => { | ||
const { ali, middlewares } = t.context.accounts; | ||
|
||
// Create the arguments which will be passed to the function. | ||
const args = JSON.stringify({ id: "1", accountId: "hello" }); | ||
// Call the function. | ||
const result = await ali.callRaw(middlewares, "get", args); | ||
// Extract the logs. | ||
const { logs } = result.result.receipts_outcome[0].outcome; | ||
// Create the expected logs. | ||
const expectedLogs = [`Log from middleware: ${args}`]; | ||
|
||
t.deepEqual(logs, expectedLogs); | ||
}); | ||
|
||
test("The middleware logs with two middleware functions", async (t) => { | ||
const { ali, middlewares } = t.context.accounts; | ||
|
||
// Create the arguments which will be passed to the function. | ||
const args = JSON.stringify({ id: "1", accountId: "hello" }); | ||
// Call the function. | ||
const result = await ali.callRaw(middlewares, "get_two", args); | ||
// Extract the logs. | ||
const { logs } = result.result.receipts_outcome[0].outcome; | ||
// Create the expected logs. | ||
const expectedLogs = [`Log from middleware: ${args}`, "Second log!"]; | ||
|
||
t.deepEqual(logs, expectedLogs); | ||
}); | ||
|
||
test("The middleware logs with private functions", async (t) => { | ||
const { ali, middlewares } = t.context.accounts; | ||
|
||
// Create the arguments which will be passed to the function. | ||
const args = { id: "test", accountId: "tset" }; | ||
// Call the function. | ||
const result = await ali.callRaw(middlewares, "get_private", ""); | ||
// Extract the logs. | ||
const { logs } = result.result.receipts_outcome[0].outcome; | ||
// Create the expected logs. | ||
const expectedLogs = [`Log from middleware: ${args}`]; | ||
|
||
t.deepEqual(logs, expectedLogs); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.