Implement structure for code changes tracking
This commit is contained in:
@@ -0,0 +1,158 @@
|
||||
const test = require('node:test');
|
||||
const assert = require('node:assert/strict');
|
||||
const fs = require('node:fs');
|
||||
|
||||
const source = fs.readFileSync('content/signup-page.js', 'utf8');
|
||||
|
||||
function extractFunction(name) {
|
||||
const markers = [`async function ${name}(`, `function ${name}(`];
|
||||
const start = markers
|
||||
.map((marker) => source.indexOf(marker))
|
||||
.find((index) => index >= 0);
|
||||
if (start < 0) {
|
||||
throw new Error(`missing function ${name}`);
|
||||
}
|
||||
|
||||
let parenDepth = 0;
|
||||
let signatureEnded = false;
|
||||
let braceStart = -1;
|
||||
for (let i = start; i < source.length; i += 1) {
|
||||
const ch = source[i];
|
||||
if (ch === '(') {
|
||||
parenDepth += 1;
|
||||
} else if (ch === ')') {
|
||||
parenDepth -= 1;
|
||||
if (parenDepth === 0) {
|
||||
signatureEnded = true;
|
||||
}
|
||||
} else if (ch === '{' && signatureEnded) {
|
||||
braceStart = i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (braceStart < 0) {
|
||||
throw new Error(`missing body for function ${name}`);
|
||||
}
|
||||
|
||||
let depth = 0;
|
||||
let end = braceStart;
|
||||
for (; end < source.length; end += 1) {
|
||||
const ch = source[end];
|
||||
if (ch === '{') depth += 1;
|
||||
if (ch === '}') {
|
||||
depth -= 1;
|
||||
if (depth === 0) {
|
||||
end += 1;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return source.slice(start, end);
|
||||
}
|
||||
|
||||
test('step 3 reports completion before deferred submit click', async () => {
|
||||
const api = new Function(`
|
||||
const logs = [];
|
||||
const completions = [];
|
||||
const clicks = [];
|
||||
const scheduled = [];
|
||||
|
||||
const snapshot = {
|
||||
state: 'password_page',
|
||||
passwordInput: { value: '', hidden: false },
|
||||
submitButton: { textContent: 'Continue', hidden: false },
|
||||
displayedEmail: 'user@example.com',
|
||||
};
|
||||
|
||||
const window = {
|
||||
setTimeout(fn) {
|
||||
scheduled.push(fn);
|
||||
return scheduled.length;
|
||||
},
|
||||
};
|
||||
|
||||
const location = {
|
||||
href: 'https://auth.openai.com/create-account/password',
|
||||
};
|
||||
|
||||
function inspectSignupEntryState() {
|
||||
return snapshot;
|
||||
}
|
||||
|
||||
async function ensureSignupPasswordPageReady() {
|
||||
return { ready: true };
|
||||
}
|
||||
|
||||
function getSignupPasswordSubmitButton() {
|
||||
return snapshot.submitButton;
|
||||
}
|
||||
|
||||
async function waitForElementByText() {
|
||||
return null;
|
||||
}
|
||||
|
||||
function fillInput(input, value) {
|
||||
input.value = value;
|
||||
}
|
||||
|
||||
async function humanPause() {}
|
||||
async function sleep() {}
|
||||
|
||||
function log(message, level = 'info') {
|
||||
logs.push({ message, level });
|
||||
}
|
||||
|
||||
function reportComplete(step, payload) {
|
||||
completions.push({ step, payload });
|
||||
}
|
||||
|
||||
function simulateClick(target) {
|
||||
clicks.push(target.textContent || 'button');
|
||||
}
|
||||
|
||||
${extractFunction('step3_fillEmailPassword')}
|
||||
|
||||
return {
|
||||
async run(payload) {
|
||||
return step3_fillEmailPassword(payload);
|
||||
},
|
||||
async flushDeferredSubmit() {
|
||||
if (!scheduled.length) {
|
||||
throw new Error('missing deferred submit');
|
||||
}
|
||||
await scheduled[0]();
|
||||
},
|
||||
snapshot() {
|
||||
return {
|
||||
logs,
|
||||
completions,
|
||||
clicks,
|
||||
passwordValue: snapshot.passwordInput.value,
|
||||
scheduledCount: scheduled.length,
|
||||
};
|
||||
},
|
||||
};
|
||||
`)();
|
||||
|
||||
const result = await api.run({
|
||||
email: 'user@example.com',
|
||||
password: 'Secret123!',
|
||||
});
|
||||
|
||||
const beforeSubmit = api.snapshot();
|
||||
assert.equal(beforeSubmit.passwordValue, 'Secret123!');
|
||||
assert.equal(beforeSubmit.scheduledCount, 1);
|
||||
assert.deepStrictEqual(beforeSubmit.clicks, []);
|
||||
assert.equal(beforeSubmit.completions.length, 1);
|
||||
assert.equal(beforeSubmit.completions[0].step, 3);
|
||||
assert.deepStrictEqual(result, beforeSubmit.completions[0].payload);
|
||||
assert.equal(result.email, 'user@example.com');
|
||||
assert.equal(result.deferredSubmit, true);
|
||||
assert.equal(typeof result.signupVerificationRequestedAt, 'number');
|
||||
|
||||
await api.flushDeferredSubmit();
|
||||
|
||||
const afterSubmit = api.snapshot();
|
||||
assert.deepStrictEqual(afterSubmit.clicks, ['Continue']);
|
||||
});
|
||||
@@ -108,3 +108,127 @@ test('verification flow runs beforeSubmit hook before filling the code', async (
|
||||
['complete', '654321'],
|
||||
]);
|
||||
});
|
||||
|
||||
test('verification flow keeps Hotmail request timestamp filtering on the first poll', async () => {
|
||||
const pollPayloads = [];
|
||||
|
||||
const helpers = api.createVerificationFlowHelpers({
|
||||
addLog: async () => {},
|
||||
chrome: { tabs: { update: async () => {} } },
|
||||
CLOUDFLARE_TEMP_EMAIL_PROVIDER: 'cloudflare-temp-email',
|
||||
completeStepFromBackground: async () => {},
|
||||
confirmCustomVerificationStepBypassRequest: async () => ({ confirmed: true }),
|
||||
getHotmailVerificationPollConfig: () => ({}),
|
||||
getHotmailVerificationRequestTimestamp: () => 87654,
|
||||
getState: async () => ({}),
|
||||
getTabId: async () => 1,
|
||||
HOTMAIL_PROVIDER: 'hotmail-api',
|
||||
isStopError: () => false,
|
||||
LUCKMAIL_PROVIDER: 'luckmail-api',
|
||||
MAIL_2925_VERIFICATION_INTERVAL_MS: 15000,
|
||||
MAIL_2925_VERIFICATION_MAX_ATTEMPTS: 15,
|
||||
pollCloudflareTempEmailVerificationCode: async () => ({}),
|
||||
pollHotmailVerificationCode: async (_step, _state, payload) => {
|
||||
pollPayloads.push(payload);
|
||||
return { code: '654321', emailTimestamp: 123 };
|
||||
},
|
||||
pollLuckmailVerificationCode: async () => ({}),
|
||||
sendToContentScript: async (_source, message) => {
|
||||
if (message.type === 'FILL_CODE') {
|
||||
return {};
|
||||
}
|
||||
return {};
|
||||
},
|
||||
sendToMailContentScriptResilient: async () => ({}),
|
||||
setState: async () => {},
|
||||
setStepStatus: async () => {},
|
||||
sleepWithStop: async () => {},
|
||||
throwIfStopped: () => {},
|
||||
VERIFICATION_POLL_MAX_ROUNDS: 5,
|
||||
});
|
||||
|
||||
await helpers.resolveVerificationStep(
|
||||
7,
|
||||
{
|
||||
email: 'user@example.com',
|
||||
loginVerificationRequestedAt: 100000,
|
||||
lastLoginCode: null,
|
||||
},
|
||||
{ provider: 'hotmail-api', label: 'Hotmail' },
|
||||
{}
|
||||
);
|
||||
|
||||
assert.equal(pollPayloads.length, 1);
|
||||
assert.equal(pollPayloads[0].filterAfterTimestamp, 87654);
|
||||
});
|
||||
|
||||
test('verification flow refreshes Hotmail signup filter timestamp after step 4 resend', async () => {
|
||||
const pollPayloads = [];
|
||||
const realDateNow = Date.now;
|
||||
Date.now = () => 200000;
|
||||
|
||||
let submitCount = 0;
|
||||
|
||||
const helpers = api.createVerificationFlowHelpers({
|
||||
addLog: async () => {},
|
||||
chrome: { tabs: { update: async () => {} } },
|
||||
CLOUDFLARE_TEMP_EMAIL_PROVIDER: 'cloudflare-temp-email',
|
||||
completeStepFromBackground: async () => {},
|
||||
confirmCustomVerificationStepBypassRequest: async () => ({ confirmed: true }),
|
||||
getHotmailVerificationPollConfig: () => ({}),
|
||||
getHotmailVerificationRequestTimestamp: (_step, state) => Math.max(0, Number(state.signupVerificationRequestedAt || 0) - 15000),
|
||||
getState: async () => ({}),
|
||||
getTabId: async () => 1,
|
||||
HOTMAIL_PROVIDER: 'hotmail-api',
|
||||
isStopError: () => false,
|
||||
LUCKMAIL_PROVIDER: 'luckmail-api',
|
||||
MAIL_2925_VERIFICATION_INTERVAL_MS: 15000,
|
||||
MAIL_2925_VERIFICATION_MAX_ATTEMPTS: 15,
|
||||
pollCloudflareTempEmailVerificationCode: async () => ({}),
|
||||
pollHotmailVerificationCode: async (_step, _state, payload) => {
|
||||
pollPayloads.push(payload);
|
||||
return {
|
||||
code: pollPayloads.length === 1 ? '111111' : '222222',
|
||||
emailTimestamp: pollPayloads.length,
|
||||
};
|
||||
},
|
||||
pollLuckmailVerificationCode: async () => ({}),
|
||||
sendToContentScript: async (_source, message) => {
|
||||
if (message.type === 'FILL_CODE') {
|
||||
submitCount += 1;
|
||||
return submitCount === 1
|
||||
? { invalidCode: true, errorText: '旧验证码' }
|
||||
: {};
|
||||
}
|
||||
if (message.type === 'RESEND_VERIFICATION_CODE') {
|
||||
return {};
|
||||
}
|
||||
return {};
|
||||
},
|
||||
sendToMailContentScriptResilient: async () => ({}),
|
||||
setState: async () => {},
|
||||
setStepStatus: async () => {},
|
||||
sleepWithStop: async () => {},
|
||||
throwIfStopped: () => {},
|
||||
VERIFICATION_POLL_MAX_ROUNDS: 5,
|
||||
});
|
||||
|
||||
try {
|
||||
await helpers.resolveVerificationStep(
|
||||
4,
|
||||
{
|
||||
email: 'user@example.com',
|
||||
signupVerificationRequestedAt: 100000,
|
||||
lastSignupCode: null,
|
||||
},
|
||||
{ provider: 'hotmail-api', label: 'Hotmail' },
|
||||
{}
|
||||
);
|
||||
} finally {
|
||||
Date.now = realDateNow;
|
||||
}
|
||||
|
||||
assert.equal(pollPayloads.length, 2);
|
||||
assert.equal(pollPayloads[0].filterAfterTimestamp, 85000);
|
||||
assert.equal(pollPayloads[1].filterAfterTimestamp, 185000);
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user