-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathplayground.test.ts
More file actions
905 lines (835 loc) · 31.9 KB
/
Copy pathplayground.test.ts
File metadata and controls
905 lines (835 loc) · 31.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
// Copyright (C) Parity Technologies (UK) Ltd.
// SPDX-License-Identifier: Apache-2.0
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
import { describe, it, expect, vi, beforeEach } from "vitest";
import { mkdtempSync, rmSync, writeFileSync } from "node:fs";
import { tmpdir } from "node:os";
import { join } from "node:path";
const { captureWarningMock, withSpanMock, bulletinStorageSigner, getBulletinAllowanceSignerMock } =
vi.hoisted(() => ({
captureWarningMock: vi.fn(),
withSpanMock: vi.fn(async (_op: string, _name: string, _attrs: any, fn: any) => fn()),
bulletinStorageSigner: { __signer: "bulletin-allowance" },
getBulletinAllowanceSignerMock: vi.fn(async (_options: unknown) => ({
__signer: "bulletin-allowance",
})),
}));
// Mock the metadata upload path so we never actually touch the network.
// The mock returns a fake CID that registry publishing treats as the metadata CID.
vi.mock("@parity/product-sdk-cloud-storage", () => ({
calculateCid: vi.fn(async () => ({ toString: (): string => "bafymeta" })),
}));
vi.mock("@parity/product-sdk-tx", () => ({
createDevSigner: vi.fn(() => ({ __devSigner: "Alice" })),
submitAndWatch: vi.fn(async () => ({ ok: true, block: { hash: "0x0" } })),
withRetry: vi.fn((fn: () => Promise<unknown>) => fn()),
}));
vi.mock("../allowances/bulletin.js", () => ({
asCloudStorageApi: (api: unknown) => api,
getBulletinAllowanceSigner: (options: unknown) => getBulletinAllowanceSignerMock(options),
isInvalidPaymentError: (err: unknown) => String(err).includes("Payment"),
}));
vi.mock("polkadot-api", () => ({
createClient: vi.fn(() => ({
getTypedApi: vi.fn(() => ({
tx: {
TransactionStorage: {
store: vi.fn((args: unknown) => ({ __kind: "store", args })),
},
},
})),
destroy: vi.fn(),
})),
}));
vi.mock("polkadot-api/ws", () => ({
getWsProvider: vi.fn(() => ({})),
}));
// Likewise stub the connection + registry helpers. We capture the publish
// arguments so we can assert on them.
const publishTx = vi.fn(async () => ({ ok: true, txHash: "0xdead" }));
const publishDevTx = vi.fn(async () => ({ ok: true, txHash: "0xfeed" }));
vi.mock("../connection.js", () => ({
getConnection: vi.fn(async () => ({ raw: { assetHub: {} } })),
}));
vi.mock("../registry.js", () => ({
getRegistryContract: vi.fn(async () => ({
publish: { tx: publishTx },
publishDev: { tx: publishDevTx },
})),
}));
vi.mock("../../telemetry.js", () => ({
captureWarning: (...args: unknown[]) => captureWarningMock(...args),
withSpan: (...args: unknown[]) =>
withSpanMock(args[0] as string, args[1] as string, args[2], args[3]),
errorMessage: (error: unknown) => (error instanceof Error ? error.message : String(error)),
}));
import { execFileSync } from "node:child_process";
import {
publishToPlayground,
buildMetadata,
normalizeDomain,
normalizeModdedFrom,
readGitBranch,
readModdedFrom,
readReadme,
README_CAP_BYTES,
} from "./playground.js";
import { submitAndWatch } from "@parity/product-sdk-tx";
import type { ResolvedSigner } from "../signer.js";
const makeTmpDir = () => mkdtempSync(join(tmpdir(), "dot-playground-test-"));
const fakeSigner: ResolvedSigner = {
signer: {} as any,
address: "5Fake",
source: "session",
destroy: () => {},
};
beforeEach(() => {
publishTx.mockClear();
publishTx.mockImplementation(async () => ({ ok: true, txHash: "0xdead" }));
publishDevTx.mockClear();
publishDevTx.mockImplementation(async () => ({ ok: true, txHash: "0xfeed" }));
captureWarningMock.mockClear();
withSpanMock.mockClear();
getBulletinAllowanceSignerMock.mockClear();
getBulletinAllowanceSignerMock.mockResolvedValue(bulletinStorageSigner);
vi.mocked(submitAndWatch).mockClear();
vi.mocked(submitAndWatch).mockResolvedValue({
ok: true,
txHash: "0x0",
block: { hash: "0x0", number: 0, index: 0 },
events: [],
});
});
describe("normalizeDomain", () => {
it("accepts a valid label and appends .dot", () => {
expect(normalizeDomain("my-app")).toEqual({ label: "my-app", fullDomain: "my-app.dot" });
});
it("strips an existing .dot suffix", () => {
expect(normalizeDomain("my-app.dot")).toEqual({
label: "my-app",
fullDomain: "my-app.dot",
});
});
it("rejects illegal characters", () => {
expect(() => normalizeDomain("My_App!")).toThrow(/Invalid domain/);
});
it("rejects uppercase (chain stores lowercase only)", () => {
expect(() => normalizeDomain("MyApp")).toThrow(/lowercase/i);
});
it("rejects labels shorter than 3 characters", () => {
expect(() => normalizeDomain("ab")).toThrow(/at least 3/i);
});
it("rejects a trailing dash", () => {
expect(() => normalizeDomain("my-app-")).toThrow(/dash/i);
});
it("rejects a one-digit suffix", () => {
expect(() => normalizeDomain("myapp1")).toThrow(/two digits/i);
});
});
describe("normalizeModdedFrom", () => {
it("returns null for omitted/empty/whitespace input", () => {
expect(normalizeModdedFrom(undefined)).toBeNull();
expect(normalizeModdedFrom(null)).toBeNull();
expect(normalizeModdedFrom("")).toBeNull();
expect(normalizeModdedFrom(" ")).toBeNull();
});
it("canonicalizes to <label>.dot, adding the suffix and trimming", () => {
expect(normalizeModdedFrom("original")).toBe("original.dot");
expect(normalizeModdedFrom("original.dot")).toBe("original.dot");
expect(normalizeModdedFrom(" original.dot ")).toBe("original.dot");
});
it("returns null for a malformed domain so it can't reach on-chain", () => {
expect(normalizeModdedFrom("Not A Domain!")).toBeNull();
expect(normalizeModdedFrom("../etc.dot")).toBeNull();
expect(normalizeModdedFrom("foo/bar.dot")).toBeNull();
});
});
describe("readReadme", () => {
it("returns content when README.md exists and fits under the cap", () => {
const dir = makeTmpDir();
try {
writeFileSync(join(dir, "README.md"), "# My App\n\nHello there.");
const status = readReadme(dir);
expect(status.kind).toBe("ok");
if (status.kind === "ok") {
expect(status.content).toBe("# My App\n\nHello there.");
expect(status.size).toBe(Buffer.byteLength(status.content, "utf8"));
}
} finally {
rmSync(dir, { recursive: true, force: true });
}
});
it("reports oversized when README.md exceeds the cap", () => {
const dir = makeTmpDir();
try {
// One byte over the default 20 KB cap.
const bigContent = "x".repeat(README_CAP_BYTES + 1);
writeFileSync(join(dir, "README.md"), bigContent);
const status = readReadme(dir);
expect(status.kind).toBe("oversized");
if (status.kind === "oversized") {
expect(status.size).toBe(README_CAP_BYTES + 1);
}
} finally {
rmSync(dir, { recursive: true, force: true });
}
});
it("returns missing when the file is not present", () => {
const dir = makeTmpDir();
try {
const status = readReadme(dir);
expect(status.kind).toBe("missing");
} finally {
rmSync(dir, { recursive: true, force: true });
}
});
it("falls back to lowercase readme.md on case-sensitive filesystems", () => {
const dir = makeTmpDir();
try {
writeFileSync(join(dir, "readme.md"), "# lower");
const status = readReadme(dir);
expect(status.kind).toBe("ok");
if (status.kind === "ok") expect(status.content).toBe("# lower");
} finally {
rmSync(dir, { recursive: true, force: true });
}
});
it("falls back to title-cased Readme.md", () => {
const dir = makeTmpDir();
try {
writeFileSync(join(dir, "Readme.md"), "# title");
const status = readReadme(dir);
expect(status.kind).toBe("ok");
if (status.kind === "ok") expect(status.content).toBe("# title");
} finally {
rmSync(dir, { recursive: true, force: true });
}
});
it("respects a custom cap", () => {
const dir = makeTmpDir();
try {
writeFileSync(join(dir, "README.md"), "abcdefghij"); // 10 bytes
expect(readReadme(dir, 5).kind).toBe("oversized");
expect(readReadme(dir, 10).kind).toBe("ok");
} finally {
rmSync(dir, { recursive: true, force: true });
}
});
});
describe("readGitBranch", () => {
// Helper: git invocations that bypass the maintainer's global gpg-signing
// and ignore-author-config settings, so the tests run identically on any
// dev machine and in CI.
const gitOpts = (cwd: string) => ({
cwd,
stdio: "ignore" as const,
env: {
...process.env,
GIT_AUTHOR_NAME: "test",
GIT_AUTHOR_EMAIL: "test@example.com",
GIT_COMMITTER_NAME: "test",
GIT_COMMITTER_EMAIL: "test@example.com",
GIT_CONFIG_GLOBAL: "/dev/null", // ignore the user's global git config (gpgsign etc.)
GIT_CONFIG_SYSTEM: "/dev/null",
},
});
it("returns null for a non-git directory", () => {
const tmp = makeTmpDir();
try {
expect(readGitBranch(tmp)).toBeNull();
} finally {
rmSync(tmp, { recursive: true, force: true });
}
});
it("returns the branch name when the repo is on a named branch", () => {
const tmp = makeTmpDir();
try {
execFileSync("git", ["init", "-b", "feature/x"], gitOpts(tmp));
writeFileSync(join(tmp, "f"), "x");
execFileSync("git", ["add", "f"], gitOpts(tmp));
execFileSync("git", ["commit", "-m", "init"], gitOpts(tmp));
expect(readGitBranch(tmp)).toBe("feature/x");
} finally {
rmSync(tmp, { recursive: true, force: true });
}
});
it("returns null in detached-HEAD state (so we never write the literal 'HEAD' to metadata)", () => {
const tmp = makeTmpDir();
try {
execFileSync("git", ["init", "-b", "main"], gitOpts(tmp));
writeFileSync(join(tmp, "f"), "x");
execFileSync("git", ["add", "f"], gitOpts(tmp));
execFileSync("git", ["commit", "-m", "init"], gitOpts(tmp));
const sha = execFileSync("git", ["rev-parse", "HEAD"], {
cwd: tmp,
encoding: "utf8",
env: gitOpts(tmp).env,
}).trim();
execFileSync("git", ["checkout", "--detach", sha], gitOpts(tmp));
expect(readGitBranch(tmp)).toBeNull();
} finally {
rmSync(tmp, { recursive: true, force: true });
}
});
});
describe("readModdedFrom", () => {
it("returns null when dot.json is missing", () => {
const dir = makeTmpDir();
try {
expect(readModdedFrom(dir)).toBeNull();
} finally {
rmSync(dir, { recursive: true, force: true });
}
});
it("returns the moddedFrom value when set", () => {
const dir = makeTmpDir();
try {
writeFileSync(join(dir, "dot.json"), JSON.stringify({ moddedFrom: "original.dot" }));
expect(readModdedFrom(dir)).toBe("original.dot");
} finally {
rmSync(dir, { recursive: true, force: true });
}
});
it("returns null when the field is absent, blank, or non-string", () => {
const dir = makeTmpDir();
try {
writeFileSync(join(dir, "dot.json"), JSON.stringify({ name: "x" }));
expect(readModdedFrom(dir)).toBeNull();
writeFileSync(join(dir, "dot.json"), JSON.stringify({ moddedFrom: " " }));
expect(readModdedFrom(dir)).toBeNull();
writeFileSync(join(dir, "dot.json"), JSON.stringify({ moddedFrom: 42 }));
expect(readModdedFrom(dir)).toBeNull();
} finally {
rmSync(dir, { recursive: true, force: true });
}
});
it("returns null when dot.json is unparseable", () => {
const dir = makeTmpDir();
try {
writeFileSync(join(dir, "dot.json"), "{ not json");
expect(readModdedFrom(dir)).toBeNull();
} finally {
rmSync(dir, { recursive: true, force: true });
}
});
it("rejects malformed domains so they can't reach published metadata", () => {
const dir = makeTmpDir();
try {
const cases = [
"<script>alert(1)</script>",
"<img onerror=fetch(0)>.dot",
"foo bar.dot",
"foo/bar.dot",
"../etc.dot",
];
for (const moddedFrom of cases) {
writeFileSync(join(dir, "dot.json"), JSON.stringify({ moddedFrom }));
expect(readModdedFrom(dir), `expected null for ${moddedFrom}`).toBeNull();
}
} finally {
rmSync(dir, { recursive: true, force: true });
}
});
it("canonicalizes to <label>.dot when the suffix is missing", () => {
const dir = makeTmpDir();
try {
writeFileSync(join(dir, "dot.json"), JSON.stringify({ moddedFrom: "original" }));
expect(readModdedFrom(dir)).toBe("original.dot");
} finally {
rmSync(dir, { recursive: true, force: true });
}
});
});
describe("buildMetadata", () => {
it("includes repository when repositoryUrl is non-null", () => {
const meta = buildMetadata({
repositoryUrl: "https://github.com/x/y",
branch: null,
readme: null,
moddedFrom: null,
tag: null,
});
expect(meta).toEqual({ repository: "https://github.com/x/y" });
});
it("omits repository entirely when repositoryUrl is null", () => {
const meta = buildMetadata({
repositoryUrl: null,
branch: null,
readme: null,
moddedFrom: null,
tag: null,
});
expect(meta.repository).toBeUndefined();
});
it("includes branch alongside repository when both are present", () => {
const meta = buildMetadata({
repositoryUrl: "https://github.com/x/y",
branch: "develop",
readme: null,
moddedFrom: null,
tag: null,
});
expect(meta).toEqual({ repository: "https://github.com/x/y", branch: "develop" });
});
it("omits branch when repositoryUrl is null (branch alone is meaningless)", () => {
const meta = buildMetadata({
repositoryUrl: null,
branch: "develop",
readme: null,
moddedFrom: null,
tag: null,
});
expect(meta.branch).toBeUndefined();
});
it("includes README when present", () => {
const meta = buildMetadata({
repositoryUrl: null,
branch: null,
readme: { kind: "ok", content: "hello", size: 5 },
moddedFrom: null,
tag: null,
});
expect(meta).toEqual({ readme: "hello" });
});
it("includes moddedFrom when present (independent of repositoryUrl)", () => {
const meta = buildMetadata({
repositoryUrl: null,
branch: null,
readme: null,
moddedFrom: "original.dot",
tag: null,
});
expect(meta).toEqual({ moddedFrom: "original.dot" });
});
it("omits moddedFrom when null or empty", () => {
const meta = buildMetadata({
repositoryUrl: "https://github.com/x/y",
branch: null,
readme: null,
moddedFrom: null,
tag: null,
});
expect(meta.moddedFrom).toBeUndefined();
});
it("includes tag when present (independent of repositoryUrl)", () => {
const meta = buildMetadata({
repositoryUrl: null,
branch: null,
readme: null,
moddedFrom: null,
tag: "defi",
});
expect(meta).toEqual({ tag: "defi" });
});
it("omits tag when null", () => {
const meta = buildMetadata({
repositoryUrl: "https://github.com/x/y",
branch: null,
readme: null,
moddedFrom: null,
tag: null,
});
expect(meta.tag).toBeUndefined();
});
});
describe("publishToPlayground", () => {
// Every test needs a cwd that doesn't accidentally pick up the repo's own
// README.md (the CLI's real README is ~10 KB and would be inlined if we
// defaulted to `process.cwd()`). Each test opts into a tmpdir explicitly.
it("uploads metadata JSON with the Bulletin allowance signer and calls registry.publish with the phone signer", async () => {
const dir = makeTmpDir();
try {
const result = await publishToPlayground({
domain: "my-app",
publishSigner: fakeSigner,
repositoryUrl: "https://github.com/paritytech/example",
cwd: dir,
});
expect(result.fullDomain).toBe("my-app.dot");
expect(result.metadata).toEqual({
repository: "https://github.com/paritytech/example",
});
expect(result.metadataCid).toBe("bafymeta");
expect(submitAndWatch).toHaveBeenCalledWith(
expect.objectContaining({ __kind: "store" }),
bulletinStorageSigner,
);
expect(publishTx).toHaveBeenCalledWith(
"my-app.dot",
"bafymeta",
1,
{
isSome: false,
value: "0x0000000000000000000000000000000000000000",
},
"",
false,
false,
);
expect(publishDevTx).not.toHaveBeenCalled();
} finally {
rmSync(dir, { recursive: true, force: true });
}
});
it("omits the repository field when repositoryUrl is null", async () => {
const result = await publishToPlayground({
domain: "my-app.dot",
publishSigner: fakeSigner,
repositoryUrl: null,
cwd: "/definitely/not/a/repo",
});
expect(result.metadata).toEqual({});
});
it("records the chosen tag in the uploaded metadata JSON", async () => {
const result = await publishToPlayground({
domain: "tagged-app",
publishSigner: fakeSigner,
repositoryUrl: null,
tag: "gaming",
cwd: "/definitely/not/a/repo",
});
expect(result.metadata).toEqual({ tag: "gaming" });
});
it("inlines README.md when it is present and within the cap", async () => {
const dir = makeTmpDir();
try {
writeFileSync(join(dir, "README.md"), "# Hello\n\nA short readme.");
const result = await publishToPlayground({
domain: "readme-app",
publishSigner: fakeSigner,
repositoryUrl: "https://example.com/r",
cwd: dir,
});
expect(result.metadata).toEqual({
repository: "https://example.com/r",
readme: "# Hello\n\nA short readme.",
});
} finally {
rmSync(dir, { recursive: true, force: true });
}
});
it("omits readme when README.md exceeds the cap", async () => {
const dir = makeTmpDir();
try {
writeFileSync(join(dir, "README.md"), "x".repeat(README_CAP_BYTES + 1));
const result = await publishToPlayground({
domain: "big-readme",
publishSigner: fakeSigner,
repositoryUrl: "https://example.com/r",
cwd: dir,
});
expect(result.metadata).toEqual({ repository: "https://example.com/r" });
} finally {
rmSync(dir, { recursive: true, force: true });
}
});
it("passes claimedOwnerH160 through as the registry.publishDev owner argument", async () => {
await publishToPlayground({
domain: "claimed-app",
publishSigner: fakeSigner,
repositoryUrl: null,
cwd: "/definitely/not/a/repo",
claimedOwnerH160: "0x1234567890abcdef1234567890abcdef12345678",
isDevSigner: true,
});
expect(publishTx).not.toHaveBeenCalled();
expect(publishDevTx).toHaveBeenCalledWith(
"claimed-app.dot",
"bafymeta",
1,
{
isSome: true,
value: "0x1234567890abcdef1234567890abcdef12345678",
},
"",
false,
);
});
it("passes visibility=0 when isPrivate is true", async () => {
await publishToPlayground({
domain: "secret",
publishSigner: fakeSigner,
repositoryUrl: "https://example.com/x",
cwd: "/definitely/not/a/repo",
isPrivate: true,
});
expect(publishTx).toHaveBeenCalledWith(
"secret.dot",
"bafymeta",
0,
{
isSome: false,
value: "0x0000000000000000000000000000000000000000",
},
"",
false,
false,
);
});
it("routes dev signer publishes through registry.publishDev", async () => {
await publishToPlayground({
domain: "modded-by-dev",
publishSigner: fakeSigner,
repositoryUrl: "https://github.com/foo/bar",
cwd: "/definitely/not/a/repo",
isModdable: true,
isDevSigner: true,
});
expect(publishTx).not.toHaveBeenCalled();
expect(publishDevTx).toHaveBeenCalledWith(
"modded-by-dev.dot",
"bafymeta",
1,
{
isSome: false,
value: "0x0000000000000000000000000000000000000000",
},
"",
true,
);
});
it("forwards moddedFrom captured by `dot mod` in dot.json to registry.publish", async () => {
const dir = makeTmpDir();
try {
writeFileSync(join(dir, "dot.json"), JSON.stringify({ moddedFrom: "original.dot" }));
await publishToPlayground({
domain: "my-mod",
publishSigner: fakeSigner,
repositoryUrl: null,
cwd: dir,
});
expect(publishTx).toHaveBeenCalledWith(
"my-mod.dot",
"bafymeta",
1,
{
isSome: false,
value: "0x0000000000000000000000000000000000000000",
},
"original.dot",
false,
false,
);
} finally {
rmSync(dir, { recursive: true, force: true });
}
});
// playground-app#335: an SDK/RevX consumer that clones the source itself
// (skipping the `dot mod` TUI that rewrites `dot.json`) passes the true
// immediate parent via the `moddedFrom` option. It MUST win over the stale
// value the cloned repo's `dot.json` carried — otherwise a mod-of-a-mod
// re-publishes the grandparent and credits the wrong owner.
it("prefers an explicit moddedFrom option over a stale dot.json value", async () => {
const dir = makeTmpDir();
try {
// Stale value inherited from the cloned source (e.g. tutorial).
writeFileSync(
join(dir, "dot.json"),
JSON.stringify({ moddedFrom: "playground-tutorial.dot" }),
);
const result = await publishToPlayground({
domain: "my-mod",
publishSigner: fakeSigner,
repositoryUrl: null,
cwd: dir,
// True immediate parent, known by the caller that did the mod.
moddedFrom: "steampunk-lizard-spock01.dot",
});
expect(publishTx).toHaveBeenCalledWith(
"my-mod.dot",
"bafymeta",
1,
{
isSome: false,
value: "0x0000000000000000000000000000000000000000",
},
"steampunk-lizard-spock01.dot",
false,
false,
);
// The explicit value must ALSO drive the metadata JSON, not just the
// on-chain arg — otherwise the badge and the XP edge disagree.
expect(result.metadata.moddedFrom).toBe("steampunk-lizard-spock01.dot");
} finally {
rmSync(dir, { recursive: true, force: true });
}
});
// The contract matches lineage on the canonical `<label>.dot` string, so a
// caller passing a bare label or trailing `.dot`-less value must still land
// on the canonical form — same as the `dot.json` path goes through
// `normalizeDomain`.
it("canonicalizes a non-`.dot` explicit moddedFrom before publishing", async () => {
const dir = makeTmpDir();
try {
const result = await publishToPlayground({
domain: "my-mod",
publishSigner: fakeSigner,
repositoryUrl: null,
cwd: dir,
moddedFrom: "steampunk-lizard-spock01",
});
expect(publishTx).toHaveBeenCalledWith(
"my-mod.dot",
"bafymeta",
1,
{
isSome: false,
value: "0x0000000000000000000000000000000000000000",
},
"steampunk-lizard-spock01.dot",
false,
false,
);
expect(result.metadata.moddedFrom).toBe("steampunk-lizard-spock01.dot");
} finally {
rmSync(dir, { recursive: true, force: true });
}
});
it("falls back to dot.json when the explicit moddedFrom is empty/whitespace", async () => {
const dir = makeTmpDir();
try {
writeFileSync(join(dir, "dot.json"), JSON.stringify({ moddedFrom: "original.dot" }));
const result = await publishToPlayground({
domain: "my-mod",
publishSigner: fakeSigner,
repositoryUrl: null,
cwd: dir,
moddedFrom: " ",
});
expect(publishTx).toHaveBeenCalledWith(
"my-mod.dot",
"bafymeta",
1,
{
isSome: false,
value: "0x0000000000000000000000000000000000000000",
},
"original.dot",
false,
false,
);
expect(result.metadata.moddedFrom).toBe("original.dot");
} finally {
rmSync(dir, { recursive: true, force: true });
}
});
// A malformed explicit value is treated as "not provided" (it can't reach
// on-chain), so it falls through to the `dot.json` value rather than
// recording garbage or wiping the lineage edge.
it("falls back to dot.json when the explicit moddedFrom is malformed", async () => {
const dir = makeTmpDir();
try {
writeFileSync(join(dir, "dot.json"), JSON.stringify({ moddedFrom: "original.dot" }));
const result = await publishToPlayground({
domain: "my-mod",
publishSigner: fakeSigner,
repositoryUrl: null,
cwd: dir,
moddedFrom: "Not A Domain!",
});
expect(publishTx).toHaveBeenCalledWith(
"my-mod.dot",
"bafymeta",
1,
{
isSome: false,
value: "0x0000000000000000000000000000000000000000",
},
"original.dot",
false,
false,
);
expect(result.metadata.moddedFrom).toBe("original.dot");
} finally {
rmSync(dir, { recursive: true, force: true });
}
});
it("retries up to 3 times on registry publish failure", async () => {
publishTx.mockImplementationOnce(async () => {
throw new Error("nonce race");
});
publishTx.mockImplementationOnce(async () => {
throw new Error("nonce race");
});
publishTx.mockImplementationOnce(async () => ({ ok: true, txHash: "0xbeef" }));
const result = await publishToPlayground({
domain: "flaky",
publishSigner: fakeSigner,
repositoryUrl: "https://example.com/x",
cwd: "/definitely/not/a/repo",
});
expect(publishTx).toHaveBeenCalledTimes(3);
expect(result.fullDomain).toBe("flaky.dot");
}, 30_000);
it("captures a warning when registry publish retries", async () => {
publishTx
.mockRejectedValueOnce(new Error("temporary registry failure"))
.mockResolvedValueOnce({ ok: true, txHash: "0xdead" });
await publishToPlayground({
domain: "my-app.dot",
publishSigner: fakeSigner,
repositoryUrl: null,
cwd: undefined,
});
expect(captureWarningMock).toHaveBeenCalledWith(
"Playground registry publish failed, retrying",
expect.objectContaining({
attempt: 1,
maxAttempts: 3,
error: "temporary registry failure",
}),
);
}, 30_000);
it("wraps metadata upload and registry publish in spans", async () => {
await publishToPlayground({
domain: "my-app.dot",
publishSigner: fakeSigner,
repositoryUrl: null,
cwd: undefined,
});
const ops = withSpanMock.mock.calls.map((call) => call[0]);
expect(ops).toContain("cli.deploy.playground.metadata-upload");
expect(ops).toContain("cli.deploy.playground.registry-publish");
});
it("re-checks Bulletin allowance once when metadata upload fails with Invalid Payment", async () => {
vi.mocked(submitAndWatch)
.mockRejectedValueOnce(new Error('{"type":"Invalid","value":{"type":"Payment"}}'))
.mockResolvedValueOnce({
ok: true,
txHash: "0x1",
block: { hash: "0x1", number: 1, index: 0 },
events: [],
});
await publishToPlayground({
domain: "my-app.dot",
publishSigner: fakeSigner,
repositoryUrl: null,
cwd: undefined,
});
expect(getBulletinAllowanceSignerMock).toHaveBeenCalledTimes(2);
expect(submitAndWatch).toHaveBeenCalledTimes(2);
});
it("surfaces the last error after exhausting retries", async () => {
publishTx.mockImplementation(async () => {
throw new Error("unauthorized");
});
await expect(
publishToPlayground({
domain: "doomed",
publishSigner: fakeSigner,
repositoryUrl: "https://example.com/x",
cwd: "/definitely/not/a/repo",
}),
).rejects.toThrow(/unauthorized/);
}, 30_000);
});