{{ shortTx(category) }}
•
- {{ amount !== "0" ? amount : "" }}
-
- {{ metadata.symbol }}
-
+
+
+ {{ metadata.symbol }}
+
+
+ Amount unavailable
NFT:
- {{ tokenCommitment }}
+
diff --git a/src/module/tokenBadgeDisplay.ts b/src/module/tokenBadgeDisplay.ts
new file mode 100644
index 0000000..9a2904a
--- /dev/null
+++ b/src/module/tokenBadgeDisplay.ts
@@ -0,0 +1,39 @@
+import BigNumber from "bignumber.js";
+
+const compactTokenUnits = [
+ { exponent: 12, suffix: "t" },
+ { exponent: 9, suffix: "b" },
+ { exponent: 6, suffix: "m" },
+ { exponent: 3, suffix: "k" },
+];
+
+// Display policy only: keep the source integer and exact decimal out of rounding.
+export function formatTokenBadgeAmount(raw: string, decimals = 0) {
+ if (!/^\d+$/.test(raw)) return;
+ // BCMR token decimals are integers from 0 through 18.
+ if (!Number.isInteger(decimals) || decimals < 0 || decimals > 18) return;
+
+ const amount = new BigNumber(raw).shiftedBy(-decimals);
+ const exact = amount.toFixed();
+ if (amount.isLessThan(1000)) return { exact, display: exact };
+
+ const rounded = amount.precision(3, BigNumber.ROUND_HALF_UP);
+ const approximation = rounded.isEqualTo(amount) ? "" : "≈";
+ if (rounded.e !== null && rounded.e >= 15) {
+ return { exact, display: `${approximation}${rounded.toExponential()}` };
+ }
+
+ const unit = compactTokenUnits.find(({ exponent }) =>
+ rounded.isGreaterThanOrEqualTo(`1e${exponent}`),
+ );
+ if (!unit) return { exact, display: exact };
+ return {
+ exact,
+ display: `${approximation}${rounded.shiftedBy(-unit.exponent).toFixed()}${unit.suffix}`,
+ };
+}
+
+export function compactTokenBadgeCommitment(commitment: string) {
+ if (commitment.length <= 21) return commitment;
+ return `${commitment.slice(0, 7)}......${commitment.slice(-8)}`;
+}
diff --git a/tests/token-badge-component.test.ts b/tests/token-badge-component.test.ts
new file mode 100644
index 0000000..96d5684
--- /dev/null
+++ b/tests/token-badge-component.test.ts
@@ -0,0 +1,192 @@
+import { describe, expect, test } from "bun:test";
+import { compileScript, parse } from "@vue/compiler-sfc";
+import { renderToString } from "@vue/server-renderer";
+import { plugin } from "bun";
+import { createPinia } from "pinia";
+import {
+ computed,
+ createSSRApp,
+ defineComponent,
+ h,
+ onMounted,
+ ref,
+ shallowReactive,
+ watch,
+} from "vue";
+
+// Exercise the real SFCs without adding a second Vue test runner or DOM library.
+plugin({
+ name: "token-badge-vue-sfc",
+ setup(build) {
+ build.onLoad({ filter: /\.vue$/ }, async ({ path }) => {
+ const { descriptor } = parse(await Bun.file(path).text(), {
+ filename: path,
+ });
+ const script = compileScript(descriptor, {
+ id: path,
+ inlineTemplate: true,
+ });
+ return { contents: script.content, loader: "ts" };
+ });
+ },
+});
+
+// Nuxt supplies these auto-imports in production.
+Object.assign(globalThis, {
+ computed,
+ onMounted,
+ ref,
+ shallowReactive,
+ watch,
+ gql: (source: string) => source,
+});
+const { useRegistryStore } = await import("../src/store/registry");
+const { default: TransactionOperationToken } = await import(
+ "../src/components/Transaction/TransactionOperationToken.vue"
+);
+const { default: TokenBadgeValue } = await import(
+ "../src/components/Transaction/TokenBadgeValue.vue"
+);
+const { default: BaseCopy } = await import("../src/components/BaseCopy.vue");
+
+async function inspectBadges(
+ tokenAmount: string | null,
+ tokenCommitment: string | null,
+ decimals?: number,
+) {
+ const category = "ab".repeat(32);
+ const app = createSSRApp({
+ setup() {
+ const registry = useRegistryStore();
+ if (decimals !== undefined) {
+ registry.registryList.push({
+ name: "Test registry",
+ url: "auth_chain",
+ enable: true,
+ default: false,
+ });
+ registry.authchains.set(category, {
+ version: { major: 2, minor: 0, patch: 0 },
+ latestRevision: "2024-01-01T00:00:00.000Z",
+ registryIdentity: "Test registry",
+ identities: {
+ [category]: {
+ "2024-01-01T00:00:00.000Z": {
+ name: "Test token",
+ token: { category, decimals, symbol: "TEST" },
+ },
+ },
+ },
+ });
+ }
+ return () =>
+ h(TransactionOperationToken, {
+ category,
+ tokenAmount,
+ tokenCommitment,
+ tokenCapability: "none",
+ });
+ },
+ });
+ app.use(createPinia());
+ app.component("TransactionTokenBadgeValue", TokenBadgeValue);
+ app.component("BaseCopy", BaseCopy);
+ app.component(
+ "NuxtLink",
+ defineComponent({
+ props: { to: String },
+ setup(props, { slots }) {
+ return () => h("a", { href: props.to }, slots.default?.());
+ },
+ }),
+ );
+
+ const inspection = {
+ summaries: new Map
(),
+ copyButtons: 0,
+ disclosures: 0,
+ text: "",
+ };
+ await new HTMLRewriter()
+ .on("summary", {
+ element(element) {
+ inspection.summaries.set(
+ element.getAttribute("title") || "",
+ element.getAttribute("aria-label"),
+ );
+ },
+ })
+ .on("button[aria-label='Copy to clipboard']", {
+ element() {
+ inspection.copyButtons++;
+ },
+ })
+ .on("details", {
+ element() {
+ inspection.disclosures++;
+ },
+ })
+ .on("div", {
+ text(chunk) {
+ inspection.text += chunk.text;
+ },
+ })
+ .transform(new Response(await renderToString(app)))
+ .text();
+ return inspection;
+}
+
+describe("transaction badges rendered with the registry and BaseCopy", () => {
+ test("keeps compact summaries separate from exact decimal and base-unit copy values", async () => {
+ const commitment = `0000000${"ab".repeat(20)}92461b87`;
+ const result = await inspectBadges("9007199254740993", commitment, 2);
+ expect(result.disclosures).toBe(2);
+ expect(result.summaries.get("90071992547409.93")).toBe(
+ "Exact token amount: 90071992547409.93. Inspect exact value",
+ );
+ expect(result.summaries.get(commitment)).toBe(
+ `Exact NFT commitment: ${commitment}. Inspect exact value`,
+ );
+ expect(result.text).toContain("≈90.1t");
+ expect(result.text).toContain("TEST");
+ expect(result.text).toContain("90071992547409.93");
+ expect(result.text).toContain("9007199254740993");
+ expect(result.text).toContain("0000000......92461b87");
+ expect(result.text).toContain(commitment);
+ expect(result.copyButtons).toBe(3);
+ });
+
+ test("scales with actual registry metadata before selecting a compact suffix", async () => {
+ const result = await inspectBadges("200000", null, 2);
+ expect(result.text).toContain("2k");
+ expect(result.summaries.has("2000")).toBe(true);
+ expect(result.copyButtons).toBe(2);
+ });
+
+ test("identifies base units when metadata is unavailable", async () => {
+ const result = await inspectBadges("2000", null);
+ expect(result.text).toContain("2k");
+ expect(result.summaries.get("2000")).toContain("metadata unavailable");
+ expect(result.copyButtons).toBe(1);
+ });
+
+ test("preserves an explicit zero and short leading-zero commitment", async () => {
+ const result = await inspectBadges("0", "0001", 0);
+ expect(result.summaries.has("0")).toBe(true);
+ expect(result.summaries.has("0001")).toBe(true);
+ expect(result.copyButtons).toBe(2);
+ });
+
+ test("omits absent values and does not invent a commitment", async () => {
+ const result = await inspectBadges(null, "");
+ expect(result.disclosures).toBe(0);
+ expect(result.copyButtons).toBe(0);
+ });
+
+ test("shows invalid amounts as unavailable instead of zero", async () => {
+ const result = await inspectBadges("not-an-integer", null, 0);
+ expect(result.text).toContain("Amount unavailable");
+ expect(result.disclosures).toBe(0);
+ expect(result.copyButtons).toBe(0);
+ });
+});
diff --git a/tests/token-badge-display.test.ts b/tests/token-badge-display.test.ts
new file mode 100644
index 0000000..3f3a631
--- /dev/null
+++ b/tests/token-badge-display.test.ts
@@ -0,0 +1,98 @@
+import { describe, expect, test } from "bun:test";
+import {
+ compactTokenBadgeCommitment,
+ formatTokenBadgeAmount,
+} from "../src/module/tokenBadgeDisplay";
+
+describe("transaction token badge amounts", () => {
+ test.each([
+ ["0", "0"],
+ ["999", "999"],
+ ["1000", "1k"],
+ ["2000", "2k"],
+ ["1234", "≈1.23k"],
+ ["1235", "≈1.24k"],
+ ["999499", "≈999k"],
+ ["999500", "≈1m"],
+ ["1000000", "1m"],
+ ["1000000000", "1b"],
+ ["1000000000000", "1t"],
+ ["999500000000000", "≈1e+15"],
+ ["1000000000000000", "1e+15"],
+ ])("formats %s as %s without changing the exact value", (raw, display) => {
+ expect(formatTokenBadgeAmount(raw)).toEqual({ exact: raw, display });
+ });
+
+ test.each([
+ ["200000", 2, "2000", "2k"],
+ ["2000", 2, "20", "20"],
+ ["999999", 3, "999.999", "999.999"],
+ ["1000001", 3, "1000.001", "≈1k"],
+ ["1", 18, "0.000000000000000001", "0.000000000000000001"],
+ ["00000123", 2, "1.23", "1.23"],
+ ])(
+ "applies metadata decimals to %s before compaction",
+ (raw, decimals, exact, display) => {
+ expect(formatTokenBadgeAmount(raw, decimals)).toEqual({ exact, display });
+ },
+ );
+
+ test("retains every base unit beyond Number.MAX_SAFE_INTEGER", () => {
+ expect(formatTokenBadgeAmount("9007199254740993", 2)).toEqual({
+ exact: "90071992547409.93",
+ display: "≈90.1t",
+ });
+ expect(formatTokenBadgeAmount("9223372036854775807")).toEqual({
+ exact: "9223372036854775807",
+ display: "≈9.22e+18",
+ });
+ });
+
+ test("never overflows or uses exponential notation for the exact value", () => {
+ const huge = `12345${"0".repeat(400)}`;
+ expect(formatTokenBadgeAmount(huge)).toEqual({
+ exact: huge,
+ display: "≈1.23e+404",
+ });
+ expect(formatTokenBadgeAmount(`1${"0".repeat(400)}`)?.display).toBe(
+ "1e+400",
+ );
+ });
+
+ test.each(["", "NaN", "Infinity", "-1", "1.5", "1e3"])(
+ "does not present an invalid on-chain amount as zero: %s",
+ (raw) => expect(formatTokenBadgeAmount(raw)).toBeUndefined(),
+ );
+
+ test.each([-1, 1.5, 19, Number.NaN, Number.POSITIVE_INFINITY])(
+ "does not guess the unit for invalid metadata decimals: %s",
+ (decimals) =>
+ expect(formatTokenBadgeAmount("1234", decimals)).toBeUndefined(),
+ );
+});
+
+describe("transaction NFT commitment badges", () => {
+ test("uses the issue's prefix and suffix without interpreting hex as a number", () => {
+ expect(
+ compactTokenBadgeCommitment(
+ "400d0300a91493439efbd0d0906be50e59f4a09d26e25192461b87",
+ ),
+ ).toBe("400d030......92461b87");
+ const commitment = `0000000${"aB".repeat(20)}00000001`;
+ expect(compactTokenBadgeCommitment(commitment)).toBe(
+ "0000000......00000001",
+ );
+ });
+
+ test.each(["", "00", "00000001", "0123456789abcdef01234"])(
+ "keeps empty and short commitments intact: %s",
+ (commitment) =>
+ expect(compactTokenBadgeCommitment(commitment)).toBe(commitment),
+ );
+
+ test("only abbreviates when the result is shorter", () => {
+ expect(compactTokenBadgeCommitment("0123456789abcdef012345")).toBe(
+ "0123456......ef012345",
+ );
+ });
+});
From f8b7e58bf2de1585becd664f92e4cdf4c63b41a2 Mon Sep 17 00:00:00 2001
From: Salem Mohammed Shamakh <58360393+salemkode@users.noreply.github.com>
Date: Sat, 14 Nov 2026 23:17:19 +0300
Subject: [PATCH 5/5] test: retain precision regressions with compact badge
disclosures
---
README.md | 5 +
tests/fixtures/token-badge-component.ts | 249 ++++++++++++++++++++++++
tests/token-badge-component.test.ts | 205 ++-----------------
tests/transaction-token.test.ts | 157 ---------------
4 files changed, 271 insertions(+), 345 deletions(-)
create mode 100644 tests/fixtures/token-badge-component.ts
delete mode 100644 tests/transaction-token.test.ts
diff --git a/README.md b/README.md
index d2fe179..b9843f3 100644
--- a/README.md
+++ b/README.md
@@ -143,3 +143,8 @@ View visits, page views, referrers, and performance in the Cloudflare account's
**Analytics → Web Analytics → explorer.salemkode.com** dashboard. The beacon token
is a public site identifier, not an API credential. Future builds retain the
tracking script automatically.
+
+The compact badge suite also retains issue #26's exact-value regressions, including
+`72624976668144285` and the maximum fungible amount. Its disclosure and copy-value
+assertions replace the earlier raw-only text snapshots because compact labels
+and registry decimal scaling intentionally change the visible summary.
diff --git a/tests/fixtures/token-badge-component.ts b/tests/fixtures/token-badge-component.ts
new file mode 100644
index 0000000..1305fcc
--- /dev/null
+++ b/tests/fixtures/token-badge-component.ts
@@ -0,0 +1,249 @@
+import { describe, expect, test } from "bun:test";
+import { compileScript, parse } from "@vue/compiler-sfc";
+import { renderToString } from "@vue/server-renderer";
+import { plugin } from "bun";
+import { createPinia } from "pinia";
+import {
+ computed,
+ createSSRApp,
+ defineComponent,
+ h,
+ onMounted,
+ ref,
+ shallowReactive,
+ shallowRef,
+ watch,
+} from "vue";
+import type { Capability } from "~/types";
+
+// Exercise the real SFCs without adding a second Vue test runner or DOM library.
+plugin({
+ name: "token-badge-vue-sfc",
+ setup(build) {
+ build.onLoad({ filter: /\.vue$/ }, async ({ path }) => {
+ const { descriptor } = parse(await Bun.file(path).text(), {
+ filename: path,
+ });
+ const script = compileScript(descriptor, {
+ id: path,
+ inlineTemplate: true,
+ });
+ return { contents: script.content, loader: "ts" };
+ });
+ },
+});
+
+// Nuxt supplies these auto-imports in production.
+Object.assign(globalThis, {
+ computed,
+ onMounted,
+ ref,
+ shallowReactive,
+ shallowRef,
+ watch,
+ gql: (source: string) => source,
+ useSubscription: () => ({ result: ref() }),
+});
+const { useRegistryStore } = await import("../../src/store/registry");
+const { default: TransactionOperationToken } = await import(
+ "../../src/components/Transaction/TransactionOperationToken.vue"
+);
+const { default: TokenBadgeValue } = await import(
+ "../../src/components/Transaction/TokenBadgeValue.vue"
+);
+const { default: BaseCopy } = await import("../../src/components/BaseCopy.vue");
+
+async function inspectBadges(
+ tokenAmount: string | null | undefined,
+ tokenCommitment: string | null,
+ decimals?: number,
+ tokenCapability: Capability = "none",
+) {
+ const category = "ab".repeat(32);
+ const app = createSSRApp({
+ setup() {
+ const registry = useRegistryStore();
+ if (decimals !== undefined) {
+ registry.registryList.push({
+ name: "Test registry",
+ url: "auth_chain",
+ enable: true,
+ default: false,
+ });
+ registry.authchains.set(category, {
+ version: { major: 2, minor: 0, patch: 0 },
+ latestRevision: "2024-01-01T00:00:00.000Z",
+ registryIdentity: "Test registry",
+ identities: {
+ [category]: {
+ "2024-01-01T00:00:00.000Z": {
+ name: "Test token",
+ token: { category, decimals, symbol: "TEST" },
+ },
+ },
+ },
+ });
+ }
+ return () =>
+ h(TransactionOperationToken, {
+ category,
+ tokenAmount,
+ tokenCommitment,
+ tokenCapability,
+ });
+ },
+ });
+ app.use(createPinia());
+ app.component("TransactionTokenBadgeValue", TokenBadgeValue);
+ app.component("BaseCopy", BaseCopy);
+ app.component(
+ "NuxtLink",
+ defineComponent({
+ props: { to: String },
+ setup(props, { slots }) {
+ return () => h("a", { href: props.to }, slots.default?.());
+ },
+ }),
+ );
+
+ const inspection = {
+ summaries: new Map(),
+ copyButtons: 0,
+ disclosures: 0,
+ text: "",
+ };
+ await new HTMLRewriter()
+ .on("summary", {
+ element(element) {
+ inspection.summaries.set(
+ element.getAttribute("title") || "",
+ element.getAttribute("aria-label"),
+ );
+ },
+ })
+ .on("button[aria-label='Copy to clipboard']", {
+ element() {
+ inspection.copyButtons++;
+ },
+ })
+ .on("details", {
+ element() {
+ inspection.disclosures++;
+ },
+ })
+ .on("div", {
+ text(chunk) {
+ inspection.text += chunk.text;
+ },
+ })
+ .transform(new Response(await renderToString(app)))
+ .text();
+ return inspection;
+}
+
+describe("transaction badges rendered with the registry and BaseCopy", () => {
+ test.each([
+ "1",
+ "9007199254740993",
+ "72624976668144285",
+ "9223372036854775807",
+ ])(
+ "preserves issue #26's exact raw amount %s when compact display is enabled",
+ async (raw) => {
+ const result = await inspectBadges(raw, null);
+ expect(result.summaries.has(raw)).toBe(true);
+ expect(result.text).toContain(raw);
+ expect(result.copyButtons).toBe(1);
+ },
+ );
+
+ test.each([undefined, null])(
+ "missing amount %s stays absent",
+ async (raw) => {
+ const result = await inspectBadges(raw, null);
+ expect(result.disclosures).toBe(0);
+ expect(result.copyButtons).toBe(0);
+ },
+ );
+
+ test.each(["none", "mutable", "minting"] as const)(
+ "NFT %s retains its commitment and capability without an FT amount",
+ async (capability) => {
+ const result = await inspectBadges(
+ undefined,
+ "00ff",
+ undefined,
+ capability,
+ );
+ expect(result.summaries.has("00ff")).toBe(true);
+ expect(result.copyButtons).toBe(1);
+ if (capability !== "none")
+ expect(result.text).toContain(`${capability} nft`);
+ },
+ );
+
+ test("combined FT and NFT outputs preserve the maximum amount and commitment", async () => {
+ const result = await inspectBadges(
+ "9223372036854775807",
+ "00ff",
+ undefined,
+ "mutable",
+ );
+ expect(result.summaries.has("9223372036854775807")).toBe(true);
+ expect(result.summaries.has("00ff")).toBe(true);
+ expect(result.copyButtons).toBe(2);
+ });
+
+ test("keeps compact summaries separate from exact decimal and base-unit copy values", async () => {
+ const commitment = `0000000${"ab".repeat(20)}92461b87`;
+ const result = await inspectBadges("9007199254740993", commitment, 2);
+ expect(result.disclosures).toBe(2);
+ expect(result.summaries.get("90071992547409.93")).toBe(
+ "Exact token amount: 90071992547409.93. Inspect exact value",
+ );
+ expect(result.summaries.get(commitment)).toBe(
+ `Exact NFT commitment: ${commitment}. Inspect exact value`,
+ );
+ expect(result.text).toContain("≈90.1t");
+ expect(result.text).toContain("TEST");
+ expect(result.text).toContain("90071992547409.93");
+ expect(result.text).toContain("9007199254740993");
+ expect(result.text).toContain("0000000......92461b87");
+ expect(result.text).toContain(commitment);
+ expect(result.copyButtons).toBe(3);
+ });
+
+ test("scales with actual registry metadata before selecting a compact suffix", async () => {
+ const result = await inspectBadges("200000", null, 2);
+ expect(result.text).toContain("2k");
+ expect(result.summaries.has("2000")).toBe(true);
+ expect(result.copyButtons).toBe(2);
+ });
+
+ test("identifies base units when metadata is unavailable", async () => {
+ const result = await inspectBadges("2000", null);
+ expect(result.text).toContain("2k");
+ expect(result.summaries.get("2000")).toContain("metadata unavailable");
+ expect(result.copyButtons).toBe(1);
+ });
+
+ test("preserves an explicit zero and short leading-zero commitment", async () => {
+ const result = await inspectBadges("0", "0001", 0);
+ expect(result.summaries.has("0")).toBe(true);
+ expect(result.summaries.has("0001")).toBe(true);
+ expect(result.copyButtons).toBe(2);
+ });
+
+ test("omits absent values and does not invent a commitment", async () => {
+ const result = await inspectBadges(null, "");
+ expect(result.disclosures).toBe(0);
+ expect(result.copyButtons).toBe(0);
+ });
+
+ test("shows invalid amounts as unavailable instead of zero", async () => {
+ const result = await inspectBadges("not-an-integer", null, 0);
+ expect(result.text).toContain("Amount unavailable");
+ expect(result.disclosures).toBe(0);
+ expect(result.copyButtons).toBe(0);
+ });
+});
diff --git a/tests/token-badge-component.test.ts b/tests/token-badge-component.test.ts
index 96d5684..598420f 100644
--- a/tests/token-badge-component.test.ts
+++ b/tests/token-badge-component.test.ts
@@ -1,192 +1,21 @@
-import { describe, expect, test } from "bun:test";
-import { compileScript, parse } from "@vue/compiler-sfc";
-import { renderToString } from "@vue/server-renderer";
-import { plugin } from "bun";
-import { createPinia } from "pinia";
-import {
- computed,
- createSSRApp,
- defineComponent,
- h,
- onMounted,
- ref,
- shallowReactive,
- watch,
-} from "vue";
+import { expect, test } from "bun:test";
+import { fileURLToPath } from "node:url";
-// Exercise the real SFCs without adding a second Vue test runner or DOM library.
-plugin({
- name: "token-badge-vue-sfc",
- setup(build) {
- build.onLoad({ filter: /\.vue$/ }, async ({ path }) => {
- const { descriptor } = parse(await Bun.file(path).text(), {
- filename: path,
- });
- const script = compileScript(descriptor, {
- id: path,
- inlineTemplate: true,
- });
- return { contents: script.content, loader: "ts" };
- });
- },
-});
-
-// Nuxt supplies these auto-imports in production.
-Object.assign(globalThis, {
- computed,
- onMounted,
- ref,
- shallowReactive,
- watch,
- gql: (source: string) => source,
-});
-const { useRegistryStore } = await import("../src/store/registry");
-const { default: TransactionOperationToken } = await import(
- "../src/components/Transaction/TransactionOperationToken.vue"
-);
-const { default: TokenBadgeValue } = await import(
- "../src/components/Transaction/TokenBadgeValue.vue"
-);
-const { default: BaseCopy } = await import("../src/components/BaseCopy.vue");
-
-async function inspectBadges(
- tokenAmount: string | null,
- tokenCommitment: string | null,
- decimals?: number,
-) {
- const category = "ab".repeat(32);
- const app = createSSRApp({
- setup() {
- const registry = useRegistryStore();
- if (decimals !== undefined) {
- registry.registryList.push({
- name: "Test registry",
- url: "auth_chain",
- enable: true,
- default: false,
- });
- registry.authchains.set(category, {
- version: { major: 2, minor: 0, patch: 0 },
- latestRevision: "2024-01-01T00:00:00.000Z",
- registryIdentity: "Test registry",
- identities: {
- [category]: {
- "2024-01-01T00:00:00.000Z": {
- name: "Test token",
- token: { category, decimals, symbol: "TEST" },
- },
- },
- },
- });
- }
- return () =>
- h(TransactionOperationToken, {
- category,
- tokenAmount,
- tokenCommitment,
- tokenCapability: "none",
- });
- },
- });
- app.use(createPinia());
- app.component("TransactionTokenBadgeValue", TokenBadgeValue);
- app.component("BaseCopy", BaseCopy);
- app.component(
- "NuxtLink",
- defineComponent({
- props: { to: String },
- setup(props, { slots }) {
- return () => h("a", { href: props.to }, slots.default?.());
- },
- }),
+test("Vue exact token badge and copy regressions", async () => {
+ const fixture = fileURLToPath(
+ new URL("./fixtures/token-badge-component.ts", import.meta.url),
);
-
- const inspection = {
- summaries: new Map(),
- copyButtons: 0,
- disclosures: 0,
- text: "",
- };
- await new HTMLRewriter()
- .on("summary", {
- element(element) {
- inspection.summaries.set(
- element.getAttribute("title") || "",
- element.getAttribute("aria-label"),
- );
- },
- })
- .on("button[aria-label='Copy to clipboard']", {
- element() {
- inspection.copyButtons++;
- },
- })
- .on("details", {
- element() {
- inspection.disclosures++;
- },
- })
- .on("div", {
- text(chunk) {
- inspection.text += chunk.text;
- },
- })
- .transform(new Response(await renderToString(app)))
- .text();
- return inspection;
-}
-
-describe("transaction badges rendered with the registry and BaseCopy", () => {
- test("keeps compact summaries separate from exact decimal and base-unit copy values", async () => {
- const commitment = `0000000${"ab".repeat(20)}92461b87`;
- const result = await inspectBadges("9007199254740993", commitment, 2);
- expect(result.disclosures).toBe(2);
- expect(result.summaries.get("90071992547409.93")).toBe(
- "Exact token amount: 90071992547409.93. Inspect exact value",
- );
- expect(result.summaries.get(commitment)).toBe(
- `Exact NFT commitment: ${commitment}. Inspect exact value`,
- );
- expect(result.text).toContain("≈90.1t");
- expect(result.text).toContain("TEST");
- expect(result.text).toContain("90071992547409.93");
- expect(result.text).toContain("9007199254740993");
- expect(result.text).toContain("0000000......92461b87");
- expect(result.text).toContain(commitment);
- expect(result.copyButtons).toBe(3);
- });
-
- test("scales with actual registry metadata before selecting a compact suffix", async () => {
- const result = await inspectBadges("200000", null, 2);
- expect(result.text).toContain("2k");
- expect(result.summaries.has("2000")).toBe(true);
- expect(result.copyButtons).toBe(2);
- });
-
- test("identifies base units when metadata is unavailable", async () => {
- const result = await inspectBadges("2000", null);
- expect(result.text).toContain("2k");
- expect(result.summaries.get("2000")).toContain("metadata unavailable");
- expect(result.copyButtons).toBe(1);
- });
-
- test("preserves an explicit zero and short leading-zero commitment", async () => {
- const result = await inspectBadges("0", "0001", 0);
- expect(result.summaries.has("0")).toBe(true);
- expect(result.summaries.has("0001")).toBe(true);
- expect(result.copyButtons).toBe(2);
- });
-
- test("omits absent values and does not invent a commitment", async () => {
- const result = await inspectBadges(null, "");
- expect(result.disclosures).toBe(0);
- expect(result.copyButtons).toBe(0);
- });
-
- test("shows invalid amounts as unavailable instead of zero", async () => {
- const result = await inspectBadges("not-an-integer", null, 0);
- expect(result.text).toContain("Amount unavailable");
- expect(result.disclosures).toBe(0);
- expect(result.copyButtons).toBe(0);
+ const child = Bun.spawn([process.execPath, "test", fixture], {
+ stdout: "pipe",
+ stderr: "pipe",
});
+ const [stdout, stderr, exitCode] = await Promise.all([
+ new Response(child.stdout).text(),
+ new Response(child.stderr).text(),
+ child.exited,
+ ]);
+ expect({
+ exitCode,
+ output: exitCode === 0 ? "passed" : stdout + stderr,
+ }).toEqual({ exitCode: 0, output: "passed" });
});
diff --git a/tests/transaction-token.test.ts b/tests/transaction-token.test.ts
deleted file mode 100644
index 3ba172a..0000000
--- a/tests/transaction-token.test.ts
+++ /dev/null
@@ -1,157 +0,0 @@
-import { describe, expect, test } from "bun:test";
-import { plugin } from "bun";
-import { createPinia } from "pinia";
-import {
- computed,
- createSSRApp,
- defineComponent,
- h,
- onMounted,
- ref,
- shallowReactive,
- watch,
-} from "vue";
-import { compileScript, parse } from "vue/compiler-sfc";
-import { renderToString } from "vue/server-renderer";
-
-// Compile the real badge, including its template, without a browser or new deps.
-plugin({
- name: "transaction-token-vue",
- setup(build) {
- build.onLoad(
- { filter: /TransactionOperationToken\.vue$/ },
- async ({ path }) => {
- const { descriptor } = parse(await Bun.file(path).text(), {
- filename: path,
- });
- const script = compileScript(descriptor, {
- id: path,
- inlineTemplate: true,
- });
- return { contents: script.content, loader: "ts" };
- },
- );
- },
-});
-
-// Nuxt supplies these auto-imports in the app; retain real Vue/Pinia behavior.
-Object.assign(globalThis, {
- computed,
- onMounted,
- ref,
- shallowReactive,
- watch,
- gql: (source: string) => source,
-});
-
-const { default: TransactionOperationToken } = await import(
- "../src/components/Transaction/TransactionOperationToken.vue"
-);
-const { useRegistryStore } = await import("../src/store/registry");
-
-const category = "0123456789abcdef".repeat(4);
-const categoryLabel = "01234567...bcdef";
-const NuxtLink = defineComponent({
- props: { to: String },
- setup(props, { slots }) {
- return () => h("a", { href: props.to }, slots.default?.());
- },
-});
-
-async function renderBadge(
- props: Partial["$props"]> = {},
- decimals?: number,
-) {
- const app = createSSRApp({
- setup() {
- if (decimals !== undefined) {
- const store = useRegistryStore();
- store.registryList.push({
- name: "Test registry",
- url: "auth_chain",
- enable: true,
- default: false,
- });
- store.authchains.set(category, {
- version: { major: 2, minor: 0, patch: 0 },
- latestRevision: "2024-01-01T00:00:00.000Z",
- registryIdentity: "Test registry",
- identities: {
- [category]: {
- "2024-01-01T00:00:00.000Z": {
- name: "Test token",
- token: { category, symbol: "TEST", decimals },
- },
- },
- },
- });
- }
- return () =>
- h(TransactionOperationToken, {
- category,
- tokenAmount: undefined,
- tokenCapability: undefined,
- tokenCommitment: undefined,
- ...props,
- });
- },
- })
- .use(createPinia())
- .component("NuxtLink", NuxtLink);
-
- return (await renderToString(app))
- .replace(/<[^>]*>/g, " ")
- .replace(/\s+/g, " ")
- .trim();
-}
-
-describe("transaction token badge", () => {
- test.each([
- "1",
- "9007199254740993",
- "72624976668144285",
- "9223372036854775807",
- ])("renders the exact raw amount %s", async (tokenAmount) => {
- expect(await renderBadge({ tokenAmount })).toBe(
- `${categoryLabel} • ${tokenAmount}`,
- );
- });
-
- test("registry decimals do not change transaction raw-unit semantics", async () => {
- expect(await renderBadge({ tokenAmount: "72624976668144285" }, 8)).toBe(
- `${categoryLabel} • 72624976668144285 TEST`,
- );
- });
-
- test("zero retains the category without an NFT's fungible amount label", async () => {
- expect(await renderBadge({ tokenAmount: "0" })).toBe(`${categoryLabel} •`);
- });
-
- test.each([undefined, null])(
- "missing amount %s stays absent",
- async (tokenAmount) => {
- expect(await renderBadge({ tokenAmount })).toBe("");
- },
- );
-
- test.each(["none", "mutable", "minting"] as const)(
- "NFT %s commitment and capability survive without a fungible amount",
- async (tokenCapability) => {
- expect(
- await renderBadge({ tokenCapability, tokenCommitment: "00ff" }),
- ).toBe(
- `NFT: 00ff${tokenCapability === "none" ? "" : ` ${tokenCapability} nft`}`,
- );
- },
- );
-
- test("combined fungible and NFT output keeps the full amount and commitment", async () => {
- expect(
- await renderBadge({
- tokenAmount: "9223372036854775807",
- tokenCapability: "mutable",
- tokenCommitment: "00ff",
- }),
- ).toBe(`${categoryLabel} • 9223372036854775807 NFT: 00ff mutable nft`);
- });
-});