|
| 1 | +/** |
| 2 | + * ObjectUI |
| 3 | + * Copyright (c) 2024-present ObjectStack Inc. |
| 4 | + * |
| 5 | + * This source code is licensed under the MIT license found in the |
| 6 | + * LICENSE file in the root directory of this source tree. |
| 7 | + * |
| 8 | + * objectstack#34627 — the result card's checklist gains cloud's |
| 9 | + * `ChecklistVerificationLine` (`{ kind: 'verification', status: 'failed', |
| 10 | + * label, count }`), appended when the build's verdict has errors, with |
| 11 | + * `status` and `passed/total` following the verdict. The console draws it as |
| 12 | + * a red row (the producer's label, then the issue count), and a failed |
| 13 | + * checklist is the one place the turn states the failure: the build's draft |
| 14 | + * card draws neither its issue-count chip nor the red summary beside it. |
| 15 | + * |
| 16 | + * A line of any other kind that carries a valid `status` and a string `line` |
| 17 | + * (cloud's `publish` line is the next one) is drawn by those two fields. |
| 18 | + * |
| 19 | + * The verification line's shape is the producer's `build-cards.schema.json` |
| 20 | + * as the card names it; the vendored copy in `fixtures/build-cards.schema.ts` |
| 21 | + * predates it, so these fixtures are not routed through the contract test. |
| 22 | + * |
| 23 | + * Each message goes through the real mapper (`uiMessageToChatMessage`) from a |
| 24 | + * raw tool part, so the lift is under test, not a hand-built invocation. |
| 25 | + */ |
| 26 | +import '@testing-library/jest-dom/vitest'; |
| 27 | +import { describe, expect, it, vi } from 'vitest'; |
| 28 | +import { render, screen, within } from '@testing-library/react'; |
| 29 | +import { I18nProvider } from '@object-ui/i18n'; |
| 30 | +import { ChatbotEnhanced } from '../ChatbotEnhanced'; |
| 31 | +import { detectBuildChecklist, uiMessageToChatMessage } from '../mapMessages'; |
| 32 | +import { applyEnvelope, checklistOf } from './fixtures/buildCards.fixtures'; |
| 33 | + |
| 34 | +const VERIFICATION = { kind: 'verification', status: 'failed', label: '校验未通过', count: 3 } as const; |
| 35 | + |
| 36 | +/** Three objects and the board view, all passed, then the verification line: 4/5, failed. */ |
| 37 | +function failedChecklist() { |
| 38 | + const base = checklistOf('zh'); |
| 39 | + return { ...base, status: 'failed', passed: 4, total: 5, lines: [...base.lines.slice(0, 4), VERIFICATION] }; |
| 40 | +} |
| 41 | + |
| 42 | +const ISSUES = [ |
| 43 | + { severity: 'error', code: 'seed_column_empty', message: 'Seed "loan_sample" sets "due_date" on none of its rows.' }, |
| 44 | + { severity: 'error', code: 'view_filter_field', message: 'View "loan.overdue_loans" filters on an unknown field.' }, |
| 45 | + { severity: 'error', code: 'seed_lookup', message: 'Seed "loan_sample" points at a missing reader.' }, |
| 46 | +]; |
| 47 | + |
| 48 | +/** `apply_blueprint`'s envelope with the verdict's errors, and the checklist given. */ |
| 49 | +const failedBuild = (checklist: unknown) => { |
| 50 | + const { checklist: _served, ...envelope } = applyEnvelope('zh'); |
| 51 | + return { |
| 52 | + ...envelope, |
| 53 | + verification: { errors: 3, warnings: 0 }, |
| 54 | + issues: ISSUES, |
| 55 | + ...(checklist === undefined ? {} : { checklist }), |
| 56 | + }; |
| 57 | +}; |
| 58 | + |
| 59 | +const turn = (output: unknown) => |
| 60 | + uiMessageToChatMessage({ |
| 61 | + id: 'built', |
| 62 | + role: 'assistant', |
| 63 | + parts: [{ type: 'tool-apply_blueprint', toolCallId: 'apply-1', state: 'output-available', input: {}, output }], |
| 64 | + }); |
| 65 | + |
| 66 | +const renderBuild = (output: unknown) => |
| 67 | + render( |
| 68 | + <I18nProvider config={{ defaultLanguage: 'zh', detectBrowserLanguage: false }}> |
| 69 | + <ChatbotEnhanced messages={[turn(output)]} onOpenBuiltApp={vi.fn()} onReviewDraft={vi.fn()} /> |
| 70 | + </I18nProvider>, |
| 71 | + ); |
| 72 | + |
| 73 | +describe('objectstack#34627 — detectBuildChecklist keeps the verification line and any new kind', () => { |
| 74 | + it('a verification line is kept with its label and count', () => { |
| 75 | + const checklist = detectBuildChecklist(failedBuild(failedChecklist()))!; |
| 76 | + expect([checklist.status, checklist.passed, checklist.total]).toEqual(['failed', 4, 5]); |
| 77 | + expect(checklist.lines).toHaveLength(5); |
| 78 | + expect(checklist.lines[4]).toEqual({ kind: 'verification', status: 'failed', label: '校验未通过', count: 3 }); |
| 79 | + }); |
| 80 | + |
| 81 | + it('a verification line without its label or count, or not failed, is dropped', () => { |
| 82 | + for (const bad of [ |
| 83 | + { ...VERIFICATION, label: '' }, |
| 84 | + { ...VERIFICATION, count: -1 }, |
| 85 | + { ...VERIFICATION, count: '3' }, |
| 86 | + { ...VERIFICATION, status: 'passed' }, |
| 87 | + { kind: 'verification', status: 'failed', line: '校验未通过 · 3' }, |
| 88 | + ]) { |
| 89 | + const checklist = detectBuildChecklist(failedBuild({ ...failedChecklist(), lines: [bad] }))!; |
| 90 | + expect(checklist.lines).toEqual([]); |
| 91 | + } |
| 92 | + }); |
| 93 | + |
| 94 | + it('a line of another kind is kept by its status and line; one without them is dropped', () => { |
| 95 | + const publish = { kind: 'publish', status: 'failed', outcome: 'rolled_back', reason: 'x', line: '发布已回滚' }; |
| 96 | + const checklist = detectBuildChecklist( |
| 97 | + failedBuild({ ...failedChecklist(), lines: [publish, { kind: 'publish', status: 'failed' }, { kind: 'publish', status: 'maybe', line: 'x' }] }), |
| 98 | + )!; |
| 99 | + expect(checklist.lines).toEqual([{ kind: 'other', servedKind: 'publish', status: 'failed', line: '发布已回滚' }]); |
| 100 | + }); |
| 101 | + |
| 102 | + it('CONTROL: a checklist without the new line maps as before', () => { |
| 103 | + expect(detectBuildChecklist(applyEnvelope('zh'))!.lines).toEqual( |
| 104 | + checklistOf('zh').lines.map((l) => ({ kind: l.kind, status: l.status, line: l.line })), |
| 105 | + ); |
| 106 | + }); |
| 107 | +}); |
| 108 | + |
| 109 | +describe('objectstack#34627 — a failed build: 4/5 over five rows, the failure stated once', () => { |
| 110 | + it('the chip reads 4/5, five rows, one red row with the label and the issue count', () => { |
| 111 | + renderBuild(failedBuild(failedChecklist())); |
| 112 | + const chip = screen.getByTestId('build-checklist-chip'); |
| 113 | + expect(chip).toHaveTextContent(/^核对通过 4\/5$/); |
| 114 | + expect(chip).toHaveAttribute('data-status', 'failed'); |
| 115 | + const rows = screen.getAllByTestId('build-checklist-line'); |
| 116 | + expect(rows).toHaveLength(5); |
| 117 | + const failed = rows.filter((r) => r.getAttribute('data-status') === 'failed'); |
| 118 | + expect(failed).toHaveLength(1); |
| 119 | + expect(failed[0]).toHaveAttribute('data-kind', 'verification'); |
| 120 | + expect(failed[0]).toHaveTextContent(/^校验未通过 · 3 处待修复$/); |
| 121 | + expect(failed[0].className).toContain('text-red-600'); |
| 122 | + expect(within(failed[0]).getByTestId('build-checklist-verification-count')).toHaveTextContent('3 处待修复'); |
| 123 | + }); |
| 124 | + |
| 125 | + it('the draft card draws no issue-count chip and no red summary; the findings stay behind Technical details', () => { |
| 126 | + renderBuild(failedBuild(failedChecklist())); |
| 127 | + expect(screen.queryByTestId('draft-verification-chip')).not.toBeInTheDocument(); |
| 128 | + expect(screen.queryByTestId('draft-issues-summary')).not.toBeInTheDocument(); |
| 129 | + expect(screen.getByTestId('draft-issues-details')).toBeInTheDocument(); |
| 130 | + }); |
| 131 | + |
| 132 | + it('a new kind renders by its status and line beside the others', () => { |
| 133 | + const publish = { kind: 'publish', status: 'failed', outcome: 'rolled_back', reason: 'x', line: '发布已回滚,应用保持原样' }; |
| 134 | + const base = checklistOf('zh'); |
| 135 | + renderBuild(failedBuild({ ...base, status: 'failed', passed: 7, total: 8, lines: [...base.lines, publish] })); |
| 136 | + const rows = screen.getAllByTestId('build-checklist-line'); |
| 137 | + expect(rows).toHaveLength(8); |
| 138 | + expect(rows[7]).toHaveTextContent(/^发布已回滚,应用保持原样$/); |
| 139 | + expect(rows[7]).toHaveAttribute('data-status', 'failed'); |
| 140 | + expect(rows[7]).not.toHaveAttribute('data-kind'); |
| 141 | + }); |
| 142 | + |
| 143 | + it('CONTROL: a passed checklist without the new line draws as before, and the draft card keeps its chip and summary', () => { |
| 144 | + renderBuild(failedBuild(checklistOf('zh'))); |
| 145 | + expect(screen.getByTestId('build-checklist-chip')).toHaveTextContent(/^核对通过 7\/7$/); |
| 146 | + const rows = screen.getAllByTestId('build-checklist-line'); |
| 147 | + expect(rows).toHaveLength(7); |
| 148 | + for (const r of rows) expect(r).not.toHaveAttribute('data-kind'); |
| 149 | + expect(screen.getByTestId('draft-verification-chip')).toHaveTextContent('3 处待修复'); |
| 150 | + expect(screen.getByTestId('draft-issues-summary')).toBeInTheDocument(); |
| 151 | + }); |
| 152 | + |
| 153 | + it('CONTROL: no checklist (an older producer): the draft card keeps its chip and summary', () => { |
| 154 | + renderBuild(failedBuild(undefined)); |
| 155 | + expect(screen.queryByTestId('build-checklist')).not.toBeInTheDocument(); |
| 156 | + expect(screen.getByTestId('draft-verification-chip')).toHaveTextContent('3 处待修复'); |
| 157 | + expect(screen.getByTestId('draft-issues-summary')).toBeInTheDocument(); |
| 158 | + }); |
| 159 | +}); |
0 commit comments