Files
FlowPilot/tests/source-registry-module.test.js
T
2026-05-23 04:29:37 +08:00

201 lines
8.1 KiB
JavaScript

const test = require('node:test');
const assert = require('node:assert/strict');
const fs = require('node:fs');
const { readSourceRegistryBundle } = require('./helpers/script-bundles.js');
function loadSourceRegistry() {
const globalScope = {};
new Function('self', `${readSourceRegistryBundle()}; return self;`)(globalScope);
return globalScope.MultiPageSourceRegistry.createSourceRegistry();
}
test('background imports shared source registry module', () => {
const source = fs.readFileSync('background.js', 'utf8');
assert.match(source, /core\/flow-kernel\/flow-registry\.js/);
assert.match(source, /core\/flow-kernel\/settings-schema\.js/);
assert.match(source, /core\/flow-kernel\/source-registry\.js/);
assert.match(source, /shared\/kiro-timeouts\.js/);
assert.match(source, /flows\/grok\/index\.js/);
assert.match(source, /flows\/grok\/workflow\.js/);
assert.match(source, /flows\/grok\/background\/state\.js/);
assert.match(source, /flows\/grok\/background\/register-runner\.js/);
assert.match(source, /flows\/grok\/mail-rules\.js/);
});
test('manifest loads shared source registry before content utils in static bundles', () => {
const manifest = JSON.parse(fs.readFileSync('manifest.json', 'utf8'));
for (const entry of manifest.content_scripts || []) {
const scripts = Array.isArray(entry.js) ? entry.js : [];
if (!scripts.includes('content/utils.js')) continue;
assert.ok(scripts.includes('core/flow-kernel/source-registry.js'));
assert.ok(
scripts.indexOf('core/flow-kernel/source-registry.js') < scripts.indexOf('content/utils.js'),
'core/flow-kernel/source-registry.js must load before content/utils.js'
);
}
});
test('manifest no longer ships a static Kiro content bundle', () => {
const manifest = JSON.parse(fs.readFileSync('manifest.json', 'utf8'));
const hasStaticKiroBundle = (manifest.content_scripts || []).some((entry) => {
const scripts = Array.isArray(entry.js) ? entry.js : [];
return scripts.includes('flows/kiro/content/register-page.js')
|| scripts.includes('flows/kiro/content/desktop-authorize-page.js');
});
assert.equal(hasStaticKiroBundle, false);
});
test('manifest loads Grok flow definition in static bundles but not Grok content runtime', () => {
const manifest = JSON.parse(fs.readFileSync('manifest.json', 'utf8'));
for (const entry of manifest.content_scripts || []) {
const scripts = Array.isArray(entry.js) ? entry.js : [];
if (!scripts.includes('flows/index.js')) continue;
assert.ok(scripts.includes('flows/kiro/index.js'));
assert.ok(scripts.includes('flows/grok/index.js'));
assert.ok(
scripts.indexOf('flows/kiro/index.js') < scripts.indexOf('flows/grok/index.js'),
'Kiro definition should load before Grok definition'
);
assert.ok(
scripts.indexOf('flows/grok/index.js') < scripts.indexOf('flows/index.js'),
'Grok definition must load before flows/index.js'
);
assert.equal(scripts.includes('flows/grok/content/register-page.js'), false);
}
});
test('background injects shared Kiro timeout module before Kiro content scripts', () => {
const source = fs.readFileSync('background.js', 'utf8');
assert.match(
source,
/const KIRO_REGISTER_INJECT_FILES = \['flows\/openai\/index\.js', 'flows\/kiro\/index\.js', 'flows\/grok\/index\.js', 'flows\/index\.js', 'core\/flow-kernel\/flow-registry\.js', 'core\/flow-kernel\/source-registry\.js', 'shared\/kiro-timeouts\.js', 'content\/utils\.js', 'flows\/kiro\/content\/register-page\.js'\];/
);
assert.match(
source,
/const KIRO_DESKTOP_AUTHORIZE_INJECT_FILES = \['flows\/openai\/index\.js', 'flows\/kiro\/index\.js', 'flows\/grok\/index\.js', 'flows\/index\.js', 'core\/flow-kernel\/flow-registry\.js', 'core\/flow-kernel\/source-registry\.js', 'shared\/kiro-timeouts\.js', 'content\/utils\.js', 'flows\/kiro\/content\/desktop-authorize-page\.js'\];/
);
assert.match(
source,
/const GROK_REGISTER_INJECT_FILES = \['flows\/openai\/index\.js', 'flows\/kiro\/index\.js', 'flows\/grok\/index\.js', 'flows\/index\.js', 'core\/flow-kernel\/flow-registry\.js', 'core\/flow-kernel\/source-registry\.js', 'content\/utils\.js', 'flows\/grok\/content\/register-page\.js'\];/
);
});
test('shared source registry exposes canonical Kiro sources and drivers', () => {
const registry = loadSourceRegistry();
assert.equal(registry.resolveCanonicalSource('openai-auth'), 'openai-auth');
assert.deepEqual(registry.getSourceKeys('openai-auth'), ['openai-auth']);
assert.equal(registry.resolveCanonicalSource('signup-page'), 'signup-page');
assert.equal(registry.getSourceLabel('openai-auth'), '认证页');
assert.equal(
registry.detectSourceFromLocation({
url: 'https://auth.openai.com/create-account',
hostname: 'auth.openai.com',
}),
'openai-auth'
);
assert.equal(
registry.detectSourceFromLocation({
url: 'https://app.kiro.dev/signin',
hostname: 'app.kiro.dev',
}),
'kiro-register-page'
);
assert.equal(
registry.detectSourceFromLocation({
injectedSource: 'kiro-desktop-authorize',
url: 'https://signin.aws/register',
hostname: 'signin.aws',
}),
'kiro-desktop-authorize'
);
assert.equal(
registry.detectSourceFromLocation({
url: 'https://accounts.x.ai/sign-up',
hostname: 'accounts.x.ai',
}),
'grok-register-page'
);
assert.equal(
registry.detectSourceFromLocation({
url: 'https://grok.com/',
hostname: 'grok.com',
}),
'grok-register-page'
);
assert.equal(
registry.detectSourceFromLocation({
url: 'https://example.com/',
hostname: 'example.com',
}),
'unknown-source'
);
assert.equal(
registry.matchesSourceUrlFamily(
'kiro-register-page',
'https://app.kiro.dev/signin',
'https://app.kiro.dev/signin'
),
true
);
assert.equal(
registry.matchesSourceUrlFamily(
'kiro-register-page',
'https://signin.aws/register',
'https://app.kiro.dev/signin'
),
true
);
assert.equal(
registry.matchesSourceUrlFamily(
'kiro-desktop-authorize',
'https://profile.aws/complete',
'https://signin.aws/register'
),
true
);
assert.equal(
registry.matchesSourceUrlFamily(
'grok-register-page',
'https://accounts.x.ai/sign-up',
'https://grok.com/'
),
true
);
assert.equal(
registry.matchesSourceUrlFamily(
'grok-register-page',
'https://grok.com/',
'https://accounts.x.ai/sign-up'
),
true
);
assert.equal(
registry.matchesSourceUrlFamily(
'kiro-desktop-authorize',
'https://oidc.us-east-1.amazonaws.com/authorize',
'https://view.awsapps.com/start'
),
true
);
assert.equal(registry.shouldReportReadyForFrame('mail-163', true), false);
assert.equal(registry.shouldReportReadyForFrame('kiro-register-page', true), false);
assert.equal(registry.shouldReportReadyForFrame('kiro-desktop-authorize', true), false);
assert.equal(registry.getCleanupOwnerSource('oauth-localhost-callback'), 'openai-auth');
assert.equal(registry.driverAcceptsCommand('openai-auth', 'submit-signup-email'), true);
assert.equal(registry.driverAcceptsCommand('content/platform-panel', 'platform-verify'), true);
assert.equal(registry.driverAcceptsCommand('openai-auth', 'platform-verify'), false);
assert.equal(registry.driverAcceptsCommand('flows/kiro/content/register-page', 'kiro-submit-password'), true);
assert.equal(registry.driverAcceptsCommand('flows/kiro/content/desktop-authorize-page', 'kiro-complete-desktop-authorize'), true);
assert.equal(registry.driverAcceptsCommand('flows/kiro/background/register-runner', 'kiro-open-register-page'), true);
assert.equal(registry.driverAcceptsCommand('flows/kiro/background/desktop-authorize-runner', 'kiro-start-desktop-authorize'), true);
assert.equal(registry.driverAcceptsCommand('flows/kiro/background/publisher-kiro-rs', 'kiro-upload-credential'), true);
assert.equal(registry.driverAcceptsCommand('flows/grok/content/register-page', 'grok-submit-profile'), true);
assert.equal(registry.driverAcceptsCommand('flows/grok/background/register-runner', 'grok-extract-sso-cookie'), true);
});