Skip to content
This repository was archived by the owner on Aug 1, 2023. It is now read-only.

Commit 9d0e741

Browse files
authored
Add DaedalusIPC nodejs test (#221)
* tests: Add DaedalusIPC spec This uses nodejs to run daedalusIPC. * nix: Update and regenerate
1 parent 353f13b commit 9d0e741

File tree

7 files changed

+92
-8
lines changed

7 files changed

+92
-8
lines changed

cardano-shell.cabal

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
cabal-version: 2.2
12
name: cardano-shell
23
version: 0.1.0.0
34
description: Please see the README on GitHub at <https://github.com/githubuser/cardano-shell#readme>
@@ -10,10 +11,10 @@ copyright: 2018 IOHK
1011
license: MIT
1112
license-file: LICENSE
1213
build-type: Simple
13-
cabal-version: >= 1.10
1414
extra-source-files:
1515
ChangeLog.md
1616
README.md
17+
test/js/mock-daedalus.js
1718

1819
source-repository head
1920
type: git
@@ -208,6 +209,7 @@ test-suite cardano-shell-test
208209
other-modules:
209210
Paths_cardano_shell
210211
DhallConfigSpec
212+
DaedalusIPCSpec
211213
UpdaterSpec
212214
if !os(windows)
213215
other-modules:
@@ -224,6 +226,7 @@ test-suite cardano-shell-test
224226
, cardano-prelude
225227
, dhall
226228
, safe-exceptions
229+
, process
227230
-- quickcheck
228231
, QuickCheck
229232
-- SM
@@ -237,6 +240,8 @@ test-suite cardano-shell-test
237240
, concurrency
238241
, dejafu
239242
, hunit-dejafu
243+
build-tool-depends:
244+
cardano-shell:daedalus-ipc
240245
default-language: Haskell2010
241246
default-extensions: NoImplicitPrelude
242247
OverloadedStrings

nix/.stack.nix/cardano-shell.nix

Lines changed: 5 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

nix/iohk-nix-src.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"url": "https://github.com/input-output-hk/iohk-nix",
3-
"rev": "eec83e9bba05093c94004a001caebae252c0c83b",
4-
"date": "2019-06-26T17:23:10+00:00",
5-
"sha256": "00b11qmgr57pm6gg7a8m6fzjj6m5wmh8hnwg16jnqsmf52882y2j",
3+
"rev": "3cee475f26458ef113a494b216b1504513fa4d03",
4+
"date": "2019-07-05T14:51:43+00:00",
5+
"sha256": "04dqb2ii03y1lvyqn3v3xwardym2h6g5zbgvjljam758w768vmii",
66
"fetchSubmodules": false
77
}

nix/pkgs.nix

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,11 @@ let
3838
# turtle seems to have the same issue.
3939
packages.turtle.doExactConfig = true;
4040
}
41+
42+
{
43+
packages.cardano-shell.src = haskell.cleanSourceHaskell ../.;
44+
packages.cardano-shell.components.tests.cardano-shell-test.build-tools = [ pkgs.nodejs ];
45+
}
4146
];
4247
};
4348

test/DaedalusIPCSpec.hs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
module DaedalusIPCSpec
2+
( spec
3+
) where
4+
5+
import Cardano.Prelude
6+
7+
import System.Exit (ExitCode (..))
8+
import System.Process ( createProcess, proc, waitForProcess)
9+
import Test.Hspec (Spec, describe, it, shouldReturn)
10+
11+
spec :: Spec
12+
spec = describe "DaedalusIPC" $ do
13+
it "should reply with the port when asked" $ do
14+
let port = 42 :: Int
15+
let testScript = "test/js/mock-daedalus.js"
16+
(_, _, _, ph) <- createProcess (proc "node" [testScript, show port])
17+
waitForProcess ph `shouldReturn` ExitSuccess

test/Spec.hs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import Cardano.Shell.Types (CardanoFeature (..))
2020
import DhallConfigSpec (dhallConfigSpec, mkConfigSpec)
2121
import NodeIPCSMSpec (nodeIPCSMSpec)
2222
import NodeIPCSpec (nodeIPCSpec)
23+
import qualified DaedalusIPCSpec as DaedalusIPC
2324
import UpdaterSpec (updaterSpec)
2425

2526
-- | Entry point for tests.
@@ -30,6 +31,7 @@ main = hspec $ do
3031
describe "Cardano configurations" mkConfigSpec
3132
describe "NodeIPC state machine" nodeIPCSMSpec
3233
describe "NodeIPC" nodeIPCSpec
34+
describe "DaedalusIPC" DaedalusIPC.spec
3335
describe "Update system" updaterSpec
3436

3537
-- | A valid concurrency specification.
@@ -146,6 +148,3 @@ concurrencyDSLGenerator = frequency
146148
-- | A delay from one millisecond to ten milliseconds.
147149
generateMillisecondDelay :: Gen Int
148150
generateMillisecondDelay = choose (1, 10)
149-
150-
151-

test/js/mock-daedalus.js

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
#!/usr/bin/env node
2+
3+
// This runs the daedalus-ipc executable in the same way that Daedalus would run cardano-wallet.
4+
// It needs node and daedalus-ipc on the PATH to run.
5+
6+
const child_process = require("child_process");
7+
const http = require('http');
8+
9+
function main() {
10+
let port = process.argv[2];
11+
const proc = child_process.spawn("daedalus-ipc", [port],
12+
{ stdio: ["ignore", "inherit", "inherit", "ipc"] }
13+
);
14+
15+
proc.on("close", function(code, signal) {
16+
console.log("JS: child_process stdio streams closed");
17+
process.exit(2);
18+
});
19+
20+
proc.on("disconnect", function() {
21+
console.log("JS: child_process disconnected");
22+
process.exit(3);
23+
});
24+
25+
proc.on("error", function(err) {
26+
console.log("JS: error child_process: " + err);
27+
process.exit(4);
28+
});
29+
30+
proc.on("exit", function(code, signal) {
31+
console.log("JS: child_process exited with status " + code + " or signal " + signal);
32+
process.exit(5);
33+
});
34+
35+
proc.on("message", function(msg) {
36+
console.log("JS: message received", msg);
37+
// See CardanoNode.js in Daedalus for the message types in use.
38+
if (msg.Started) {
39+
console.log("JS: sending a bogus message");
40+
proc.send("hello");
41+
} else if (msg.ParseError && msg.ParseError.match(/encountered String/)) {
42+
console.log("JS: sending QueryPort");
43+
proc.send({ QueryPort: [] });
44+
} else if (msg.ParseError) {
45+
console.log("JS: i did not expect that");
46+
process.exit(6);
47+
} else if (msg.ReplyPort) {
48+
console.log("JS: received ReplyPort " + msg.ReplyPort + ", expecting " + port);
49+
process.exit(msg.ReplyPort == port ? 0 : 1);
50+
}
51+
});
52+
}
53+
54+
main();

0 commit comments

Comments
 (0)