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
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -112,11 +112,12 @@
"docs:site": "astro build && tsx scripts/build-md-siblings.ts",
"docs:dev": "astro dev",
"docs:preview": "astro preview",
"test": "pnpm run docs:snippets:check && pnpm run test:types && pnpm run test:node && pnpm run test:node:axios && pnpm run test:integration && pnpm run test:browser",
"test": "pnpm run docs:snippets:check && pnpm run test:types && pnpm run typecheck:tests && pnpm run test:node && pnpm run test:node:axios && pnpm run test:integration && pnpm run test:browser",
Comment thread
quietbits marked this conversation as resolved.
"test:all": "pnpm run test:node && pnpm run test:node:axios && pnpm run test:integration && pnpm run test:guides && pnpm run test:browser && pnpm run test:e2e",
"test:e2e": "./test/e2e/initialize.sh && vitest run --config config/vitest.config.e2e.ts --coverage",
"test:node": "vitest run test/unit --config config/vitest.config.ts --coverage",
"test:types": "tsc -p test/types/tsconfig.json",
"typecheck:tests": "tsc -p test/tsconfig.json",
"test:node:axios": "cross-env TRANSPORT=axios vitest run test/unit --config config/vitest.config.ts",
"test:e2e:noeval": "NODE_OPTIONS=--disallow-code-generation-from-strings pnpm run test:e2e",
"test:integration": "vitest run test/integration --config config/vitest.config.ts --coverage",
Expand Down Expand Up @@ -180,7 +181,6 @@
"js-xdr-v4": "npm:@stellar/js-xdr@4.0.0",
"jsdom": "^29.1.1",
"lint-staged": "^17.0.8",
"lodash": "^4.18.1",
"msw": "^2.14.6",
"playwright": "^1.61.0",
"prettier": "^3.8.4",
Expand Down
8 changes: 0 additions & 8 deletions pnpm-lock.yaml

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

11 changes: 6 additions & 5 deletions test/integration/apiary.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
// All endpoints from here are tested:
// https://docs.google.com/document/d/1pXL8kr1a2vfYSap9T67R-g72B_WWbaE1YsLMa04OgoU/edit
import { describe, it, expect } from "vitest";
import * as _ from "lodash";
import * as StellarSdk from "../../src/index.js";

const { Horizon } = StellarSdk;
Expand Down Expand Up @@ -59,17 +58,19 @@ describe("tests the /liquidity_pools endpoint", () => {
.call();

resp.records.forEach((record, i) => {
let expectedRecord = data._embedded.records[i];
const expectedRecord = data._embedded.records[i];

// TransactionRecord values don't map 1-to-1 to the JSON (see
// e.g. the ledger vs. ledger_attr properties), so we do a "best
// effort" validation by checking that at least the keys exist.
if (suffix === "transactions") {
record = Object.keys(record) as any;
expectedRecord = Object.keys(expectedRecord);
expect(Object.keys(record)).toEqual(
expect.arrayContaining(Object.keys(expectedRecord)),
);
return;
}

expect(_.isMatch(record, expectedRecord)).toBe(true);
expect(record).toMatchObject(expectedRecord);
});
});
});
Expand Down
1 change: 0 additions & 1 deletion test/integration/bindings.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { XdrString } from "@/xdr/index.js";
import * as StellarSdk from "../../src/index.js";
import { describe, it, expect } from "vitest";

Expand Down
14 changes: 11 additions & 3 deletions test/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
{
// Typechecks the test suites (`pnpm typecheck:tests`). The vitest runs
// compile through esbuild, which strips types without checking them, so
// without this a type error in a test file is invisible. Also the config
// eslint's type-aware rules run against (see eslint.config.js).
Comment thread
quietbits marked this conversation as resolved.
"extends": "../tsconfig.base.json",
"compilerOptions": {
"target": "ES2022",
Expand All @@ -13,25 +17,29 @@
"sourceMap": true,
"removeComments": false,
"strict": true,
"noImplicitAny": false,
"strictNullChecks": true,
"strictFunctionTypes": true,
"noImplicitReturns": true,
"noFallthroughCasesInSwitch": true,
"noUncheckedIndexedAccess": false,
"exactOptionalPropertyTypes": true,
"lib": ["ES2022", "DOM"],
"types": ["node", "vitest/globals"],
"skipLibCheck": true,
"forceConsistentCasingInFileNames": true,
"resolveJsonModule": true,
"isolatedModules": true,
// Matches the root build config; the base config's `true` would require
// tslib, which is not a dependency.
"importHelpers": false,
"noEmit": true,
"rootDir": "../",
"paths": {
"@/*": ["../src/*"]
},
"typeRoots": ["../types", "../node_modules/@types"]
// `../node_modules` is required for the `vitest/globals` entry above to
// resolve: it lives at node_modules/vitest/globals.d.ts, not under
// @types. Without it tsc fails with TS2688 and checks nothing at all.
"typeRoots": ["../types", "../node_modules/@types", "../node_modules"]
},
"include": ["**/*.ts", "**/*.js"],
"exclude": ["out"]
Expand Down
8 changes: 4 additions & 4 deletions test/unit/base/address.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ describe("Address", () => {

it("parses contract addresses", () => {
const sc = xdr.ScAddress.scAddressTypeContract(
new xdr.Hash(StrKey.decodeContract(CONTRACT)),
new xdr.ContractId(StrKey.decodeContract(CONTRACT)),
);
const c = Address.fromScAddress(sc);
expect(c.toString()).toBe(CONTRACT);
Expand Down Expand Up @@ -129,7 +129,7 @@ describe("Address", () => {

it("parses liquidity-pool addresses", () => {
const sc = xdr.ScAddress.scAddressTypeLiquidityPool(
new xdr.Hash(new Uint8Array(32)),
new xdr.PoolId(new Uint8Array(32)),
);
const lp = Address.fromScAddress(sc);
expect(lp.toString()).toBe(LIQUIDITY_POOL_ZERO);
Expand All @@ -152,7 +152,7 @@ describe("Address", () => {
it("parses contract ScVals", () => {
const scVal = xdr.ScVal.scvAddress(
xdr.ScAddress.scAddressTypeContract(
new xdr.Hash(StrKey.decodeContract(CONTRACT)),
new xdr.ContractId(StrKey.decodeContract(CONTRACT)),
),
);
const c = Address.fromScVal(scVal);
Expand Down Expand Up @@ -187,7 +187,7 @@ describe("Address", () => {
it("parses liquidity-pool ScVals", () => {
const scVal = xdr.ScVal.scvAddress(
xdr.ScAddress.scAddressTypeLiquidityPool(
new xdr.Hash(new Uint8Array(32)),
new xdr.PoolId(new Uint8Array(32)),
),
);
const lp = Address.fromScVal(scVal);
Expand Down
24 changes: 18 additions & 6 deletions test/unit/base/auth.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1110,8 +1110,12 @@ describe("building authorization entries", () => {
);

// internal verification passing proves the payload is the signing hash
const creds = expectUnionVariant(
signed.credentials,
"sorobanCredentialsAddress",
).address;
const sigArgs = expectDefined(
signed.credentials.address.signature.value,
expectUnionVariant(creds.signature, "scvVec").value,
).map((v) => scValToNative(v));
const sig = sigArgs[0] as {
public_key: Uint8Array;
Expand Down Expand Up @@ -1161,7 +1165,10 @@ describe("building authorization entries", () => {
Networks.TESTNET,
);

const addr = signed.credentials.address;
const addr = expectUnionVariant(
signed.credentials,
"sorobanCredentialsAddress",
).address;
expect(addr.signatureExpirationLedger).toBe(10);
// verbatim: not wrapped in an scvVec, byte-identical to what we returned
expect(addr.signature.toXdr("hex")).toBe(customSig.toXdr("hex"));
Expand All @@ -1187,9 +1194,11 @@ describe("building authorization entries", () => {
Networks.TESTNET,
);

expect(signed.credentials.addressV2.signature.toXdr("hex")).toBe(
customSig.toXdr("hex"),
);
const addrV2 = expectUnionVariant(
signed.credentials,
"sorobanCredentialsAddressV2",
).addressV2;
expect(addrV2.signature.toXdr("hex")).toBe(customSig.toXdr("hex"));
});

it("routes the signature via the returned address, like forAddress", async () => {
Expand All @@ -1211,7 +1220,10 @@ describe("building authorization entries", () => {
Networks.TESTNET,
);

const wd = signed.credentials.addressWithDelegates;
const wd = expectUnionVariant(
signed.credentials,
"sorobanCredentialsAddressWithDelegates",
).addressWithDelegates;
// top-level untouched (still the Void placeholder), delegate node
// carries the custom ScVal
expect(wd.addressCredentials.signature.type).toBe("scvVoid");
Expand Down
4 changes: 2 additions & 2 deletions test/unit/base/events.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ describe("humanizing raw events", () => {
inSuccessfulContractCall: true,
event: new xdr.ContractEvent({
ext: xdr.ExtensionPoint.v0(),
contractId: new xdr.Hash(StrKey.decodeContract(contractId)),
contractId: new xdr.ContractId(StrKey.decodeContract(contractId)),
type: xdr.ContractEventType.contract,
body: makeBody({
topics: topics1,
Expand Down Expand Up @@ -73,7 +73,7 @@ describe("humanizing raw events", () => {
const contractEvents = [
new xdr.ContractEvent({
ext: xdr.ExtensionPoint.v0(),
contractId: new xdr.Hash(StrKey.decodeContract(contractId)),
contractId: new xdr.ContractId(StrKey.decodeContract(contractId)),
type: xdr.ContractEventType.contract,
body: makeBody({
topics: topics1,
Expand Down
1 change: 0 additions & 1 deletion test/unit/base/muxed_account.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { describe, it, expect } from "vitest";
import { Account } from "../../../src/base/account.js";
import { MuxedAccount } from "../../../src/base/muxed_account.js";
import { StrKey } from "../../../src/base/strkey.js";
import * as xdr from "../../../src/xdr/index.js";
import { encodeMuxedAccountToAddress } from "../../../src/base/util/decode_encode_muxed_account.js";

const PUBKEY = "GA7QYNF7SOWQ3GLR2BGMZEHXAVIRZA4KVWLTJJFC7MGXUA74P7UJVSGZ";
Expand Down
2 changes: 0 additions & 2 deletions test/unit/base/numbers/xdr_large_int.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ import {
XdrLargeInt,
} from "../../../../src/base/numbers/xdr_large_int.js";

import * as xdr from "../../../../src/xdr/index.js";

describe("XdrLargeInt", () => {
describe("constructor", () => {
describe("accepts single values", () => {
Expand Down
23 changes: 19 additions & 4 deletions test/unit/base/operations/set_options.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,19 @@ import {
expectOperationType,
} from "../support/operation.js";

// `Operation.fromXdrObject` parses signer hashes into bytes, but the
// `SetOptionsResult` signer is declared with the *input* union
// (`Uint8Array | string`), so the parsed value needs narrowing here.
function expectBytes(value: Uint8Array | string): Uint8Array {
expect(value).toBeInstanceOf(Uint8Array);

if (typeof value === "string") {
throw new Error("Expected a parsed signer hash, got a string");
}

return value;
}

describe("Operation.setOptions()", () => {
it("auth flags are set correctly", () => {
expect(AuthRequiredFlag).toBe(1);
Expand Down Expand Up @@ -88,7 +101,9 @@ describe("Operation.setOptions()", () => {
"preAuthTx",
);

expect(uint8ArrayToHex(signer.preAuthTx)).toBe(uint8ArrayToHex(txHash));
expect(uint8ArrayToHex(expectBytes(signer.preAuthTx))).toBe(
uint8ArrayToHex(txHash),
);
expect(signer.weight).toBe(10);
});

Expand All @@ -113,7 +128,7 @@ describe("Operation.setOptions()", () => {
"preAuthTx",
);

expect(uint8ArrayToHex(signer.preAuthTx)).toBe(txHash);
expect(uint8ArrayToHex(expectBytes(signer.preAuthTx))).toBe(txHash);
expect(signer.weight).toBe(10);
});

Expand All @@ -136,7 +151,7 @@ describe("Operation.setOptions()", () => {
"sha256Hash",
);

expect(uint8ArrayToHex(signer.sha256Hash)).toBe(
expect(uint8ArrayToHex(expectBytes(signer.sha256Hash))).toBe(
uint8ArrayToHex(sha256Hash),
);
expect(signer.weight).toBe(10);
Expand All @@ -163,7 +178,7 @@ describe("Operation.setOptions()", () => {
"sha256Hash",
);

expect(uint8ArrayToHex(signer.sha256Hash)).toBe(sha256Hash);
expect(uint8ArrayToHex(expectBytes(signer.sha256Hash))).toBe(sha256Hash);
expect(signer.weight).toBe(10);
});

Expand Down
27 changes: 16 additions & 11 deletions test/unit/base/scval.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
} from "../../../src/base/index.js";
import { Contract } from "../../../src/base/contract.js";
import * as xdr from "../../../src/xdr/index.js";
import { expectVariant } from "./support/xdr.js";
import {
nativeToScVal,
scValToNative,
Expand Down Expand Up @@ -369,9 +370,10 @@ describe("nativeToScVal with Maps", () => {
["a", 1],
]),
);
expect(scv.type).toBe("scvMap");
const entries = scv.value!;
expect(entries.map((e) => e.key.value.toString())).toEqual(["a", "b"]);
const entries = expectVariant(scv, "scvMap").value ?? [];
expect(entries.map((e) => expectVariant(e.key, "scvString").value)).toEqual(
["a", "b"],
);
expect(entries.map((e) => Number(e.val.value))).toEqual([1, 2]);
});

Expand All @@ -382,7 +384,7 @@ describe("nativeToScVal with Maps", () => {
});

it("converts a Map subclass", () => {
class MyMap extends Map {}
class MyMap extends Map<string, number> {}
const scv = nativeToScVal(new MyMap([["a", 1]]));
expect(scv.type).toBe("scvMap");
});
Expand All @@ -395,7 +397,9 @@ describe("nativeToScVal with Maps", () => {
]),
{ type: ["symbol", "u32"] },
);
const entries = scv.value!;
const entries = expectDefined(expectVariant(scv, "scvMap").value);
// asserted so the forEach below cannot pass by iterating nothing
expect(entries).toHaveLength(2);
entries.forEach((e) => {
expect(e.key.type).toBe("scvSymbol");
expect(e.val.type).toBe("scvU32");
Expand All @@ -404,9 +408,10 @@ describe("nativeToScVal with Maps", () => {

it("allows a partial pair", () => {
const scv = nativeToScVal(new Map([["a", 1]]), { type: ["symbol"] });
const entries = scv.value!;
expect(entries[0].key.type).toBe("scvSymbol");
expect(entries[0].val.type).toBe("scvU64");
const entries = expectVariant(scv, "scvMap").value ?? [];
const first = expectDefined(entries[0]);
expect(first.key.type).toBe("scvSymbol");
expect(first.val.type).toBe("scvU64");
});

it("applies a per-key spec for string-keyed Maps", () => {
Expand All @@ -417,9 +422,9 @@ describe("nativeToScVal with Maps", () => {
]),
{ type: { a: ["symbol", "u32"] } },
);
const entries = scv.value!;
const a = entries.find((e) => e.key.type === "scvSymbol")!;
const b = entries.find((e) => e.key.type === "scvString")!;
const entries = expectVariant(scv, "scvMap").value ?? [];
const a = expectDefined(entries.find((e) => e.key.type === "scvSymbol"));
const b = expectDefined(entries.find((e) => e.key.type === "scvString"));
expect(a.val.type).toBe("scvU32");
expect(b.val.type).toBe("scvU64");
});
Expand Down
6 changes: 2 additions & 4 deletions test/unit/base/strkey.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -440,10 +440,8 @@ describe("StrKey", () => {
for (const testCase of happyPaths) {
it(testCase.desc, () => {
const signedPayloadBuf = StrKey.decodeSignedPayload(testCase.strkey);
const signedPayload = xdr.SignerKeyEd25519SignedPayload.fromXdr(
signedPayloadBuf,
"raw",
);
const signedPayload =
xdr.SignerKeyEd25519SignedPayload.fromXdr(signedPayloadBuf);

const signer = StrKey.encodeEd25519PublicKey(signedPayload.ed25519);
expect(signer).toBe(testCase.ed25519);
Expand Down
Loading
Loading