forked from shirser121/whatsapp-web.js
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add tests and detect WhatsApp Web updates (pedroslopez#686)
* test setup, add initializer tests * test sending messages * add script to check latest version * add github action * use env vars * configure environment with .env file * add test for sticker name and author * add DownloadManager model * test chats and contacts * test for number utility functions * throw error if no remote id has been set * Update .version
- Loading branch information
1 parent
04d2308
commit a03cc41
Showing
10 changed files
with
574 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
WWEBJS_TEST_SESSION_PATH=test_session.json | ||
WWEBJS_TEST_REMOTE_ID=[email protected] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
name: Update | ||
|
||
on: | ||
schedule: | ||
- cron: "0/15 * * * *" | ||
workflow_dispatch: | ||
|
||
jobs: | ||
update: | ||
runs-on: ubuntu-latest | ||
|
||
steps: | ||
- uses: actions/checkout@v2 | ||
- name: Install node v14 | ||
uses: actions/setup-node@v2 | ||
with: | ||
node-version: '14' | ||
- name: Install dependencies | ||
run: npm install | ||
- run: cd ./tools/version-checker | ||
- name: Run Updater | ||
run: ./update-version | ||
- name: Store WA Version | ||
run: echo WA_VERSION=`cat ./.version` >> $GITHUB_ENV | ||
- name: Create Pull Request | ||
uses: peter-evans/create-pull-request@v3 | ||
with: | ||
commit-message: Update supported WhatsApp Web version to v${{ env.WA_VERSION }} | ||
title: Update WhatsApp Web Version (${{ env.WA_VERSION }}) | ||
body: | | ||
A new version of WhatsApp Web has been detected! | ||
Tests should be run against this new version before merging. | ||
labels: WhatsApp Change | ||
reviewers: pedroslopez |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
## Running tests | ||
|
||
These tests require an authenticated WhatsApp Web session, as well as an additional phone that you can send messages to. | ||
|
||
This can be configured using the following environment variables: | ||
- `WWEBJS_TEST_SESSION`: A JSON-formatted string with the session details. Must include `WABrowserId`, `WASecretBundle`, `WAToken1` and `WAToken2`. | ||
- `WWEBJS_TEST_SESSION_PATH`: Path to a JSON file that contains the session details. Must include `WABrowserId`, `WASecretBundle`, `WAToken1` and `WAToken2`. | ||
- `WWEBJS_TEST_REMOTEID`: A valid WhatsApp ID that you can send messages to, e.g. `[email protected]`. | ||
|
||
You *must* set `WWEBJS_TEST_REMOTEID` **and** either `WWEBJS_TEST_SESSION` or `WWEBJS_TEST_SESSION_PATH` for the tests to run properly. |
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
const path = require('path'); | ||
const Client = require('../src/Client'); | ||
const Util = require('../src/util/Util'); | ||
|
||
require('dotenv').config(); | ||
|
||
const remoteId = process.env.WWEBJS_TEST_REMOTE_ID; | ||
if(!remoteId) throw new Error('The WWEBJS_TEST_REMOTE_ID environment variable has not been set.'); | ||
|
||
function getSessionFromEnv() { | ||
const envSession = process.env.WWEBJS_TEST_SESSION; | ||
if(envSession) return JSON.parse(envSession); | ||
|
||
const envSessionPath = process.env.WWEBJS_TEST_SESSION_PATH; | ||
if(envSessionPath) { | ||
const absPath = path.resolve(process.cwd(), envSessionPath); | ||
return require(absPath); | ||
} | ||
|
||
throw new Error('No session found in environment.'); | ||
} | ||
|
||
function createClient({withSession, options: additionalOpts}={}) { | ||
const options = {}; | ||
if(withSession) { | ||
options.session = getSessionFromEnv(); | ||
} | ||
|
||
return new Client(Util.mergeDefault(options, additionalOpts || {})); | ||
} | ||
|
||
function sleep(ms) { | ||
return new Promise(resolve => setTimeout(resolve, ms)); | ||
} | ||
|
||
module.exports = { | ||
sleep, | ||
createClient, | ||
remoteId | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
2.2126.10 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
#!/usr/bin/env node | ||
|
||
const fs = require('fs'); | ||
const puppeteer = require('puppeteer'); | ||
const { DefaultOptions } = require('../../src/util/Constants'); | ||
|
||
const getLatestVersion = async () => { | ||
const browser = await puppeteer.launch(); | ||
const page = await browser.newPage(); | ||
await page.setUserAgent(DefaultOptions.userAgent); | ||
|
||
await page.goto('https://web.whatsapp.com/', { waitUntil: 'load'}); | ||
await page.waitForSelector('.landing-header'); | ||
|
||
const version = await page.evaluate(() => window.Debug.VERSION); | ||
await browser.close(); | ||
|
||
return version; | ||
}; | ||
|
||
const getCurrentVersion = () => { | ||
try { | ||
const versionFile = fs.readFileSync('./.version'); | ||
return versionFile ? versionFile.toString() : null; | ||
} catch(_) { | ||
return null; | ||
} | ||
}; | ||
|
||
const updateVersion = async (oldVersion, newVersion) => { | ||
const readmePath = '../../README.md'; | ||
|
||
const readme = fs.readFileSync(readmePath); | ||
const newReadme = readme.toString().replaceAll(oldVersion, newVersion); | ||
|
||
fs.writeFileSync(readmePath, newReadme); | ||
fs.writeFileSync('./.version', newVersion); | ||
|
||
}; | ||
|
||
(async () => { | ||
const currentVersion = getCurrentVersion(); | ||
const version = await getLatestVersion(); | ||
|
||
console.log(`Current version: ${currentVersion}`); | ||
console.log(`Latest version: ${version}`); | ||
|
||
if(currentVersion !== version) { | ||
console.log('Updating files...'); | ||
await updateVersion(currentVersion, version); | ||
console.log('Updated!'); | ||
} else { | ||
console.log('No changes.'); | ||
} | ||
})(); |