openmaic-custom/tests/lib/agent/tools/edit-interactive-html.test.ts
wyuc dcf8a63333
fix(generation): make outline type changes take effect (interactive/PBL no longer downgraded to slide) (#772)
* feat(generation): changeOutlineType total constructor for valid type switches

Switching a scene outline's type in the editor only flipped `type` and left
the new type without its required config, so applyOutlineFallbacks silently
downgraded interactive/pbl back to slide. changeOutlineType returns a new
outline that strips foreign per-type config and seeds the target type's
required config from the shared fields, valid by construction.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>

* i18n(generation): keys for interactive widget kind and PBL project config

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>

* feat(editor): make outline type changes take effect with config forms

TypePill now builds a valid outline via changeOutlineType (replace, not
partial-merge) so a type switch drops stale config and seeds the new type's
required config — no longer silently downgraded to slide by applyOutlineFallbacks.
Adds InteractiveConfigDisclosure (widget kind + concept) and PblConfigDisclosure
(topic / description / target skills), mirroring QuizConfigDisclosure, so users
can configure interactive and PBL scenes that previously had no input UI.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>

* fix(editor): move type-specific config under the type pill

The quiz/interactive/PBL config disclosure rendered as a detached chip at the
bottom-left of the card, reading like a stray key point. Pin it to the
bottom-right of the key-points row instead, so it sits directly under the
TypePill it configures (type top-right -> its config bottom-right, same edge),
with key points flowing on the left.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>

* fix(editor): cascade type config as a connected segment left of the type pill

Replace the standalone config chip with a cascading control in the header: the
type-specific config (quiz count / interactive widget kind / PBL project) renders
as a themed segment joined to the left of the TypePill, the two clipped into one
rounded pill split by a hairline divider. Reads as a single 'config › type'
cascade and groups the config with the selector it refines.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>

* fix(editor): address cross-review — preserve widget config, drop stale fields

- changeOutlineType: preserve an existing procedural-skill widget config
  instead of replacing it with a seeded simulation (codex P2 — task-engine
  fields were silently dropped on Interactive re-select).
- InteractiveConfigDisclosure.setWidgetType: reset widgetOutline to the shared
  concept only, so switching widget kind drops the previous kind's fields
  (claude P2 — language/gameType/etc. leaked across kinds).
- i18n locale-coverage test now also checks ko-KR and pt-BR.
- Drop a redundant WidgetType cast; add tests for procedural-skill preservation
  and pbl targetSkills dedupe/cap.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>

* fix(editor): round 2 cross-review nits — procedural-skill label + skill a11y

- InteractiveConfigDisclosure: when an outline carries a preserved
  procedural-skill widget, surface it as the current (selected) kind with its
  own label instead of mislabeling it 'Simulation', and make a same-kind
  re-select a no-op so its task-engine fields are never silently clobbered.
- PBL target-skill remove button uses a dedicated removeSkill aria-label.
- Add widgetProceduralSkill / removeSkill i18n keys (8 locales) + test coverage.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>

* fix(generation): make re-selecting the current outline type a no-op

changeOutlineType rebuilt the outline on every type-menu click, so re-selecting
the already-current type dropped fields it doesn't re-seed — legacy
interactiveConfig, or a partial pblConfig whose projectTopic is empty — turning a
harmless click into data loss. Short-circuit same-type selections to return the
outline untouched (codex P2).

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>

* chore(format): prettier --write files left unformatted on main

These 14 agent/edit files (not part of this PR's feature) fail the repo-wide
`prettier . --check` in the required Lint job — they landed on main unformatted,
turning main's CI (and therefore this PR after a branch update) red. Formatting
them here unblocks the required check; pure formatting, no logic changes.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>

* fix(lint): suppress intentional set-state-in-effect in reasoning timer

The reasoning timer does a deliberate one-shot setState in its effect to show
the right elapsed value before the first 1s interval tick. main's CI fails the
required Lint job on the new react-hooks/set-state-in-effect rule here; suppress
it with a justification (no behavior change). Unblocks the required check.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-06-23 17:48:07 +08:00

114 lines
3.9 KiB
TypeScript

import { describe, expect, it } from 'vitest';
import { makeEditInteractiveHtmlTool } from '@/lib/agent/tools/edit-interactive-html';
import type { SceneContext } from '@/lib/agent/tools/regenerate-scene-actions';
import type { SceneContent } from '@/lib/types/stage';
import type { SceneOutline } from '@/lib/types/generation';
const BROKEN =
'<!DOCTYPE html><html><head></head><body><button id="go">Go</button>' +
'<script>document.getElementById("strt").addEventListener("click",fn);</script></body></html>';
function outline(id: string, type: SceneOutline['type']): SceneOutline {
return {
id,
type,
title: 'Widget',
description: 'd',
keyPoints: [],
order: 0,
} as unknown as SceneOutline;
}
function interactiveCtx(id: string, html?: string): SceneContext {
return {
outline: outline(id, 'interactive'),
allOutlines: [outline(id, 'interactive')],
content: {
type: 'interactive',
url: 'about:blank',
html,
widgetType: 'simulation',
} as unknown as SceneContent,
stageId: 'stage-1',
};
}
function slideCtx(id: string): SceneContext {
return {
outline: outline(id, 'slide'),
allOutlines: [outline(id, 'slide')],
content: { type: 'slide', canvas: { elements: [] } } as unknown as SceneContent,
stageId: 'stage-1',
};
}
const deps = (ctx: (id: string) => SceneContext | undefined) => ({
aiCall: async () => '',
getSceneContext: ctx,
});
describe('edit_interactive_html tool', () => {
it('applies str_replace edits and returns the new html', async () => {
const tool = makeEditInteractiveHtmlTool(
deps((id) => (id === 'w1' ? interactiveCtx('w1', BROKEN) : undefined)),
);
const res = await tool.execute('call-1', {
sceneId: 'w1',
edits: [{ oldText: 'getElementById("strt")', newText: 'getElementById("go")' }],
});
expect((res as { isError?: boolean }).isError).toBeFalsy();
expect(res.details.sceneId).toBe('w1');
expect(res.details.editCount).toBe(1);
expect(res.details.html).toContain('getElementById("go").addEventListener');
expect(res.details.html).not.toContain('strt');
});
it('errors (no apply) when an edit cannot be anchored', async () => {
const tool = makeEditInteractiveHtmlTool(
deps((id) => (id === 'w1' ? interactiveCtx('w1', BROKEN) : undefined)),
);
const res = await tool.execute('call-1', {
sceneId: 'w1',
edits: [{ oldText: 'this text is not in the page', newText: 'x' }],
});
expect((res as { isError?: boolean }).isError).toBe(true);
expect(res.details.html).toBeNull();
expect(res.details.editCount).toBe(0);
expect((res.content[0] as { text?: string }).text).toMatch(/could not find/i);
});
it('refuses a non-interactive scene', async () => {
const tool = makeEditInteractiveHtmlTool(
deps((id) => (id === 's1' ? slideCtx('s1') : undefined)),
);
const res = await tool.execute('call-1', {
sceneId: 's1',
edits: [{ oldText: 'a', newText: 'b' }],
});
expect((res as { isError?: boolean }).isError).toBe(true);
expect(res.details.html).toBeNull();
});
it('refuses an interactive scene with no embedded html', async () => {
const tool = makeEditInteractiveHtmlTool(
deps((id) => (id === 'w2' ? interactiveCtx('w2', undefined) : undefined)),
);
const res = await tool.execute('call-1', {
sceneId: 'w2',
edits: [{ oldText: 'a', newText: 'b' }],
});
expect((res as { isError?: boolean }).isError).toBe(true);
expect(res.details.html).toBeNull();
});
it('errors when the scene context is missing', async () => {
const tool = makeEditInteractiveHtmlTool(deps(() => undefined));
const res = await tool.execute('call-1', {
sceneId: 'nope',
edits: [{ oldText: 'a', newText: 'b' }],
});
expect((res as { isError?: boolean }).isError).toBe(true);
expect(res.details.html).toBeNull();
});
});