@cheasim/clawdex-channel 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,140 @@
1
+ const baseUrl = (process.env.CLAWDEX_CONTROL_PLANE_BASE_URL || "http://127.0.0.1:3000/api").replace(/\/$/, "");
2
+ const token = process.env.CLAWDEX_PLUGIN_TOKEN || "";
3
+ const stamp = Date.now();
4
+
5
+ function buildHeaders() {
6
+ const headers = {
7
+ "Content-Type": "application/json",
8
+ };
9
+
10
+ if (token) {
11
+ headers.Authorization = `Bearer ${token}`;
12
+ }
13
+
14
+ return headers;
15
+ }
16
+
17
+ async function request(path, init = {}) {
18
+ const response = await fetch(`${baseUrl}${path}`, {
19
+ ...init,
20
+ headers: {
21
+ ...buildHeaders(),
22
+ ...(init.headers || {}),
23
+ },
24
+ });
25
+
26
+ const payload = await response.json().catch(() => ({}));
27
+
28
+ if (!response.ok) {
29
+ throw new Error(`${init.method || "GET"} ${path} failed: ${payload.message || response.status}`);
30
+ }
31
+
32
+ return payload;
33
+ }
34
+
35
+ async function main() {
36
+ console.log("[selftest] discovery");
37
+ const discovery = await request("/openclaw/plugin/discovery");
38
+ console.log(JSON.stringify(discovery, null, 2));
39
+
40
+ console.log("[selftest] provision challenger");
41
+ const challenger = await request("/openclaw/plugin/accounts/provision", {
42
+ method: "POST",
43
+ body: JSON.stringify({
44
+ email: `challenger-${stamp}@agents.clawdex.local`,
45
+ name: `SelfTest Challenger ${String(stamp).slice(-4)}`,
46
+ channel: "Clawdex SelfTest Channel",
47
+ accountId: `stc-${String(stamp).slice(-6)}`,
48
+ clientVersion: "selftest",
49
+ autoReady: true,
50
+ }),
51
+ });
52
+ console.log(JSON.stringify(challenger, null, 2));
53
+
54
+ console.log("[selftest] provision defender");
55
+ const defender = await request("/openclaw/plugin/accounts/provision", {
56
+ method: "POST",
57
+ body: JSON.stringify({
58
+ email: `defender-${stamp}@agents.clawdex.local`,
59
+ name: `SelfTest Defender ${String(stamp).slice(-4)}`,
60
+ channel: "Clawdex SelfTest Channel",
61
+ accountId: `std-${String(stamp).slice(-6)}`,
62
+ clientVersion: "selftest",
63
+ autoReady: true,
64
+ }),
65
+ });
66
+ console.log(JSON.stringify(defender, null, 2));
67
+
68
+ const challengerSlug = challenger.player?.slug;
69
+ const defenderSlug = defender.player?.slug;
70
+
71
+ if (!challengerSlug || !defenderSlug) {
72
+ throw new Error("Self-test failed to resolve provisioned player slugs");
73
+ }
74
+
75
+ console.log("[selftest] readiness");
76
+ const readiness = await Promise.all([
77
+ request(`/openclaw/plugin/readiness?playerSlug=${encodeURIComponent(challengerSlug)}`),
78
+ request(`/openclaw/plugin/readiness?playerSlug=${encodeURIComponent(defenderSlug)}`),
79
+ ]);
80
+ console.log(JSON.stringify(readiness, null, 2));
81
+
82
+ console.log("[selftest] create challenge");
83
+ const created = await request("/openclaw/plugin/challenges", {
84
+ method: "POST",
85
+ body: JSON.stringify({
86
+ challengerSlug,
87
+ defenderSlug,
88
+ mode: "public-arena",
89
+ stake: 20,
90
+ scheduledFor: "即刻开战",
91
+ visibility: "public",
92
+ rulesNote: "Created by scripts/selftest.mjs",
93
+ }),
94
+ });
95
+ console.log(JSON.stringify(created, null, 2));
96
+
97
+ const challengeId = created.challenge?.id;
98
+
99
+ if (!challengeId) {
100
+ throw new Error("Self-test did not receive challengeId");
101
+ }
102
+
103
+ console.log("[selftest] accept challenge");
104
+ const accepted = await request(`/openclaw/plugin/challenges/${challengeId}/accept`, {
105
+ method: "POST",
106
+ body: JSON.stringify({
107
+ defenderSlug,
108
+ sourceChannel: "clawdex-channel",
109
+ sourceSessionId: `selftest-accept-${stamp}`,
110
+ }),
111
+ });
112
+ console.log(JSON.stringify(accepted, null, 2));
113
+
114
+ console.log("[selftest] settle challenge");
115
+ const settled = await request(`/openclaw/plugin/challenges/${challengeId}/settle`, {
116
+ method: "POST",
117
+ body: JSON.stringify({
118
+ winnerSlug: challengerSlug,
119
+ settlementSummary: "HTTP self-test completed successfully.",
120
+ sourceChannel: "clawdex-channel",
121
+ sourceSessionId: `selftest-settle-${stamp}`,
122
+ }),
123
+ });
124
+ console.log(JSON.stringify(settled, null, 2));
125
+
126
+ console.log("[selftest] credit snapshots");
127
+ const credits = await Promise.all([
128
+ request(`/openclaw/plugin/credits?playerSlug=${encodeURIComponent(challengerSlug)}`),
129
+ request(`/openclaw/plugin/credits?playerSlug=${encodeURIComponent(defenderSlug)}`),
130
+ ]);
131
+ console.log(JSON.stringify(credits, null, 2));
132
+
133
+ console.log("[selftest] complete");
134
+ }
135
+
136
+ main().catch((error) => {
137
+ console.error("[selftest] failed");
138
+ console.error(error instanceof Error ? error.message : error);
139
+ process.exitCode = 1;
140
+ });