import { uid, timestamp, resolveSelection, questionsFor, eligibleDocuments, readiness } from './store.jsx';
import { defaultFields } from './seed.js';
export const generationStages = ['Preparing local sample fixtures', 'Applying the selected test template', 'Checking sample source traceability', 'Ready for review'];
export function makeDemoSuite(state, workspace, templateId, provisional) {
const selection = resolveSelection(state, workspace.baselineId, workspace.crIds);
if (selection.issues.length) throw new Error(selection.issues.join(' '));
if (!provisional && !readiness(state, workspace).ready) throw new Error('Final generation requires reviewed evidence and resolved clarifications.');
const template = state.templates.find(t => t.id === templateId && t.team === workspace.team && (t.visibility !== 'workspace' || t.workspaceId === workspace.id));
if (!template) throw new Error('Choose an accessible team template.');
const available = new Set(eligibleDocuments(state, workspace).map(d => d.id));
const map = new Map((selection.baseline?.rules || []).map(r => [r.id, r]));
selection.diff.forEach(change => { if (change.op === 'REMOVE') map.delete(change.ruleId); else map.set(change.ruleId, { id: change.ruleId, title: change.title, text: change.after, source: change.source, changed: true }); });
(workspace.customRules || []).forEach(r => map.set(r.id, r));
const tests = [...map.values()].filter(r => available.has(r.source)).map((r, i) => ({ id: `TC-${workspace.team.toUpperCase()}-${String(i + 1).padStart(3, '0')}`, title: `Verify ${r.title.toLowerCase()}`, category: r.changed ? 'Positive' : 'Regression', requirement: r.id, objective: `Check the selected revision of ${r.title}.`, preconditions: 'Use an approved symbolic fixture applicable to this rule.', data: 'APPROVED_RULE_FIXTURE', steps: ['Prepare the source-backed fixture for this rule.', `Exercise ${r.title.toLowerCase()} within the owning team’s scope.`, 'Record the observed result against the selected evidence.'], expected: r.text, priority: 'Medium', rationale: 'Directly exercises a selected source-backed rule.', sourceIds: [r.source], status: 'Generated', notes: '', revision: 1 }));
const gaps = questionsFor(state, workspace).filter(q => q.status !== 'resolved');
if (provisional) gaps.forEach((q, i) => tests.push({ id: `TC-${workspace.team.toUpperCase()}-GAP-${i + 1}`, title: q.title, category: 'Dependency', requirement: q.requirement, objective: q.why, preconditions: 'Approved evidence is missing.', data: 'UNRESOLVED_FIXTURE', steps: ['Resolve the cited clarification before completing this test.'], expected: 'Expected result unresolved', priority: 'Unassigned', rationale: 'Priority and outcome require reviewed evidence.', sourceIds: q.sourceId && available.has(q.sourceId) ? [q.sourceId] : [], status: 'Provisional', notes: q.requested, revision: 1 }));
const resolved = questionsFor(state, workspace).filter(q => q.status === 'resolved');
resolved.forEach((q, i) => { const source = q.answerSourceId || q.sourceId; tests.push({ id: `TC-${workspace.team.toUpperCase()}-CL-${i + 1}`, title: `Verify ${q.title.replace(/\?$/, '').toLowerCase()}`, category: 'Dependency', requirement: q.requirement, objective: 'Check behavior supplied by reviewed workspace evidence.', preconditions: 'Use the fixture described by the reviewed clarification.', data: 'REVIEWED_CLARIFICATION_FIXTURE', steps: ['Prepare the agreed fixture.', 'Exercise the approved behavior.', 'Compare the result to the reviewed answer.'], expected: q.answer, priority: 'Medium', rationale: 'Covers a previously missing dependency rule.', sourceIds: source && available.has(source) ? [source] : [], answerEvidence: q.sourceReference, status: 'Generated', notes: `User-provided evidence, saved by ${q.resolvedBy || q.author || workspace.team}.`, revision: 1 }); });
const customFields = template.fields.filter(field => !defaultFields.some(known => known.toLowerCase() === field.toLowerCase()));
tests.forEach(test => { test.customFields = Object.fromEntries(customFields.map(field => [field, ''])); });
return { id: uid('suite'), name: `${workspace.name} · ${provisional ? 'Provisional draft' : 'Test suite'}`, workspaceId: workspace.id, version: workspace.version, baselineId: workspace.baselineId, crIds: [...workspace.crIds], sharedContractIds: [...workspace.sharedContractIds || []], templateId, templateSnapshot: structuredClone(template), provisional, status: provisional ? 'PROVISIONAL' : 'Ready for review', createdAt: timestamp(), gaps: gaps.map(q => ({ requirement: q.requirement, title: q.title })), tests, demo: true };
}
For further actions, you may consider blocking this person and/or reporting abuse
Top comments (0)