-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathwasmInMem.test.scala
More file actions
113 lines (93 loc) · 4.42 KB
/
Copy pathwasmInMem.test.scala
File metadata and controls
113 lines (93 loc) · 4.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
package io.github.quafadas.sjsls
import mill.api.Discover
import mill.api.Task.Simple
import mill.testkit.TestRootModule
import mill.testkit.UnitTester
import mill.util.TokenReaders.*
import mill.javalib.DepSyntax
import utest.*
object MemWasmTests extends TestSuite:
def tests: Tests = Tests {
test("fastLinkJS WASM output has a hashed wasm file, not hashed JS files") {
object build extends TestRootModule with InMemoryFastLinkHashScalaJSModule:
override def scalaVersion: Simple[String] = "3.8.2"
override def scalaJSExperimentalUseWebAssembly = true
override def mvnDeps = Seq(
mvn"com.raquo::laminar::17.0.0"
)
lazy val millDiscover = Discover[this.type]
end build
val resourceFolder = os.Path(sys.env("MILL_TEST_RESOURCE_DIR"))
UnitTester(build, resourceFolder / "simple").scoped {
eval =>
val Right(result) = eval(build.fastLinkJS).runtimeChecked
val report = result.value
// Files live in-memory, not on disk
val memFiles = build.inMemoryOutputDirectory.fileNames().toSet
// The main entry-point JS file must be renamed to a hashed filename.
assert(!memFiles.contains("main.wasm"))
assert(memFiles.contains("__loader.js"))
assert(memFiles.contains("main.js"))
// The WASM binary must be preserved (not renamed).
if !memFiles.exists(_.endsWith(".wasm")) then
throw new java.lang.AssertionError(s"no .wasm file found in: $memFiles")
end if
// The Report's public module must reference the hashed JS file.
assert(report.publicModules.nonEmpty)
val reported = report.publicModules.head.jsFileName
if reported != "main.js" then
throw new java.lang.AssertionError(
s"report module jsFileName should be main.js, was $reported"
)
end if
}
}
test("InMemoryFastLinkHashScalaJSModule fullLinkJS runs wasm-opt and hashes the optimised binary") {
object build extends TestRootModule with InMemoryFastLinkHashScalaJSModule:
override def scalaVersion: Simple[String] = "3.8.2"
override def scalaJSExperimentalUseWebAssembly = true
override def mvnDeps = Seq(
mvn"com.raquo::laminar::17.0.0"
)
lazy val millDiscover = Discover[this.type]
end build
val resourceFolder = os.Path(sys.env("MILL_TEST_RESOURCE_DIR"))
UnitTester(build, resourceFolder / "simple").scoped {
eval =>
// Run fastLinkJS first to capture the unoptimised wasm size (lives in-memory).
val Right(_) = eval(build.fastLinkJS).runtimeChecked
val fastWasmNames = build.inMemoryOutputDirectory.fileNames().filter(_.endsWith(".wasm"))
if fastWasmNames.size != 1 then
throw new java.lang.AssertionError(s"Expected exactly 1 wasm file after fastLinkJS, got: $fastWasmNames")
end if
val fastBuf = build.inMemoryOutputDirectory.content(fastWasmNames.head).get
val fastWasmSize = fastBuf.remaining().toLong
// Run fullLinkJS: wasm-opt should produce a smaller binary with a different hash.
// fullLinkJS writes file-based output to Task.dest (not in-memory).
val Right(fullResult) = eval(build.fullLinkJS).runtimeChecked
val fullDir = fullResult.value.dest.path
val fullWasmFiles = os.list(fullDir).filter(p => os.isFile(p) && p.ext == "wasm")
if fullWasmFiles.size != 1 then
throw new java.lang.AssertionError(
s"Expected exactly 1 wasm file after fullLinkJS, got: $fullWasmFiles"
)
end if
val fullWasm = fullWasmFiles.head
val fullWasmSize = os.size(fullWasm)
// The name must be content-hashed (base.<hash>.wasm), not the bare original name.
val nameParts = fullWasm.baseName.split('.')
if nameParts.length < 2 then
throw new java.lang.AssertionError(
s"Expected hashed wasm filename like 'main.<hash>.wasm', got: ${fullWasm.last}"
)
end if
// The optimised binary must be strictly smaller.
if fullWasmSize >= fastWasmSize then
throw new java.lang.AssertionError(
s"wasm-opt did not reduce size: fastLinkJS=$fastWasmSize bytes, fullLinkJS=$fullWasmSize bytes"
)
end if
}
}
}
end MemWasmTests