Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
fix(build): detect a backport cut off after one file
Yudhi Armyndharis8fa2f1fec7ee
diff --git a/scripts/patch-wwebjs-201832.js b/scripts/patch-wwebjs-201832.jsindex e083a12e1..c7499768d 100644--- a/scripts/patch-wwebjs-201832.js+++ b/scripts/patch-wwebjs-201832.js@@ -42,13 +42,15 @@ const EXPECTED_REJECTS = new Set(['src/structures/Contact.js.rej']);// unnoticed. `patch` always writes a .rej for a hunk it couldn't place, but a hunk placed at the// WRONG offset would be silent — these assertions are what close that gap.//-// This list must cover every file the patch normalizes, not just the structure constructors, because+// This list must cover every file the patch writes, not just the structure constructors, because// the skip branch below uses it to decide a half-patched tree apart from an upstream fix. `patch`// applies the diff in file order and Utils.js is applied LAST, one file after Message.js — which is// what the skip branch keys on. Omitting a file here means a run that died in that window leaves a// tree where every assertion passes, so every later run stands down and the missing site latches in-// permanently. One entry per file: `siteLanded` resolves a file's marker with `.find`, so a second-// entry for the same path would never be read.+// permanently. The same holds at the other end: index.d.ts is the FIRST file written, and a run that+// died right after it reads as a pristine tree, so the diff is applied again and rejects its hunk.+// One entry per file: `siteLanded` resolves a file's marker with `.find`, so a second entry for the+// same path would never be read.const REQUIRED_SITES = [['src/structures/Base.js', /static _normalizeId\(id\)/],['src/structures/Message.js', /this\.id = Base\._normalizeId\(data\.id\)/],@@ -63,6 +65,8 @@ const REQUIRED_SITES = [['src/util/Injected/Utils.js', /_serialized: msg\.id\.\$1/],['src/Client.js', /res\.gid\._serialized \|\| res\.gid\.\$1/],['src/structures/GroupChat.js', /pWid\._serialized \|\| pWid\.\$1/],+ // The typings hunk, the one file the patch writes before any of the above.+ ['index.d.ts', /sendReaction\(messageId: string, reaction: string\): Promise<void>;/],];/** Artifacts `patch` can leave behind. `.~N~` are GNU's backup-if-mismatch copies of pre-patch source. */diff --git a/src/engine/adapters/wwebjs-backport-201832.spec.ts b/src/engine/adapters/wwebjs-backport-201832.spec.tsindex 9b598ccd6..d9c1ae069 100644--- a/src/engine/adapters/wwebjs-backport-201832.spec.ts+++ b/src/engine/adapters/wwebjs-backport-201832.spec.ts@@ -162,6 +162,26 @@ describe('patch-wwebjs-201832 (build-time backport of upstream #201832)', () =>expect(litter).toEqual([]);});+ // index.d.ts is the FIRST file `patch` writes. No structure marker covers it, so a run that died right+ // after it read as a pristine tree: the diff was applied again, its typings hunk rejected and the+ // interrupted install reported as version skew with a .rej left behind.+ it('refuses a tree whose run stopped right after the first file, without touching it', () => {+ const done = copyWwjs();+ applyBackport(done);+ const dir = copyWwjs();+ const typings = path.join(dir, 'index.d.ts');+ fs.copyFileSync(path.join(done, 'index.d.ts'), typings);+ const before = fs.readFileSync(typings, 'utf8');+ const baseJs = path.join(dir, 'src', 'structures', 'Base.js');+ const base = fs.readFileSync(baseJs, 'utf8');++ expect(() => applyBackport(dir)).toThrow(/PARTIALLY patched[\s\S]*Message\.js[\s\S]*reinstall/i);++ expect(fs.readFileSync(typings, 'utf8')).toBe(before);+ expect(fs.readFileSync(baseJs, 'utf8')).toBe(base);+ expect(fs.existsSync(`${typings}.rej`)).toBe(false);+ });+it('refuses to stand down on a half-patched tree', () => {const dir = copyWwjs();// Exactly what a run that died mid-apply leaves: `patch` writes as it goes and Message.js is thediff --git a/src/engine/adapters/wwebjs-backport-check.spec.ts b/src/engine/adapters/wwebjs-backport-check.spec.tsindex ffa10db9d..38e214a57 100644--- a/src/engine/adapters/wwebjs-backport-check.spec.ts+++ b/src/engine/adapters/wwebjs-backport-check.spec.ts@@ -108,6 +108,21 @@ describe('isBackportMissing', () => {expect(backportProblem(dir)).toContain('src/structures/Message.js, src/util/Injected/Utils.js');});+ // index.d.ts is the first file written, so a run killed right after it leaves every structure file+ // unpatched: the tree is not a pristine one, and the answer is the list, not "run the patcher".+ it('names a tree where only the first file the patcher writes landed', () => {+ const dir = wholeInstall();+ for (const [rel] of REQUIRED_SITES) {+ if (rel !== 'index.d.ts') fs.writeFileSync(path.join(dir, rel), 'this.id = data.id;');+ }++ expect(isBackportMissing(dir)).toBe(true);+ expect(partialBackportSites(dir)).toHaveLength(REQUIRED_SITES.length - 1);+ expect(partialBackportSites(dir)).not.toContain('index.d.ts');+ expect(backportProblem(dir)).toContain('src/structures/Message.js');+ expect(backportProblem(dir)).not.toBe(BACKPORT_MISSING_MESSAGE);+ });+it('leaves a tree fixed upstream in its own way alone', () => {const dir = install('this.id = data.id._serialized || data.id.$1;');