Skip to content

An open-source, self-hosted system for apps built with web technologies.

Simplify contract verification

zohayrslileh016a05b17bfb
diff --git a/package.json b/package.json
index cbf5786..43cd7da 100644
--- a/package.json
+++ b/package.json
@@ -30,7 +30,7 @@
"build": "vite-node scripts/build.ts",
"start": "node --run build && node dist/server/main.js",
"pack": "node --run build && vite-node scripts/pack.ts",
- "verify:contracts": "vite-node scripts/verify-client-launch.mjs && vite-node scripts/verify-process-find-or-create.mjs && vite-node scripts/verify-client-surface.mjs && vite-node scripts/verify-window-geometry.mjs && vite-node scripts/verify-readiness.mjs && vite-node scripts/verify-storage.mjs && vite-node scripts/verify-uploads.mjs && vite-node scripts/verify-endpoint-services.mjs && vite-node scripts/verify-program-install.mjs && vite-node scripts/verify-host-traffic.mjs && vite-node scripts/verify-gateway-address.mjs && vite-node scripts/verify-gateway.mjs && vite-node scripts/verify-appearance.mjs && vite-node scripts/verify-worker-runtime.mjs",
+ "verify:contracts": "vite-node scripts/verify-contracts.ts",
"verify:distribution": "node scripts/verify-distribution.mjs",
"verify": "node --run check && node --run verify:contracts && node --run verify:distribution"
},
diff --git a/scripts/verify-appearance.mjs b/scripts/contracts/appearance.ts
similarity index 65%
rename from scripts/verify-appearance.mjs
rename to scripts/contracts/appearance.ts
index 79bbb7c..ccdc83d 100644
--- a/scripts/verify-appearance.mjs
+++ b/scripts/contracts/appearance.ts
@@ -1,7 +1,12 @@
import assert from "node:assert/strict"
import Keyv from "keyv"
import { standardAppearance } from "@phreshos/core"
-import AppearanceManager, { appearanceSchema } from "../source/server/core/appearance-manager.ts"
+import AppearanceManager, { appearanceSchema } from "@server/core/appearance-manager"
+import FileManager from "@libs/file-manager"
+import UploadManager from "@server/core/upload-manager"
+import { mkdtemp, rm } from "node:fs/promises"
+import { tmpdir } from "node:os"
+import { join } from "node:path"
assert.deepEqual(appearanceSchema.parse(standardAppearance), standardAppearance)
assert.throws(() => appearanceSchema.parse({}))
@@ -15,7 +20,8 @@ assert.throws(() => appearanceSchema.parse({
}))
const store = new Keyv()
-const manager = await AppearanceManager.open(store, { describe() {} })
+const directory = await mkdtemp(join(tmpdir(), "phresh-appearance-"))
+const manager = await AppearanceManager.open(store, new UploadManager(new FileManager(directory)))
assert.deepEqual(manager.value, standardAppearance)
assert(Object.isFrozen(manager.value))
@@ -30,3 +36,5 @@ await manager.update(updated)
assert.deepEqual(manager.value.background, updated.background)
assert.deepEqual(await store.get("appearance:background"), updated.background)
assert.equal(await store.get("appearance:theme"), undefined)
+
+await rm(directory, { recursive: true, force: true })
diff --git a/scripts/contracts/client-launch.ts b/scripts/contracts/client-launch.ts
new file mode 100644
index 0000000..abf0bdc
--- /dev/null
+++ b/scripts/contracts/client-launch.ts
@@ -0,0 +1,27 @@
+import assert from "node:assert/strict"
+import ProgramManager, { type LaunchClient } from "@server/core/link-manager/auth-manager/program-manager/program-manager"
+import type Program from "@server/core/link-manager/auth-manager/program-manager/program"
+
+const manager = {
+ authManager: {
+ processManager: {
+ processes: new Map()
+ }
+ }
+}
+
+const program = {
+ client: {},
+ clientLocation: "/client",
+ title: "Declared title"
+}
+
+const shape = (overrides: LaunchClient) => ProgramManager.prototype.clientShape.call(
+ manager as unknown as ProgramManager,
+ program as unknown as Program,
+ overrides
+)
+
+assert.equal(shape({ title: "Fetched title" }).title, "Fetched title")
+assert.equal(shape({}).title, "Declared title")
+assert.throws(() => shape({ title: 42 } as unknown as LaunchClient), /title must be text/)
diff --git a/scripts/verify-client-surface.mjs b/scripts/contracts/client-surface.ts
similarity index 67%
rename from scripts/verify-client-surface.mjs
rename to scripts/contracts/client-surface.ts
index 6fcf549..e8387de 100644
--- a/scripts/verify-client-surface.mjs
+++ b/scripts/contracts/client-surface.ts
@@ -1,10 +1,13 @@
import assert from "node:assert/strict"
-import ClientProcessBoundary from "../source/client/view/components/desktop-host/client-process-boundary.ts"
-import ClientProcessManager from "../source/client/core/link-manager/auth-manager/process-manager/process-manager.ts"
-import { visibilityTransition, visualTransaction } from "../source/client/view/components/desktop-host/local-window.ts"
-import host from "../source/client/view/components/desktop-host/host.ts"
-import LocalWindows from "../source/client/view/components/window-manager/local-windows.ts"
-import ServerWindow from "../source/server/core/link-manager/auth-manager/process-manager/window.ts"
+import ClientProcessBoundary from "@client/view/components/desktop-host/client-process-boundary"
+import ClientProcessManager from "@client/core/link-manager/auth-manager/process-manager/process-manager"
+import { visibilityTransition, visualTransaction } from "@client/view/components/desktop-host/local-window"
+import host from "@client/view/components/desktop-host/host"
+import LocalWindows from "@client/view/components/window-manager/local-windows"
+import type { LocalWindowEntry } from "@client/view/components/window-manager/local-windows"
+import ServerWindow from "@server/core/link-manager/auth-manager/process-manager/window"
+import type { LocalWindowState } from "@client/view/components/desktop-host/local-window"
+import type { Transaction, WindowLayer } from "@phreshos/core"
const authoritativeWindow = new ServerWindow(
{ title: "Target", url: null, layer: "over", location: "/" },
@@ -31,10 +34,10 @@ const bare = client("over")
const clients = new Map([
["ordinary", { identity: "ordinary:0", client: ordinary }],
["bare", { identity: "bare:0", client: bare }]
-])
+]) as unknown as ReadonlyMap<string, LocalWindowEntry>
const byProcess = new Map([["ordinary", ordinary], ["bare", bare]])
-const first = new LocalWindows(clients, identity => byProcess.get(identity) ?? null)
-const second = new LocalWindows(clients, identity => byProcess.get(identity) ?? null)
+const first = new LocalWindows(clients, identity => byProcess.get(identity) as never ?? null)
+const second = new LocalWindows(clients, identity => byProcess.get(identity) as never ?? null)
await first.move("ordinary", { x: 40, y: 50 })
await first.move("bare", { x: 60, y: 70 })
@@ -70,10 +73,10 @@ assert.deepEqual(first.state("ordinary").position, { x: 15, y: 25 })
assert.deepEqual(first.state("bare").position, { x: 60, y: 70 })
const matchingAuthority = first.move("ordinary", { x: 30, y: 40 }, { duration: 120, wait: true })
-const matchingRevision = first.windows.get("ordinary:0").geometryAnimation.revision
+const matchingRevision = represented(first, "ordinary:0").geometryAnimation!.revision
ordinary.window.position = { x: 30, y: 40 }
first.reconcile(clients)
-assert.equal(first.windows.get("ordinary:0").geometryAnimation.revision, matchingRevision)
+assert.equal(represented(first, "ordinary:0").geometryAnimation!.revision, matchingRevision)
first.complete("ordinary", "geometry", matchingRevision)
await matchingAuthority
@@ -81,38 +84,40 @@ const waiting = first.geometry("bare", {
position: { x: 80, y: 90 },
size: { width: 320, height: 240 }
}, transaction)
-const geometryRevision = first.windows.get("bare:0").geometryAnimation.revision
+const geometryRevision = represented(first, "bare:0").geometryAnimation!.revision
first.complete("bare", "geometry", geometryRevision)
await waiting
-assert.equal(first.windows.get("bare:0").geometryAnimation, null)
+assert.equal(represented(first, "bare:0").geometryAnimation, null)
const interrupted = first.move("bare", { x: 100, y: 110 }, { duration: 200, wait: true })
await first.move("bare", { x: 120, y: 130 })
await assert.rejects(interrupted, /interrupted/)
const surfaceWaiting = first.setSurface("bare", visibility)
-const surfaceRevision = first.windows.get("bare:0").surface.transition.revision
+const surfaceRevision = surface(first, "bare:0").transition!.revision
first.complete("bare", "surface", surfaceRevision)
await surfaceWaiting
-assert.equal(first.windows.get("bare:0").surface.transition, null)
-assert.equal(first.windows.get("bare:0").surface.visible, true)
+assert.equal(surface(first, "bare:0").transition, null)
+assert.equal(surface(first, "bare:0").visible, true)
const surfaceRemoval = first.removeSurface("bare", visibility)
-const removalRevision = first.windows.get("bare:0").surface.transition.revision
-assert.equal(first.windows.get("bare:0").surface.visible, false)
+const removalRevision = surface(first, "bare:0").transition!.revision
+assert.equal(surface(first, "bare:0").visible, false)
first.complete("bare", "surface", removalRevision)
await surfaceRemoval
-assert.equal(first.windows.get("bare:0").surface, null)
+assert.equal(represented(first, "bare:0").surface, null)
assert.throws(() => first.setSurface("ordinary", visibility), /window-layer/)
-assert.equal(second.windows.get("bare:0").surface, null)
+assert.equal(represented(second, "bare:0").surface, null)
first.release("bare")
assert.deepEqual(first.state("bare").position, bare.window.position)
-assert.equal(first.windows.get("bare:0").surface, null)
+assert.equal(represented(first, "bare:0").surface, null)
const removed = first.move("bare", { x: 140, y: 150 }, { duration: 200, wait: true })
-first.reconcile(new Map([["ordinary", clients.get("ordinary")]]))
+const remaining = clients.get("ordinary")
+assert(remaining)
+first.reconcile(new Map([["ordinary", remaining]]))
await assert.rejects(removed, /representation was removed/)
const requester = {
@@ -128,12 +133,12 @@ const target = {
client: { window: { process: "target", layer: "over", position: { x: 10, y: 20 } } }
}
const processes = new Map([[requester.identity, requester], [target.identity, target]])
-const calls = []
+const calls: unknown[][] = []
const localWindow = {
- state(identity) { return { position: identity === "target" ? { x: 70, y: 80 } : { x: 0, y: 0 } } },
- move(identity, value, motion) { calls.push(["move", identity, value, motion]) },
- setSurface(identity, motion) { calls.push(["set", identity, motion]) },
- removeSurface(identity, motion) { calls.push(["remove", identity, motion]) }
+ state(identity: string) { return { position: identity === "target" ? { x: 70, y: 80 } : { x: 0, y: 0 } } },
+ move(identity: string, value: unknown, motion: Transaction | undefined) { calls.push(["move", identity, value, motion]) },
+ setSurface(identity: string, motion: Transaction) { calls.push(["set", identity, motion]) },
+ removeSurface(identity: string, motion: Transaction) { calls.push(["remove", identity, motion]) }
}
const processManager = {
processes,
@@ -147,7 +152,7 @@ const authManager = {
processManager,
programManager: { programs: new Map() }
}
-const request = host(authManager, requester.identity, () => ({ width: 1, height: 1 }), () => "owner", {}, localWindow)
+const request = host(authManager as never, requester.identity, () => ({ width: 1, height: 1 }), () => "owner", {} as never, localWindow as never)
assert.deepEqual(await request("desktop"), [{ width: 1, height: 1 }])
const targetAddress = { identity: target.identity, reference: target.reference }
@@ -165,15 +170,15 @@ assert.deepEqual(target.client.window.position, { x: 10, y: 20 })
await assert.rejects(request("windowLocalSurfaceSet", targetAddress, { identity: "unexpected" }), /no "identity" field/)
await assert.rejects(request("windowLocalSurfaceSet", { ...targetAddress, reference: "wrong" }, visibility), /does not know this process/)
-const lifecycle = []
+const lifecycle: string[] = []
const boundary = new ClientProcessBoundary(
"requester",
- { contentWindow: null },
- authManager,
+ { contentWindow: null } as unknown as HTMLIFrameElement,
+ authManager as never,
() => ({ width: 1, height: 1 }),
- {},
- {},
- { release(identity) { lifecycle.push(identity) } }
+ {} as never,
+ {} as never,
+ { release(identity: string) { lifecycle.push(identity) } } as never
)
await boundary.own("first-owner")
@@ -182,7 +187,7 @@ await boundary.release()
assert.deepEqual(lifecycle, ["requester", "requester"])
-function client(layer) {
+function client(layer: WindowLayer) {
return {
window: {
title: "Window",
@@ -195,3 +200,15 @@ function client(layer) {
}
}
}
+
+function represented(windows: LocalWindows, identity: string): LocalWindowState {
+ const value = windows.windows.get(identity)
+ assert(value)
+ return value
+}
+
+function surface(windows: LocalWindows, identity: string) {
+ const value = represented(windows, identity).surface
+ assert(value)
+ return value
+}
diff --git a/scripts/verify-endpoint-services.mjs b/scripts/contracts/endpoint-services.ts
similarity index 87%
rename from scripts/verify-endpoint-services.mjs
rename to scripts/contracts/endpoint-services.ts
index 888ba76..2698ef9 100644
--- a/scripts/verify-endpoint-services.mjs
+++ b/scripts/contracts/endpoint-services.ts
@@ -1,6 +1,8 @@
import assert from "node:assert/strict"
-import EndpointServices from "../source/server/core/link-manager/auth-manager/process-manager/endpoint-services.ts"
-import Program from "../source/server/core/link-manager/auth-manager/program-manager/program.ts"
+import EndpointServices from "@server/core/link-manager/auth-manager/process-manager/endpoint-services"
+import Program from "@server/core/link-manager/auth-manager/program-manager/program"
+import type Process from "@server/core/link-manager/auth-manager/process-manager/process"
+import type { ServiceKey } from "@phreshos/core"
const privateProgram = new Program({
identity: "private-program",
@@ -21,22 +23,22 @@ assert.throws(() => new Program({
server: { location: ".", startCommand: "true" }
}), /agent documentation/)
-function process(reference, program = "program") {
+function process(reference: string, program = "program") {
return {
reference,
program: { identity: program },
server: {},
client: {}
- }
+ } as unknown as Process
}
const services = new EndpointServices()
const provider = process("provider")
const conflicting = process("conflicting")
const independent = process("independent", "independent")
-const key = { program: "program", endpoint: "server", name: "counter" }
-const lifecycle = []
-const publications = []
+const key = { program: "program", endpoint: "server", name: "counter" } satisfies ServiceKey
+const lifecycle: unknown[] = []
+const publications: unknown[] = []
let disableTransportCompleted = false
assert.equal(services.identity(key), services.identity({ ...key }))
@@ -64,7 +66,7 @@ assert.deepEqual(lifecycle, ["enable"])
await services.waitReady(key, 0)
// Subscriptions are future-only: joining after enable does not replay it.
-const lateLifecycle = []
+const lateLifecycle: unknown[] = []
services.follow(key, "lifecycle", null, event => lateLifecycle.push(event))
assert.deepEqual(lateLifecycle, [])
diff --git a/scripts/verify-gateway-address.mjs b/scripts/contracts/gateway-address.ts
similarity index 88%
rename from scripts/verify-gateway-address.mjs
rename to scripts/contracts/gateway-address.ts
index f25699a..a5043e4 100644
--- a/scripts/verify-gateway-address.mjs
+++ b/scripts/contracts/gateway-address.ts
@@ -1,6 +1,6 @@
import assert from "node:assert/strict"
import { join } from "node:path"
-import gatewayAddress from "../source/server/view/gateway/address.ts"
+import gatewayAddress from "@server/view/gateway/address"
const first = gatewayAddress("C:\\Users\\Person\\.phreshos", "win32")
diff --git a/scripts/verify-gateway.mjs b/scripts/contracts/gateway.ts
similarity index 80%
rename from scripts/verify-gateway.mjs
rename to scripts/contracts/gateway.ts
index b01ad6a..6595a2d 100644
--- a/scripts/verify-gateway.mjs
+++ b/scripts/contracts/gateway.ts
@@ -2,13 +2,14 @@ import assert from "node:assert/strict"
import { mkdtemp, rm } from "node:fs/promises"
import { connect } from "node:net"
import { join, resolve } from "node:path"
-import gateway from "../source/server/view/gateway/gateway.ts"
+import gateway from "@server/view/gateway/gateway"
+import type Application from "@server/core/application"
const directory = await mkdtemp(resolve(".verify-gateway-"))
const path = join(directory, "gateway.sock")
const application = {
systemControl: {
- async execute(request) { return { received: request } }
+ async execute(request: unknown) { return { received: request } }
},
linkManager: {
authManager: {
@@ -17,7 +18,7 @@ const application = {
async exit() {}
},
programManager: {
- async *installSource(program) {
+ async *installSource(program: { identity: string, name: string, version: string }) {
yield {
stage: "installed",
replaced: false,
@@ -32,7 +33,7 @@ const application = {
}
}
-const server = await gateway(application, path)
+const server = await gateway(application as unknown as Application, path)
try {
const system = await exchange(path, { target: "system", request: { capability: "program", operation: "list", input: {} } })
@@ -54,8 +55,8 @@ try {
await rm(directory, { recursive: true, force: true })
}
-async function exchange(path, request) {
- return await new Promise((resolve, reject) => {
+async function exchange(path: string, request: unknown): Promise<unknown[]> {
+ return await new Promise<unknown[]>((resolve, reject) => {
const socket = connect(path)
let buffer = ""
diff --git a/scripts/verify-host-traffic.mjs b/scripts/contracts/host-traffic.ts
similarity index 84%
rename from scripts/verify-host-traffic.mjs
rename to scripts/contracts/host-traffic.ts
index 786d000..a76212a 100644
--- a/scripts/verify-host-traffic.mjs
+++ b/scripts/contracts/host-traffic.ts
@@ -1,11 +1,11 @@
import assert from "node:assert/strict"
-import HostTraffic from "../source/server/core/link-manager/auth-manager/process-manager/host-traffic.ts"
+import HostTraffic from "@server/core/link-manager/auth-manager/process-manager/host-traffic"
const traffic = new HostTraffic()
-const hostProgram = []
-const ownProgram = []
-const hostProcess = []
-const programProcess = []
+const hostProgram: unknown[][] = []
+const ownProgram: unknown[][] = []
+const hostProcess: unknown[][] = []
+const programProcess: unknown[][] = []
traffic.observe("program", "uninstall", null, (_delivery, event, ...values) => hostProgram.push([event, ...values]))
traffic.observe("program", "uninstall", "program-reference", (_delivery, event, ...values) => ownProgram.push([event, ...values]))
diff --git a/scripts/verify-process-find-or-create.mjs b/scripts/contracts/process-find-or-create.ts
similarity index 55%
rename from scripts/verify-process-find-or-create.mjs
rename to scripts/contracts/process-find-or-create.ts
index 4b60f76..bba6019 100644
--- a/scripts/verify-process-find-or-create.mjs
+++ b/scripts/contracts/process-find-or-create.ts
@@ -1,7 +1,20 @@
import assert from "node:assert/strict"
-import ProgramManager from "../source/server/core/link-manager/auth-manager/program-manager/program-manager.ts"
+import ProgramManager, { type Launch } from "@server/core/link-manager/auth-manager/program-manager/program-manager"
+import type Program from "@server/core/link-manager/auth-manager/program-manager/program"
-const processes = new Map()
+interface FixtureProcess {
+ identity: string
+ name: string | null
+ program: Program
+ launch: unknown
+}
+
+interface FixtureManager {
+ start(owner: Program, launch: Launch, watching?: unknown, parent?: unknown, transition?: boolean, prepared?: { intent: unknown }): Promise<`${string}-${string}-${string}-${string}-${string}`>
+ resolveLaunch(owner: Program, launch: Launch): { intent: { client: { position: unknown, size: unknown } | null } }
+}
+
+const processes = new Map<string, FixtureProcess>()
const program = {
identity: "example",
server: { start: true },
@@ -15,7 +28,7 @@ const program = {
title: "Example",
clientLocation: "/",
async validate() {}
-}
+} as unknown as Program
let starts = 0
@@ -23,21 +36,22 @@ const manager = Object.assign(Object.create(ProgramManager.prototype), {
authManager: { processManager: { processes } },
creating: new Map(),
$outbound: { async publish() {} },
- reach: identity => identity === program.identity ? program : null
-})
+ reach: (identity: string) => identity === program.identity ? program : null
+}) as ProgramManager
+const fixture = manager as unknown as FixtureManager
-manager.start = async (owner, launch, _watching, _parent, _transition, prepared) => {
+fixture.start = async (owner, launch, _watching, _parent, _transition, prepared) => {
await Promise.resolve()
const existing = [...processes.values()].find(process => process.program === owner && process.name === launch.name)
if (existing) throw new Error("This program already has a process with that name")
- const identity = `process-${++starts}`
+ const identity = `00000000-0000-0000-0000-${String(++starts).padStart(12, "0")}` as `${string}-${string}-${string}-${string}-${string}`
processes.set(identity, {
identity,
name: launch.name ?? null,
program: owner,
- launch: (prepared ?? manager.resolveLaunch(owner, launch)).intent
+ launch: (prepared ?? fixture.resolveLaunch(owner, launch)).intent
})
return identity
}
@@ -72,14 +86,15 @@ await assert.rejects(
/different launch/
)
-const omitted = manager.resolveLaunch(program, { name: "defaults" }).intent
-processes.set("unrelated", { program })
-const explicit = manager.resolveLaunch(program, {
+const omitted = fixture.resolveLaunch(program, { name: "defaults" }).intent
+processes.set("unrelated", { identity: "unrelated", name: null, program, launch: null })
+const explicit = fixture.resolveLaunch(program, {
name: "defaults",
server: true,
client: true
}).intent
assert.deepEqual(omitted, explicit)
+assert(omitted.client)
assert.equal(omitted.client.position, null)
assert.equal(omitted.client.size, null)
diff --git a/scripts/verify-program-install.mjs b/scripts/contracts/program-install.ts
similarity index 81%
rename from scripts/verify-program-install.mjs
rename to scripts/contracts/program-install.ts
index 538ef4c..8bc3da1 100644
--- a/scripts/verify-program-install.mjs
+++ b/scripts/contracts/program-install.ts
@@ -2,8 +2,9 @@ import assert from "node:assert/strict"
import { mkdtempSync, mkdirSync, readFileSync, rmSync, writeFileSync } from "node:fs"
import { tmpdir } from "node:os"
import { join } from "node:path"
-import Program from "../source/server/core/link-manager/auth-manager/program-manager/program.ts"
-import { copyProgram } from "../source/server/core/link-manager/auth-manager/program-manager/program-manager.ts"
+import Program from "@server/core/link-manager/auth-manager/program-manager/program"
+import { copyProgram } from "@server/core/link-manager/auth-manager/program-manager/program-manager"
+import type { ProgramCommandChunk } from "@phreshos/core"
const temporary = mkdtempSync(join(tmpdir(), "phresh-program-install-"))
const source = join(temporary, "source")
@@ -42,15 +43,16 @@ try {
assert.deepEqual(declaration.categories, ["Development"])
assert.deepEqual(declaration.keywords, ["example"])
assert.equal(declaration.website, "https://example.test/program")
+ assert(program.config.server)
assert.equal(declaration.server.uninstallCommand, program.config.server.uninstallCommand)
assert.equal(readFileSync(join(installed, "agent.md"), "utf8"), "Program operating knowledge")
const installedProgram = new Program(join(installed, "program.json"))
- const installation = []
- const uninstallation = []
+ const installation: ProgramCommandChunk[] = []
+ const uninstallation: ProgramCommandChunk[] = []
- await installedProgram.installServer(chunk => installation.push(chunk))
- await installedProgram.uninstallServer(chunk => uninstallation.push(chunk))
+ await installedProgram.installServer(chunk => { installation.push(chunk) })
+ await installedProgram.uninstallServer(chunk => { uninstallation.push(chunk) })
assert.deepEqual(Object.fromEntries(installation.map(chunk => [chunk.stream, chunk.text])), {
stdout: "install-out",
diff --git a/scripts/verify-readiness.mjs b/scripts/contracts/readiness.tsx
similarity index 61%
rename from scripts/verify-readiness.mjs
rename to scripts/contracts/readiness.tsx
index f016ef6..37b701e 100644
--- a/scripts/verify-readiness.mjs
+++ b/scripts/contracts/readiness.tsx
@@ -1,8 +1,7 @@
import assert from "node:assert/strict"
-import { createElement } from "react"
import { renderToStaticMarkup } from "react-dom/server"
-import Readiness from "../source/libs/readiness/main.tsx"
-import ReadinessState from "../source/libs/readiness/state.ts"
+import Readiness from "@libs/readiness/main"
+import ReadinessState from "@libs/readiness/state"
const startup = ReadinessState.start(["connection", "session", "wallpaper"])
@@ -34,28 +33,36 @@ assert.throws(() => startup.ready("unknown"), /does not know/)
assert.throws(() => ReadinessState.start(["connection", "connection"]), /already knows/)
assert.throws(() => ReadinessState.start([""]), /must have a name/)
-const pendingMarkup = renderToStaticMarkup(createElement(Readiness, { requirements: ["connection"] },
- createElement(Readiness.Pending, null, createElement("span", null, "Loading")),
- createElement("p", null, "Prepared content")
-))
+const pendingMarkup = renderToStaticMarkup(
+ <Readiness requirements={["connection"]}>
+ <Readiness.Pending><span>Loading</span></Readiness.Pending>
+ <p>Prepared content</p>
+ </Readiness>
+)
-const readyMarkup = renderToStaticMarkup(createElement(Readiness, { requirements: [] },
- createElement(Readiness.Pending, null, createElement("span", null, "Loading")),
- createElement("p", null, "Prepared content")
-))
+const readyMarkup = renderToStaticMarkup(
+ <Readiness requirements={[]}>
+ <Readiness.Pending><span>Loading</span></Readiness.Pending>
+ <p>Prepared content</p>
+ </Readiness>
+)
assert.match(pendingMarkup, /Loading/)
assert.match(pendingMarkup, /Prepared content/)
assert.doesNotMatch(readyMarkup, /Loading/)
assert.match(readyMarkup, /Prepared content/)
-const observedPendingMarkup = renderToStaticMarkup(createElement(Readiness, { requirements: ["connection", "session"] },
- createElement(Readiness.Pending, null, pending => createElement("span", null, pending.join(",")))
-))
+const observedPendingMarkup = renderToStaticMarkup(
+ <Readiness requirements={["connection", "session"]}>
+ <Readiness.Pending>{pending => <span>{pending.join(",")}</span>}</Readiness.Pending>
+ </Readiness>
+)
-const observedReadyMarkup = renderToStaticMarkup(createElement(Readiness, { requirements: [] },
- createElement(Readiness.Pending, null, pending => createElement("span", null, String(pending.length)))
-))
+const observedReadyMarkup = renderToStaticMarkup(
+ <Readiness requirements={[]}>
+ <Readiness.Pending>{pending => <span>{pending.length}</span>}</Readiness.Pending>
+ </Readiness>
+)
assert.match(observedPendingMarkup, /connection,session/)
assert.match(observedReadyMarkup, />0</)
diff --git a/scripts/verify-storage.mjs b/scripts/contracts/storage.ts
similarity index 97%
rename from scripts/verify-storage.mjs
rename to scripts/contracts/storage.ts
index 806ac51..9252c39 100644
--- a/scripts/verify-storage.mjs
+++ b/scripts/contracts/storage.ts
@@ -2,7 +2,7 @@ import assert from "node:assert/strict"
import { mkdirSync, mkdtempSync, readdirSync, rmSync, symlinkSync, writeFileSync } from "node:fs"
import { tmpdir } from "node:os"
import { join } from "node:path"
-import FileArea from "../source/libs/file-area.ts"
+import FileArea from "@libs/file-area"
const fixture = mkdtempSync(join(tmpdir(), "phreshos-storage-"))
const root = join(fixture, "configured")
diff --git a/scripts/verify-uploads.mjs b/scripts/contracts/uploads.ts
similarity index 78%
rename from scripts/verify-uploads.mjs
rename to scripts/contracts/uploads.ts
index 8e003c3..4df0ddf 100644
--- a/scripts/verify-uploads.mjs
+++ b/scripts/contracts/uploads.ts
@@ -1,9 +1,10 @@
import assert from "node:assert/strict"
import { mkdtemp, readFile, rm } from "node:fs/promises"
import { resolve } from "node:path"
-import FileManager from "../source/libs/file-manager.ts"
-import UploadManager from "../source/server/core/upload-manager.ts"
-import uploadView from "../source/server/view/uploads.ts"
+import FileManager from "@libs/file-manager"
+import UploadManager from "@server/core/upload-manager"
+import uploadView from "@server/view/uploads"
+import type Application from "@server/core/application"
import { Hono } from "hono"
const directory = await mkdtemp(resolve(".verify-uploads-"))
@@ -23,21 +24,25 @@ try {
const view = new Hono()
- view.route("/uploads", uploadView({
+ const application = {
uploads,
linkManager: {
authManager: {
- verify(value) {
+ verify(value: unknown) {
if (value !== "allowed") throw new Error("Unauthorized")
},
- async upload(authorization, content, extension, signal) {
+ async upload(authorization: unknown, content: ReadableStream<Uint8Array> | null, extension: string, signal?: AbortSignal) {
this.verify(authorization)
const written = await uploads.write(extension, content, signal)
- return uploads.stat(written)
+ const upload = uploads.stat(written)
+ assert(upload)
+ return upload
}
}
}
- }))
+ } as unknown as Application
+
+ view.route("/uploads", uploadView(application))
const response = await view.request("http://system/uploads", {
method: "POST",
headers: {
diff --git a/scripts/verify-window-geometry.mjs b/scripts/contracts/window-geometry.ts
similarity index 63%
rename from scripts/verify-window-geometry.mjs
rename to scripts/contracts/window-geometry.ts
index 29c8d02..ed48ebf 100644
--- a/scripts/verify-window-geometry.mjs
+++ b/scripts/contracts/window-geometry.ts
@@ -1,7 +1,8 @@
import assert from "node:assert/strict"
-import ClientWindow from "../source/client/core/link-manager/auth-manager/process-manager/window.ts"
-import ProcessManager from "../source/server/core/link-manager/auth-manager/process-manager/process-manager.ts"
-import ServerWindow from "../source/server/core/link-manager/auth-manager/process-manager/window.ts"
+import ClientWindow from "@client/core/link-manager/auth-manager/process-manager/window"
+import ProcessManager from "@server/core/link-manager/auth-manager/process-manager/process-manager"
+import ServerWindow from "@server/core/link-manager/auth-manager/process-manager/window"
+import type ClientProcessManager from "@client/core/link-manager/auth-manager/process-manager/process-manager"
const initial = {
position: { x: 10, y: 20 },
@@ -27,24 +28,25 @@ const next = {
position: { x: "1/4", y: 30 },
size: { width: "1/2", height: 240 }
}
-const events = []
-const echoes = []
+const events: unknown[][] = []
+const echoes: unknown[][] = []
const manager = {
$outbound: {
- publish(...echo) {
+ async publish(...echo: unknown[]) {
echoes.push(echo)
+ return []
}
},
- mutableWindowOf(identity) {
+ mutableWindowOf(identity: string) {
assert.equal(identity, "process")
return authority
},
- said(...event) {
+ said(...event: unknown[]) {
events.push(event)
}
}
-const echo = await ProcessManager.prototype.setGeometry.call(manager, "process", next)
+const echo = await ProcessManager.prototype.setGeometry.call(manager as unknown as ProcessManager, "process", next)
assert.deepEqual(authority.position, next.position)
assert.deepEqual(authority.size, next.size)
@@ -57,9 +59,9 @@ assert.deepEqual(echoes, [["/geometry", { identity: "process", window: authority
assert.equal(echo.identity, "process")
assert.equal(echo.window, authority)
-const publications = []
+const publications: unknown[][] = []
const counterpart = new ClientWindow(
- { $outbound: { publish: (...publication) => publications.push(publication) } },
+ { $outbound: { async publish(...publication: unknown[]) { publications.push(publication); return [] } } } as unknown as ClientProcessManager,
"process",
authority.toJSON()
)
diff --git a/scripts/verify-worker-runtime.mjs b/scripts/contracts/worker-runtime.ts
similarity index 74%
rename from scripts/verify-worker-runtime.mjs
rename to scripts/contracts/worker-runtime.ts
index f987f4e..a10c412 100644
--- a/scripts/verify-worker-runtime.mjs
+++ b/scripts/contracts/worker-runtime.ts
@@ -4,8 +4,9 @@ import { tmpdir } from "node:os"
import { join, resolve } from "node:path"
import { pathToFileURL } from "node:url"
import { decode, encode } from "@msgpack/msgpack"
-import { WorkerServerRuntime } from "../source/server/core/link-manager/auth-manager/process-manager/server-runtime.ts"
-import Program from "../source/server/core/link-manager/auth-manager/program-manager/program.ts"
+import { WorkerServerRuntime } from "@server/core/link-manager/auth-manager/process-manager/server-runtime"
+import Program from "@server/core/link-manager/auth-manager/program-manager/program"
+import type { ProgramConfig } from "@server/core/link-manager/auth-manager/program-manager/config"
const directory = await mkdtemp(join(tmpdir(), "phresh-worker-runtime-"))
const entry = join(directory, "server.mjs")
@@ -30,14 +31,19 @@ try {
assert.equal(program.serverEntryPath, entry)
- assert.throws(() => new Program({ identity: "worker-conflict", server: { location: directory, startCommand: "node main.js", entryFile: "server.mjs" } }))
+ assert.throws(() => new Program({ identity: "worker-conflict", server: { location: directory, startCommand: "node main.js", entryFile: "server.mjs" } } as unknown as ProgramConfig))
assert.throws(() => new Program({ identity: "worker-escape", server: { location: directory, entryFile: "../server.mjs" } }))
const runtime = new WorkerServerRuntime(entry)
- const messages = []
- const output = []
-
- runtime.onMessage(message => messages.push(decode(message)))
+ const messages: unknown[][] = []
+ const output: ["out" | "err", string][] = []
+
+ runtime.onMessage(message => {
+ if (!(message instanceof Uint8Array)) throw new Error("The Worker response must be bytes")
+ const decoded = decode(message)
+ if (!Array.isArray(decoded)) throw new Error("The Worker response must be an array")
+ messages.push(decoded)
+ })
runtime.onOutput((stream, text) => output.push([stream, text]))
await until(() => messages.some(message => message[0] === "boundary" && message[1] === "ready"))
@@ -58,7 +64,7 @@ try {
await rm(directory, { recursive: true, force: true })
}
-async function until(condition, timeout = 2_000) {
+async function until(condition: () => boolean, timeout = 2_000) {
const began = Date.now()
while (!condition()) {
diff --git a/scripts/verify-client-launch.mjs b/scripts/verify-client-launch.mjs
deleted file mode 100644
index 7fde9a1..0000000
--- a/scripts/verify-client-launch.mjs
+++ /dev/null
@@ -1,22 +0,0 @@
-import assert from "node:assert/strict"
-import ProgramManager from "../source/server/core/link-manager/auth-manager/program-manager/program-manager.ts"
-
-const manager = {
- authManager: {
- processManager: {
- processes: new Map()
- }
- }
-}
-
-const program = {
- client: {},
- clientLocation: "/client",
- title: "Declared title"
-}
-
-const shape = overrides => ProgramManager.prototype.clientShape.call(manager, program, overrides)
-
-assert.equal(shape({ title: "Fetched title" }).title, "Fetched title")
-assert.equal(shape({}).title, "Declared title")
-assert.throws(() => shape({ title: 42 }), /title must be text/)
diff --git a/scripts/verify-contracts.ts b/scripts/verify-contracts.ts
new file mode 100644
index 0000000..9f7b510
--- /dev/null
+++ b/scripts/verify-contracts.ts
@@ -0,0 +1,8 @@
+import { readdir } from "node:fs/promises"
+
+const directory = new URL("./contracts/", import.meta.url)
+
+for (const file of (await readdir(directory)).filter(file => /\.tsx?$/.test(file)).sort()) {
+
+ await import(new URL(file, directory).href)
+}