app_auto_all/douyin_auto/tests/earning-navigation.test.js

86 lines
4.0 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

const test = require('node:test');
const assert = require('node:assert/strict');
const fs = require('node:fs');
const path = require('node:path');
const vm = require('node:vm');
const source = fs.readFileSync(path.join(__dirname, '../douyin-lite.js'), 'utf8').split('// MAIN:')[0];
const fixture = name => JSON.parse(fs.readFileSync(path.join(__dirname, 'fixtures', name), 'utf8'));
function runtime() {
const ctx = vm.createContext({device: {width: 1080, height: 2400},
files: {write() {}, append() {}}, sleep() {}, log() {}});
vm.runInContext(source, ctx);
ctx.keepScreenAwake = () => {};
ctx.foregroundPackage = () => ctx.CONFIG.packageName;
return ctx;
}
function nativePage(ctx) {
const nodes = fixture('vivo-home-native.json').map(item => ({
bounds: () => item.box, visibleToUser: () => true, enabled: () => true,
id: () => item.id, text: () => item.text, desc: () => item.desc
}));
ctx.packageName = () => ({find: () => ({size: () => nodes.length, get: i => nodes[i]})});
return ctx.readSnapshot();
}
test('vivo 图片赚钱入口经过原生快照读取后可定位', () => {
const ctx = runtime();
const page = nativePage(ctx);
assert.equal(ctx.earningNavigation(page).id, ctx.CONFIG.packageName + ':id/ki3');
assert.equal(ctx.isHome(page), true);
});
test('真实 OCR 的左括号不再阻断赚钱入口', () => {
const ctx = runtime();
const page = fixture('vivo-home-ocr.json');
assert.equal(ctx.earningNavigation(page).text, '(赚钱');
assert.equal(ctx.isHome(page), true);
});
test('vivo 控件实际延伸到屏幕底边时仍可定位', () => {
const ctx = runtime();
const page = nativePage(ctx);
const button = ctx.earningNavigation(page);
button.rect.bottom = 2400;
assert.equal(ctx.earningNavigation(page), button);
});
test('真实浮层任务页的合框收益和广告卡片优先于底下首页', () => {
const ctx = runtime();
const page = fixture('vivo-task-overlay.json');
assert.equal(ctx.isTaskCenter(page), true);
assert.equal(ctx.isHome(page), false);
assert.equal(ctx.rowsOnPage(page, ctx.STAGES[2].title)[0].action.text, '去观看');
ctx.snapshot = () => page;
ctx.tapFresh = () => assert.fail('不应点击浮层背后的首页');
assert.equal(ctx.enterHomeThenEarning(), page);
});
test('图片入口必须同时具有底部导航证据,且位于底部中央', () => {
const ctx = runtime();
const page = nativePage(ctx);
const button = ctx.earningNavigation(page);
assert.equal(ctx.earningNavigation([button]), null);
button.rect.top = 600;
button.rect.bottom = 730;
assert.equal(ctx.earningNavigation(page), null);
});
test('正文中的赚钱文字和相似推广文案不能当作导航', () => {
const ctx = runtime();
assert.equal(ctx.earningNavigation([{text: '赚钱', rect: {left: 450, top: 500, right: 600, bottom: 560}}]), null);
assert.equal(ctx.earningNavigation([{text: '赚钱领福利', rect: {left: 450, top: 2264, right: 620, bottom: 2329}}]), null);
});
test('从真实无文字首页依次点击首页、赚钱并核实任务页', () => {
const ctx = runtime();
const page = nativePage(ctx), clicks = [];
ctx.snapshot = () => page;
ctx.tapFresh = (item, resolve) => clicks.push((resolve ? resolve(page) : item).text);
ctx.ensureTaskCenter = () => 'task-center-confirmed';
assert.equal(ctx.enterHomeThenEarning(), 'task-center-confirmed');
assert.deepEqual(clicks, ['首页', '@douyin_earning_tab']);
});
test('已确认任务页时直接恢复,不返回首页重走入口', () => {
const ctx = runtime();
const page = [{text: '任务中心', rect: {left: 80, top: 200, right: 300, bottom: 260}},
{text: '每日签到', rect: {left: 60, top: 650, right: 300, bottom: 700}},
{text: '开宝箱', rect: {left: 60, top: 900, right: 300, bottom: 950}}];
ctx.snapshot = () => page;
ctx.tapFresh = () => assert.fail('不应离开已确认的任务页');
assert.equal(ctx.enterHomeThenEarning(), page);
});