qwen-code/scripts/tests/no-config-object-create.test.js
易良 6652fdc9f6
refactor(core): centralize worktree config derivation (#9915)
* refactor(core): centralize worktree config derivation

* refactor(core): centralize agent config derivation (#9918)

* refactor(core): centralize agent config derivation

* refactor(core): centralize scoped config profiles (#9920)

* fix(core): keep approval-mode transitions working on derived teammate and dispatch configs

Give every per-agent config child-local approval state, not just the ones
spawned with an explicit mode: tools bind to the config and teammate mode
switches (Shift+Tab) plus 'Proceed always' confirmations call setApprovalMode
on it, which the derived-Config guard rejects without an approval profile.
Snapshot the base mode when none is requested so no AUTO strip is acquired
and cleanup stays a no-op.

Layer an approval profile over the workflow-orchestrator's derived dispatch
contexts (worktree isolation / workingDir) as agent.ts does, with cleanup in
the outer finally.

* fix(core): anchor plans-directory assertions at the plans-owning base Config

Derived agent profiles rebind targetDir to their own workspace, but the plan
file stays in the owning base's configured plans directory. The containment
assertions in savePlan/loadPlan compared against the derived workspace and
threw FatalConfigError for teammates whose cwd differs from the parent project
root, which savePlanBestEffort swallowed into a debug warning — silently
dropping the plan. Resolve the anchor to the Config that owns the
plans-directory state instead.

* test(core): cover deriveAgentConfig workspace rebinds

Mirror the deriveWorktreeConfig pairing test: assert the public getter
overrides (getCwd/getProjectRoot/getWorkingDir/getTargetDir, plan-file path)
and the paired private targetDir/cwd field writes plus own
workspaceContext/fileDiscoveryService, so a refactor that drops any rebind
fails loudly instead of leaking parent-tree reads into spawned agents.

* test(scripts): add flat-config integration test for no-config-object-create

Mirror no-core-root-barrel-config.test.js: load the real eslint.config.js via
new ESLint({overrideConfigFile}) and lintText virtual packages/core/src/**
paths, so a future edit to the flat-config wiring (plugin key, rule name,
ignores) that silently stops the gate applying to packages/core/src/** turns
this test red instead of degrading the invariant with no signal.

---------

Co-authored-by: Shaojin Wen <shaojin.wensj@alibaba-inc.com>
2026-08-27 12:28:08 +00:00

43 lines
1.3 KiB
JavaScript

import { Linter } from 'eslint';
import { describe, expect, it } from 'vitest';
import rule from '../../eslint-rules/no-config-object-create.js';
function verify(code) {
const linter = new Linter({ configType: 'eslintrc' });
linter.defineRule('qwen-code/no-config-object-create', rule);
return linter.verify(code, {
parserOptions: { ecmaVersion: 'latest', sourceType: 'module' },
rules: { 'qwen-code/no-config-object-create': 'error' },
});
}
describe('no-config-object-create', () => {
it('rejects prototype derivation in a module that imports Config', () => {
const messages = verify(`
import { Config } from '../config/config.js';
const child = Object.create(parent);
`);
expect(messages).toHaveLength(1);
expect(messages[0]?.messageId).toBe('useDeriveConfig');
});
it('allows deriveConfig and null-prototype dictionaries', () => {
expect(
verify(`
import { Config, deriveConfig } from '../config/config.js';
const child = deriveConfig(parent);
const dictionary = Object.create(null);
`),
).toEqual([]);
});
it('ignores unrelated Config imports', () => {
expect(
verify(`
import { Config } from '../telemetry/config.js';
const error = Object.create(Object.getPrototypeOf(source));
`),
).toEqual([]);
});
});