Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 18 additions & 1 deletion packages/app-compose/src/__tests__/is.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { createTask, tag } from "@runnable"
import { literal } from "@computable"
import { createTask, createWire, tag } from "@runnable"
import { describe, expect, it } from "vitest"
import { is } from "../is"

Expand Down Expand Up @@ -33,3 +34,19 @@ describe("is.task", () => {
expect(is.task({})).toBe(false)
})
})

describe("is.wire", () => {
it("returns true for a wire", () => {
const wire = createWire({ from: literal(1), to: tag<number>("x") })

expect(is.wire(wire)).toBe(true)
})

it("returns false for a non object", () => {
expect(is.wire(1)).toBe(false)
})

it("returns false for an object", () => {
expect(is.wire({})).toBe(false)
})
})
13 changes: 6 additions & 7 deletions packages/app-compose/src/compose/__tests__/compose.bench.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
import { compose } from "@compose"
import { literal, shape, type Spot } from "@computable"
import { createWire, tag, createTask } from "@runnable"
import { createTask, createWire, tag } from "@runnable"
import { bench, describe, vi } from "vitest"
import { compose } from "../compose"

const double = (x: number) => x * 2

describe("multi layer, compute shapes", () => {
const rootTag = tag<number>("root")
const wire = createWire({ from: literal(1), to: rootTag })

let app = compose()
.meta({ name: "bench" })
.step(createWire(rootTag, literal(1)))
let app = compose().meta({ name: "bench" }).step(wire)

let previous: Spot<number> = rootTag.value

Expand All @@ -31,8 +30,8 @@ describe("multi layer, compute shapes", () => {
})

app = app
.step([createWire(a, shape(l, double)), createWire(b, shape(l, double))])
.step([createWire(c, shape(l, double)), createWire(d, shape(l, double))])
.step([createWire({ from: shape(l, double), to: a }), createWire({ from: shape(l, double), to: b })])
.step([createWire({ from: shape(l, double), to: c }), createWire({ from: shape(l, double), to: d })])
.step(task)

previous = task.result
Expand Down
12 changes: 6 additions & 6 deletions packages/app-compose/src/compose/__tests__/compose.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@ describe("compose", () => {
describe("guard", () => {
it("throws on warning graph", () => {
const alpha = tag<number>("alpha")
const wire = createWire(alpha, literal(1))
const wire = createWire({ from: literal(1), to: alpha })

const app = compose().step(wire)

const message = `Unused Wire found with name Tag[alpha] in step root > #1.`
const message = `Unused Wire found with name Wire[alpha] for Tag[alpha] in step root > #1.`

expect(() => app.guard()).toThrowError(message)
})
Expand All @@ -33,10 +33,10 @@ describe("compose", () => {
const alpha = tag<number>("alpha")

const app = compose()
.step(createWire(alpha, literal(1)))
.step(createWire(alpha, literal(2)))
.step(createWire({ from: literal(1), to: alpha }))
.step(createWire({ from: literal(2), to: alpha }))

const message = `A duplicate Wire found with name Tag[alpha] in step root > #2.`
const message = `A duplicate Wire found with name Wire[alpha] in step root > #2.`

expect(() => app.guard()).toThrowError(message)
})
Expand All @@ -47,7 +47,7 @@ describe("compose", () => {
const alpha = tag<number>("alpha")

const task = createTask({ name: "task", run: { fn: () => {} } })
const wire = createWire(alpha, literal(1))
const wire = createWire({ from: literal(1), to: alpha })

const graph = compose().meta({ name: "app" }).step(wire).step(task).graph()

Expand Down
62 changes: 57 additions & 5 deletions packages/app-compose/src/compose/__tests__/graph.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,11 @@ describe("graph", () => {
const valueTag = tag<boolean>("value")
const betaTask = createTask({ name: "beta", run: { fn: vi.fn(), context: valueTag.value } })

const app = compose().step(alphaTask).step(createWire(valueTag, alphaTask.result.value)).step(betaTask)
const app = compose()
.step(alphaTask)
.step(createWire({ from: alphaTask.result.value, to: valueTag }))
.step(betaTask)

const result = graph(app[Node$])

const expected = {
Expand Down Expand Up @@ -170,12 +174,12 @@ describe("graph", () => {
})

it("dependency on a task via a tag [optional]", () => {
const valueTag = tag<boolean>("value")
const valueTag = tag<boolean | undefined>("value")
const betaTask = createTask({ name: "beta", run: { fn: vi.fn(), context: optional(valueTag.value) } })

const app = compose()
.step(alphaTask)
.step(createWire(valueTag, optional(alphaTask.result.value)))
.step(createWire({ from: optional(alphaTask.result.value), to: valueTag }))
.step(betaTask)

const result = graph(app[Node$])
Expand Down Expand Up @@ -213,7 +217,7 @@ describe("graph", () => {
const betaTask = createTask({ name: "beta", run: { fn: vi.fn(), context: valueTag.value } })

const app = compose()
.step(createWire(valueTag, literal(false)))
.step(createWire({ from: literal(false), to: valueTag }))
.step(betaTask)

const result = graph(app[Node$])
Expand Down Expand Up @@ -274,7 +278,7 @@ describe("graph", () => {

const app = compose()
.step(alphaTask)
.step(createWire(fn, literal(vi.fn())))
.step(createWire({ from: literal(vi.fn()), to: fn }))
.step(betaTask)

const result = graph(app[Node$])
Expand Down Expand Up @@ -510,4 +514,52 @@ describe("graph", () => {
expect(result).toStrictEqual(expected)
})
})

describe("multi-tag wire", () => {
it("multi-tag wire satisfies multiple downstream tasks", () => {
const a = tag<number>("a")
const b = tag<string>("b")

const taskA = createTask({ name: "useA", run: { fn: vi.fn(), context: a.value } })
const taskB = createTask({ name: "useB", run: { fn: vi.fn(), context: b.value } })

const wire = createWire({ from: literal({ a: 1, b: "x" }), to: { a, b } })
const app = compose().step(wire).step([taskA, taskB])

const result = graph(app[Node$])

const expected = {
type: "seq",
meta: { name: undefined },
children: [
{
type: "run",
id: 0,
meta: { name: "a + b", kind: "wire" },
dependencies: { required: [], optional: [] },
},
{
type: "con",
meta: { name: undefined },
children: [
{
type: "run",
id: 1,
meta: { name: "useA", kind: "task" },
dependencies: { required: [0], optional: [] },
},
{
type: "run",
id: 2,
meta: { name: "useB", kind: "task" },
dependencies: { required: [0], optional: [] },
},
],
},
],
}

expect(result).toStrictEqual(expected)
})
})
})
Loading
Loading