The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 03:37 UTC
| 1 | const assert = require("node:assert/strict"); |
| 2 | const test = require("node:test"); |
| 3 | |
| 4 | const { main, mergeState, readState, render } = require("./mobile-preview-comment.cjs"); |
| 5 | |
| 6 | const fingerprint = "c31608b37a8a2dca35ccee39cfd2ec7305042629"; |
| 7 | const run = "https://github.com/pingdotgg/t3code/actions/runs/1"; |
| 8 | |
| 9 | test("the comment round-trips its state", () => { |
| 10 | const state = { |
| 11 | android: { fingerprint, client: "", group: "8e413a0a" }, |
| 12 | ios: { fingerprint: "ios-fingerprint", client: "https://expo.dev/ios", group: "ios-group" }, |
| 13 | }; |
| 14 | const body = render(state, run); |
| 15 | assert.deepEqual(readState(body), state); |
| 16 | assert.match(body, /Client unavailable/); |
| 17 | assert.match(body, /groupId=8e413a0a/); |
| 18 | assert.match(body, /groupId=ios-group/); |
| 19 | assert.match(body, /\[Install\]\(https:\/\/expo.dev\/ios\)/); |
| 20 | assert.equal(readState("🚀 Expo continuous deployment is ready!"), null); |
| 21 | }); |
| 22 | |
| 23 | test("the build job adds the install link without losing the update", () => { |
| 24 | const first = mergeState(null, { fingerprint, client: "", group: "8e413a0a" }); |
| 25 | const built = mergeState(first, { fingerprint, client: "https://expo.dev/a/b", group: "" }); |
| 26 | assert.deepEqual(built, { fingerprint, client: "https://expo.dev/a/b", group: "8e413a0a" }); |
| 27 | assert.match( |
| 28 | render({ android: built }, run), |
| 29 | /Dev client \| \[Install\]\(https:\/\/expo\.dev\/a\/b\)/, |
| 30 | ); |
| 31 | }); |
| 32 | |
| 33 | test("only a successful publication changes the fingerprint and client", () => { |
| 34 | const built = { fingerprint, client: "https://expo.dev/a/b", group: "old" }; |
| 35 | assert.equal(mergeState(null, { fingerprint, client: "", group: "" }), null); |
| 36 | assert.deepEqual( |
| 37 | mergeState(built, { fingerprint: "f".repeat(40), client: "", group: "" }), |
| 38 | built, |
| 39 | ); |
| 40 | assert.deepEqual(mergeState(built, { fingerprint, client: "", group: "new" }), { |
| 41 | fingerprint, |
| 42 | client: "https://expo.dev/a/b", |
| 43 | group: "new", |
| 44 | }); |
| 45 | assert.deepEqual(mergeState(built, { fingerprint: "f".repeat(40), client: "", group: "new" }), { |
| 46 | fingerprint: "f".repeat(40), |
| 47 | client: "", |
| 48 | group: "new", |
| 49 | }); |
| 50 | }); |
| 51 | |
| 52 | test("a build that finishes after the PR moved to another fingerprint changes nothing", () => { |
| 53 | const current = { fingerprint: "f".repeat(40), client: "", group: "new" }; |
| 54 | assert.deepEqual( |
| 55 | mergeState(current, { fingerprint, client: "https://expo.dev/a/b", group: "" }), |
| 56 | current, |
| 57 | ); |
| 58 | }); |
| 59 | |
| 60 | test("state from the comment is always three strings", () => { |
| 61 | assert.deepEqual( |
| 62 | readState('<!-- mobile-preview-android {"fingerprint":5,"client":null} -->\nx'), |
| 63 | { android: { fingerprint: "", client: "", group: "" } }, |
| 64 | ); |
| 65 | }); |
| 66 | |
| 67 | test("publishing consolidates platform comments and preserves unrelated comments", async () => { |
| 68 | const android = { fingerprint, client: "https://expo.dev/android", group: "android-group" }; |
| 69 | const ios = { |
| 70 | fingerprint: "ios-fingerprint", |
| 71 | client: "https://expo.dev/ios", |
| 72 | group: "ios-group", |
| 73 | }; |
| 74 | const comments = new Map([ |
| 75 | [ |
| 76 | 1, |
| 77 | { |
| 78 | id: 1, |
| 79 | user: { login: "github-actions[bot]" }, |
| 80 | body: `<!-- mobile-preview-android ${JSON.stringify(android)} -->`, |
| 81 | }, |
| 82 | ], |
| 83 | [ |
| 84 | 2, |
| 85 | { |
| 86 | id: 2, |
| 87 | user: { login: "github-actions[bot]" }, |
| 88 | body: `<!-- mobile-preview-ios ${JSON.stringify(ios)} -->`, |
| 89 | }, |
| 90 | ], |
| 91 | [ |
| 92 | 4, |
| 93 | { |
| 94 | id: 4, |
| 95 | user: { login: "github-actions[bot]" }, |
| 96 | body: "<!-- continuous-deploy-fingerprint-projectId:d763fcb8-d37c-41ea-a773-b54a0ab4a454 -->\nOld Expo preview", |
| 97 | }, |
| 98 | ], |
| 99 | [ |
| 100 | 3, |
| 101 | { |
| 102 | id: 3, |
| 103 | user: { login: "contributor" }, |
| 104 | body: `<!-- mobile-preview-ios ${JSON.stringify(ios)} -->`, |
| 105 | }, |
| 106 | ], |
| 107 | ]); |
| 108 | await main({ |
| 109 | env: { |
| 110 | GITHUB_REPOSITORY: "pingdotgg/t3code", |
| 111 | PR: "1", |
| 112 | GH_TOKEN: "test", |
| 113 | ANDROID_FINGERPRINT: fingerprint, |
| 114 | ANDROID_GROUP: "new-group", |
| 115 | }, |
| 116 | request: async (path, init = {}) => { |
| 117 | if (!init.method) return [...comments.values()]; |
| 118 | const id = Number(path.split("/").at(-1)); |
| 119 | if (init.method === "DELETE") comments.delete(id); |
| 120 | else if (init.method === "PATCH") comments.get(id).body = JSON.parse(init.body).body; |
| 121 | else assert.fail("an existing preview should be reused"); |
| 122 | }, |
| 123 | }); |
| 124 | assert.equal(comments.size, 2); |
| 125 | assert.deepEqual(readState(comments.get(1).body), { |
| 126 | android: { ...android, group: "new-group" }, |
| 127 | ios, |
| 128 | }); |
| 129 | assert.equal(comments.has(2), false); |
| 130 | assert.equal(comments.has(4), false); |
| 131 | assert.equal(comments.get(3).user.login, "contributor"); |
| 132 | await main({ |
| 133 | env: { |
| 134 | GITHUB_REPOSITORY: "pingdotgg/t3code", |
| 135 | PR: "1", |
| 136 | GH_TOKEN: "test", |
| 137 | ANDROID_FINGERPRINT: fingerprint, |
| 138 | PR_HEAD_SHA: "old-head", |
| 139 | }, |
| 140 | request: async (path) => { |
| 141 | assert.equal(path, "/repos/pingdotgg/t3code/pulls/1"); |
| 142 | return { head: { sha: "new-head" } }; |
| 143 | }, |
| 144 | }); |
| 145 | }); |