Files
go_bt/tests/gmail_protocol_settings_service.test.js

95 lines
3.0 KiB
JavaScript

const assert = require('assert');
const {
GMAIL_INBOX_URL,
GMAIL_PROTOCOL_SETTINGS_URL,
enableGmailPopAndImap,
} = require('../src/services/gmail_protocol_settings_service');
class FakeLocator {
constructor(page, kind, exists = true) {
this.page = page;
this.kind = kind;
this.exists = exists;
}
first() { return this; }
async count() { return this.exists ? 1 : 0; }
async innerText() { return this.page.bodyText; }
async evaluate() {
if (this.kind === 'pop') return this.page.popChecked;
if (this.kind === 'imap') return this.page.imapChecked;
return false;
}
async check() {
if (!this.exists) throw new Error('locator does not exist');
if (this.kind === 'pop') this.page.popChecked = true;
if (this.kind === 'imap') this.page.imapChecked = true;
this.page.saveEnabled = true;
}
async isEnabled() { return this.page.saveEnabled; }
async click() {
this.page.saveClicked = true;
this.page.saveEnabled = false;
this.page.currentUrl = GMAIL_INBOX_URL;
}
}
class FakePage {
constructor({ imapToggle = false } = {}) {
this.currentUrl = 'about:blank';
this.visited = [];
this.popChecked = false;
this.imapToggle = imapToggle;
this.imapChecked = false;
this.saveEnabled = false;
this.saveClicked = false;
this.bodyText = 'Forwarding and POP/IMAP IMAP access Folder size limits';
}
url() { return this.currentUrl; }
async goto(url) {
this.currentUrl = url;
this.visited.push(url);
}
async waitForLoadState() {}
async waitForTimeout() {}
locator(selector) {
if (selector === 'body') return new FakeLocator(this, 'body');
if (selector.includes('name="bx_pe"')) return new FakeLocator(this, 'pop');
if (selector.includes('name="bx_ie"')) return new FakeLocator(this, 'imap', this.imapToggle);
if (selector.includes('save_changes_button')) return new FakeLocator(this, 'save');
return new FakeLocator(this, 'missing', false);
}
}
(async () => {
{
const page = new FakePage();
const result = await enableGmailPopAndImap(page);
assert.strictEqual(result.ok, true);
assert.strictEqual(result.popEnabled, true);
assert.strictEqual(result.imapEnabled, true);
assert.strictEqual(result.imapAlwaysOn, true, '新版 Gmail 没有 IMAP 开关时应按始终开启处理');
assert.strictEqual(page.popChecked, true);
assert.strictEqual(page.saveClicked, true);
assert.deepStrictEqual(page.visited, [GMAIL_INBOX_URL, GMAIL_PROTOCOL_SETTINGS_URL]);
}
{
const page = new FakePage({ imapToggle: true });
const result = await enableGmailPopAndImap(page);
assert.strictEqual(result.ok, true);
assert.strictEqual(result.imapAlwaysOn, false);
assert.strictEqual(page.popChecked, true);
assert.strictEqual(page.imapChecked, true, '旧版 Gmail 有 IMAP 开关时应显式启用');
assert.strictEqual(page.saveClicked, true);
}
console.log('gmail_protocol_settings_service.test.js: PASS');
})().catch(error => {
console.error(error);
process.exitCode = 1;
});