# Fix Test Blockers and Module Boundary Implementation Plan
> **For agentic workers:** REQUIRED SUB-SKILL: Use superpowers:subagent-driven-development (recommended) or superpowers:executing-plans to implement this plan task-by-task. Steps use checkbox (`- [ ]`) syntax for tracking.
**Goal:** Remove the stale marketing flag assertion and dynamic-form ESM/CommonJS cycle so the Windows-compatible npm test chain can progress through the real suites.
**Architecture:** Keep the active-only marketing map contract and test its public resolver. Align the ESM-dominant form-engine boundary by converting only the show-rule visibility modules and their tests to ESM; then repair any independently reproduced CommonJS mock re-entry at the narrowest dependency boundary.
- Work on the current branch because the user explicitly declined a separate branch/worktree.
- Do not add a root `"type": "module"` declaration.
- Do not change marketing flag production behavior.
- Do not alter form visibility or payload behavior.
- Treat unrelated date and `wx` failures as separate issues.
- This small repair does not require a migration manifest.
---
### Task 1: Correct the marketing flag behavior test
**Files:**
- Modify: `tests/marketingFlag.test.mjs`
- Verify: `utils/marketingFlag.js`
**Interfaces:**
- Consumes: `buildMarketingFlagMap(list)` and `resolveMarketingFlag(map, goods)`.
- Produces: a regression test for active-only indexing and inactive fallback to `0`.
-[]**Step 1: Preserve the observed failing evidence**
Run: `node --test tests/marketingFlag.test.mjs`
Expected: FAIL because `map['200']` is `undefined`, not `0`.
-[]**Step 2: Replace the private representation assertion**
Update the first test to assert `map['200'] === undefined` and `resolveMarketingFlag(map, { merchantGoodsId: 200 }) === 0`. Rename the test so it states that only active goods are indexed and inactive goods resolve to zero.
-[]**Step 3: Verify the corrected behavior test**
Run: `node --test tests/marketingFlag.test.mjs`
Expected: PASS with no production change.
### Task 2: Remove the core show-rule module cycle
- Consumes: `evaluateShowRule(keys, resultExpr, rowFields, resolver)` and `resolveShowRuleContextValue(key, pageContext)`.
- Produces: named ESM exports `isGroupChildVisibleInRow`, `filterVisibleRowFields`, `resolveShowRuleSceneCodes`, `resolveShowRuleBrandIds`, and `resolveShowRuleContextValue` with unchanged signatures.
Expected: FAIL with `ERR_REQUIRE_CYCLE_MODULE` through `group-visibility.js`.
-[]**Step 2: Convert the visibility boundary to ESM**
Use static imports in `group-visibility.js`, export both public functions, and convert the three context functions in `show-rule-context.js` to named ESM exports. Do not change their logic.
Replace `createRequire()` calls for `group-show-rule-eval.js` and `group-visibility.js` with ESM imports. Keep CommonJS fixture/config requires unchanged.
-[]**Step 4: Verify the focused cycle tests**
Run the Step 1 command again.
Expected: the show-rule `ERR_REQUIRE_CYCLE_MODULE` no longer appears; tests either pass or reveal an independent mock re-entry.
### Task 3: Repair a reproduced mock submit-adapter re-entry
**Files:**
- Modify only the affected files selected by the focused failure among:
Expected: if still affected, FAIL with `ERR_REQUIRE_CYCLE_MODULE` naming `submit-adapter.js`; otherwise skip the remaining steps in this task.
-[]**Step 2: Add or preserve a focused behavioral test**
Use the existing mock detail tests to assert non-empty normalized detail data and valid `jsonContent`. The regression fails before the boundary repair because the module cannot initialize.
-[]**Step 3: Apply the narrowest boundary repair**
Remove only the synchronous CommonJS-to-ESM re-entry proven by Step 1. Preserve `buildMockDetailJsonContent`, staff/store mock constants, and response shapes.
-[]**Step 4: Verify focused mock tests**
Run the Step 1 command again.
Expected: PASS without `ERR_REQUIRE_CYCLE_MODULE`.
### Task 4: Regression verification
**Files:**
- Review: all modified files with `git diff --check` and `git diff`.
**Interfaces:**
- Consumes: completed repairs from Tasks 1-3.
- Produces: fresh evidence for the original symptoms and the repository test chain.
The cross-platform npm test scripts now reach the real test suites on Windows. Two pre-existing failures block the root test chain:
1.`tests/marketingFlag.test.mjs` expects an inactive goods entry to be materialized as `0`, while `buildMarketingFlagMap()` intentionally stores active goods only and callers use `resolveMarketingFlag()` to turn missing entries into `0`.
2.`packageDynamicForm` mixes ESM imports with synchronous CommonJS `require()` calls across the same form-engine dependency graph. Node 20 rejects the ESM -> CommonJS -> ESM re-entry with `ERR_REQUIRE_CYCLE_MODULE`.
This is a small repair, not a cross-project migration or large refactor, so the repository migration manifest gate does not apply.
## Goals
- Make the marketing flag test assert the public inactive-resolution behavior instead of a private sparse-map representation.
- Remove the form-engine ESM/CommonJS cycle without changing form visibility, show-rule evaluation, save payload, or mock detail behavior.
- Preserve the project's existing CommonJS API/adapters and avoid a repository-wide module-type switch.
- Verify the original failures and expose any later independent test failures accurately.
## Non-goals
- Do not add `"type": "module"` to the root package because many existing `.js` API, config, fixture, and utility files are CommonJS.
- Do not rewrite all `packageDynamicForm` files to one module system.
- Do not change marketing flag production semantics.
- Do not repair unrelated date-dependent or mini-program-global (`wx`) failures unless they are proven to be part of these module cycles.
## Design
### Marketing flag contract
`buildMarketingFlagMap()` remains a sparse active-only index. The test will assert:
- active goods resolve to `MARKETING_FLAG_ACTIVE`;
- inactive goods are not materialized in the sparse map;
-`resolveMarketingFlag()` returns `0` for that inactive goods ID.
This protects consumer-visible behavior while allowing the internal map to remain compact.
### Form-engine module boundary
The form engine is already predominantly ESM. `group-visibility.js` and the recently added `show-rule-context.js` will follow their ESM callers:
-`group-visibility.js` uses static imports for `evaluateShowRule` and context resolution and exports its two public functions.
-`show-rule-context.js` uses named ESM exports.
- tests that synchronously require ESM form-engine modules use ESM imports instead.
This removes the core ESM -> CommonJS -> ESM cycle while keeping behavior and function signatures unchanged.
### CommonJS mock boundary
After the core cycle is removed, tests may still expose a second re-entry: an ESM test loads `submit-adapter.js` while a CommonJS mock synchronously requires the same adapter during concurrent suite initialization. The repair will be evidence-driven:
- rerun focused tests after the core conversion;
- if the re-entry remains, move the small mock-only transformations behind a CommonJS-safe helper or convert only the affected mock boundary to ESM and update its callers consistently;
- do not duplicate the full submit adapter or introduce a package-wide module declaration.
## Verification
-`node --test tests/marketingFlag.test.mjs`
- focused show-rule and form-engine tests
- focused mock/detail tests if the second cycle reproduces
-`npm run test:dynamicForm`
-`npm test`
Any later independent failure will be reported separately instead of being attributed to this repair.