Skip to content

Commit 6e6486d

Browse files
authored
Merge pull request #908 from IntersectMBO/cardano-wasm-playwright
Add playwright test to `cardano-wasm` to ensure it works in browsers
2 parents 715d7ef + 2f4f1cf commit 6e6486d

File tree

5 files changed

+92
-19
lines changed

5 files changed

+92
-19
lines changed

.github/workflows/haskell-wasm.yml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,21 @@ jobs:
142142
run: |
143143
wasm32-wasi-cabal build cardano-wasm --no-semaphore -j1 --ghc-options="-j1"
144144
145+
- name: Prepare example
146+
run: |
147+
cp $(env -u CABAL_CONFIG wasm32-wasi-cabal list-bin exe:cardano-wasm | tail -n1) cardano-wasm/example/
148+
$(wasm32-wasi-ghc --print-libdir)/post-link.mjs -i "$(env -u CABAL_CONFIG wasm32-wasi-cabal list-bin exe:cardano-wasm | tail -n1)" -o cardano-wasm/example/cardano-wasm.js
149+
cp cardano-wasm/lib-wrapper/* cardano-wasm/example/
150+
151+
- uses: rrbutani/use-nix-shell-action@v1
152+
with:
153+
devShell: .#playwright
154+
155+
- name: Run playwright test in example
156+
run: |
157+
httpserver -h localhost -a 127.0.0.1 -p 8080 cardano-wasm/example &
158+
playwright test cardano-wasm/js-test/basic-test.spec.ts
159+
145160
# - name: Run tests
146161
# env:
147162
# TMPDIR: ${{ runner.temp }}

cardano-wasm/example/example.js

Lines changed: 43 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,30 +9,61 @@ async function get_protocol_params() {
99

1010
let protocolParams = await get_protocol_params();
1111

12+
const output = document.createElement("code");
13+
output.innerText = "";
14+
output.id = "test-output";
15+
document.body.appendChild(output);
16+
17+
function log(out) {
18+
console.log(out);
19+
if (typeof(out) == "object") {
20+
output.innerText += "> [object] {\n";
21+
for (let [key, val] of Object.entries(out)) {
22+
let text = val.toString();
23+
if (typeof(val) == "function") {
24+
text = text.split("{")[0];
25+
}
26+
output.innerText += "    " + key + ": " + text + "\n";
27+
}
28+
output.innerText += "  }\n";
29+
} else {
30+
output.innerText += "> " + JSON.stringify(out) + "\n";
31+
}
32+
}
33+
34+
function finish_test() {
35+
let finishTag = document.createElement("p");
36+
finishTag.innerText = "Finished test!";
37+
finishTag.id = "finish-tag";
38+
document.body.appendChild(finishTag);
39+
}
40+
1241
async function do_async_work() {
1342
let api = await promise;
14-
console.log("Api object:");
15-
console.log(api);
43+
log("Api object:");
44+
log(api);
1645

1746
let emptyTx = await api.newConwayTx();
18-
console.log("UnsignedTx object:");
19-
console.log(emptyTx);
47+
log("UnsignedTx object:");
48+
log(emptyTx);
2049

2150
let tx = await emptyTx.addTxInput("be6efd42a3d7b9a00d09d77a5d41e55ceaf0bd093a8aa8a893ce70d9caafd978", 0)
22-
.addSimpleTxOut("addr_test1vzpfxhjyjdlgk5c0xt8xw26avqxs52rtf69993j4tajehpcue4v2v", 10_000_000n)
51+
.addSimpleTxOut("addr_test1vzpfxhjyjdlgk5c0xt8xw26avqxs52rtf69993j4tajehpcue4v2v", 10_000_000n)
2352

2453
let feeEstimate = await tx.estimateMinFee(protocolParams, 1, 0, 0);
25-
console.log("Estimated fee:");
26-
console.log(feeEstimate);
54+
log("Estimated fee:");
55+
log(feeEstimate);
2756

2857
let signedTx = await tx.setFee(feeEstimate)
29-
.signWithPaymentKey("addr_sk1648253w4tf6fv5fk28dc7crsjsaw7d9ymhztd4favg3cwkhz7x8sl5u3ms");
30-
console.log("SignedTx object:");
31-
console.log(signedTx);
58+
.signWithPaymentKey("addr_sk1648253w4tf6fv5fk28dc7crsjsaw7d9ymhztd4favg3cwkhz7x8sl5u3ms");
59+
log("SignedTx object:");
60+
log(signedTx);
3261

3362
let txCbor = await signedTx.txToCbor();
34-
console.log("Tx CBOR:");
35-
console.log(txCbor);
63+
log("Tx CBOR:");
64+
log(txCbor);
65+
66+
finish_test();
3667
}
3768

3869
do_async_work().then(() => { });

cardano-wasm/example/index.html

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
<html>
2+
<head>
3+
<title>cardano-wasm test</title>
4+
</head>
5+
<body>
6+
<script type="module" src="./example.js"></script>
7+
<h1>Test output</h1>
8+
</body>
29

3-
<body>
4-
<script type="module" src="./example.js"></script>
5-
</body>
6-
7-
</html>
10+
</html>
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { test, expect } from '@playwright/test';
2+
3+
test('test output matches', async ({ page }) => {
4+
await page.goto('http://localhost:8080');
5+
await expect(page).toHaveTitle(/cardano-wasm test/);
6+
await expect(page.locator('#finish-tag')).toHaveText("Finished test!");
7+
await expect(page.locator('#test-output')).toHaveText('> "Api object:"> [object] { objectType: cardano-api newConwayTx: async function (...args) }> "UnsignedTx object:"> [object] { objectType: UnsignedTx addTxInput: function(txId,txIx) addSimpleTxOut: function(destAddr,lovelaceAmount) setFee: function(lovelaceAmount) estimateMinFee: function(protocolParams,numKeyWitnesses,numByronKeyWitnesses,totalRefScriptSize) signWithPaymentKey: function(signingKey) }> "Estimated fee:"> 164005> "SignedTx object:"> [object] { objectType: SignedTx alsoSignWithPaymentKey: function(signingKey) txToCbor: function() }> "Tx CBOR:"> "84a300d9010281825820be6efd42a3d7b9a00d09d77a5d41e55ceaf0bd093a8aa8a893ce70d9caafd97800018182581d6082935e44937e8b530f32ce672b5d600d0a286b4e8a52c6555f659b871a00989680021a000280a5a100d9010281825820adfc1c30385916da87db1ba3328f0690a57ebb2a6ac9f6f86b2d97f943adae005840a49259b5977aea523b46f01261fbff93e0899e8700319e11f5ab96b67eb628fca1a233ce2d50ee3227b591b84f27237d920d63974d65728362382f751c4d9400f5f6"');
8+
});
9+

flake.nix

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -207,19 +207,34 @@
207207
];
208208
};
209209
};
210+
playwrightShell = let
211+
playwright-pkgs = inputs.nixpkgs.legacyPackages.${system};
212+
in {
213+
playwright = playwright-pkgs.mkShell {
214+
packages = [
215+
playwright-pkgs.playwright-test
216+
playwright-pkgs.python313Packages.docopt
217+
playwright-pkgs.python313Packages.httpserver
218+
];
219+
};
220+
};
210221
flakeWithWasmShell = nixpkgs.lib.recursiveUpdate flake {
211222
devShells = wasmShell;
212223
hydraJobs = {devShells = wasmShell;};
213224
};
225+
flakeWithPlaywrightShell = nixpkgs.lib.recursiveUpdate flakeWithWasmShell {
226+
devShells = playwrightShell;
227+
hydraJobs = {devShells = playwrightShell;};
228+
};
214229
in
215-
nixpkgs.lib.recursiveUpdate flakeWithWasmShell rec {
230+
nixpkgs.lib.recursiveUpdate flakeWithPlaywrightShell rec {
216231
project = cabalProject;
217232
# add a required job, that's basically all hydraJobs.
218233
hydraJobs =
219234
nixpkgs.callPackages inputs.iohkNix.utils.ciJobsAggregates
220235
{
221236
ciJobs =
222-
flakeWithWasmShell.hydraJobs
237+
flakeWithPlaywrightShell.hydraJobs
223238
// {
224239
# This ensure hydra send a status for the required job (even if no change other than commit hash)
225240
revision = nixpkgs.writeText "revision" (inputs.self.rev or "dirty");

0 commit comments

Comments
 (0)