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
90 changes: 90 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
[workspace]
resolver = "2"
members = ["rust/crates/*"]
exclude = ["rust/crates/truapi-server"]

[workspace.package]
edition = "2024"
Expand Down
81 changes: 69 additions & 12 deletions js/packages/truapi/src/client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,16 @@ import type { Result } from "neverthrow";
import { describe, expect, it } from "bun:test";

import { createTransport } from "./client.js";
import { indexedTaggedUnion, Result as ScaleResult, str, _void } from "./scale.js";
import { CallError, indexedTaggedUnion, Result as ScaleResult, str, _void } from "./scale.js";
import type { Codec } from "./scale.js";
import { createClient, SubscriptionError } from "./generated/client.js";
import * as T from "./generated/types.js";
import * as W from "./generated/wire-table.js";
import { encodeWireMessage } from "./transport.js";

/** Wrap a codec in the `{ V1: [0, codec] }` indexed-tagged-union envelope. */
const versionedV1 = <T>(codec: Codec<T>) => indexedTaggedUnion({ V1: [0, codec] });

function toHex(u: Uint8Array): string {
return Array.from(u)
.map((b) => b.toString(16).padStart(2, "0"))
Expand Down Expand Up @@ -56,9 +60,26 @@ function providerFixture() {

/** Encode a V1 host-handshake response result payload. */
function handshakeResponsePayload(value: { success: true; value: undefined }): Uint8Array {
return indexedTaggedUnion({
V1: [0, ScaleResult(_void, T.HostHandshakeError)],
}).enc({ tag: "V1", value });
return versionedV1(ScaleResult(_void, CallError(T.VersionedHostHandshakeError))).enc({
tag: "V1",
value,
});
}

function accountGetResponsePayload(
value:
| {
success: true;
value: T.HostAccountGetResponse;
}
| {
success: false;
value: { tag: "Domain"; value: T.VersionedHostAccountGetError };
},
): Uint8Array {
return versionedV1(
ScaleResult(T.HostAccountGetResponse, CallError(T.VersionedHostAccountGetError)),
).enc({ tag: "V1", value });
}

describe("generated client transport", () => {
Expand Down Expand Up @@ -129,6 +150,35 @@ describe("generated client transport", () => {
expect(result.isOk()).toBe(true);
});

it("decodes request domain errors from the versioned response envelope", async () => {
const fixture = providerFixture();
const transport = createTransport(fixture.provider);
const client = createClient(transport);

const response = client.account.getAccount({
productAccountId: { dotNsIdentifier: "foo", derivationIndex: 0 },
});
const reason = { tag: "V1", value: { tag: "NotConnected", value: undefined } } as const;
const frame = unwrap(
encodeWireMessage({
requestId: "p:1",
payload: {
id: W.ACCOUNT_GET_ACCOUNT.response,
value: accountGetResponsePayload({
success: false,
value: { tag: "Domain", value: reason },
}),
},
}),
"encode account_get error response",
);
fixture.receive(frame);

const result = await response;
expect(result.isErr()).toBe(true);
expect(result._unsafeUnwrapErr()).toEqual({ tag: "Domain", value: reason });
});

it("auto-responds to an inbound handshake with the versioned-result shape", () => {
const fixture = providerFixture();
createTransport(fixture.provider);
Expand Down Expand Up @@ -225,14 +275,18 @@ describe("generated client transport", () => {
});

const reason = { tag: "PermissionDenied", value: undefined } as const;
const callError = {
tag: "Domain",
value: { tag: "V1", value: reason },
} as const;
const frame = unwrap(
encodeWireMessage({
requestId: sub.subscriptionId,
payload: {
id: W.PAYMENT_BALANCE_SUBSCRIBE.interrupt,
value: T.VersionedHostPaymentBalanceSubscribeError.enc({
value: versionedV1(CallError(T.VersionedHostPaymentBalanceSubscribeError)).enc({
tag: "V1",
value: reason,
value: callError,
}),
},
}),
Expand All @@ -243,7 +297,7 @@ describe("generated client transport", () => {
expect(completions).toEqual([]);
expect(errors).toHaveLength(1);
expect(errors[0]).toBeInstanceOf(SubscriptionError);
expect((errors[0] as SubscriptionError).reason).toEqual(reason);
expect((errors[0] as SubscriptionError).reason).toEqual(callError);
expect(fixture.sent).toHaveLength(1);
});

Expand All @@ -258,15 +312,18 @@ describe("generated client transport", () => {
.subscribe({ error: (error) => errors.push(error) });

const reason = "Denied";
const callError = {
tag: "Domain",
value: { tag: "V1", value: reason },
} as const;
const frame = unwrap(
encodeWireMessage({
requestId: sub.subscriptionId,
payload: {
id: W.COIN_PAYMENT_REBALANCE_PURSE.interrupt,
value: T.VersionedHostCoinPaymentRebalancePurseError.enc({
tag: "V1",
value: reason,
}),
value: versionedV1(
CallError(T.VersionedHostCoinPaymentRebalancePurseError),
).enc({ tag: "V1", value: callError }),
},
}),
"encode typed coin payment interrupt",
Expand All @@ -275,7 +332,7 @@ describe("generated client transport", () => {

expect(errors).toHaveLength(1);
expect(errors[0]).toBeInstanceOf(SubscriptionError);
expect((errors[0] as SubscriptionError).reason).toEqual(reason);
expect((errors[0] as SubscriptionError).reason).toEqual(callError);
});

it("treats a malformed receive payload as terminal and sends _stop", () => {
Expand Down
2 changes: 2 additions & 0 deletions rust/crates/truapi-codegen/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Mismatch dumps written by tests/golden_rust_emit.rs for local inspection.
tests/golden/*.actual
3 changes: 3 additions & 0 deletions rust/crates/truapi-codegen/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,6 @@ anyhow = "1"
clap = { version = "4", features = ["derive"] }
indoc = "2"
convert_case = "0.6"

[dev-dependencies]
tempfile = "3"
Loading