Kế hoạch triển khai
Tải bản gốc ↓Kế hoạch triển khai Import Contacts của CRM-142
Dành cho tác tử: chỉ lập kế hoạch trong lần này. Lần thực thi sau vẫn theo cơ chế gốc, một tác tử; không kỹ năng, commit, worktree hay ủy quyền bổ sung. Chỉ dẫn người dùng/dự án ưu tiên hơn mẫu subskill thực thi gốc.
Mục tiêu: Nhập liên hệ CSV đã xét vào danh sách cục bộ mà không mất liên hệ hoặc lưu khi xem trước.
Kiến trúc: Phân tích/phân loại/hợp nhất thuần trong mô-đun nhập mới; bảng tích hợp và ghi tuần tự trong ứng dụng hiện có. Bộ điều hợp liên hệ/lưu trữ giữ nguyên.
Công nghệ: HTML/CSS/ES module trình duyệt thuần, localStorage; bộ kiểm thử tích hợp Node >=24, không phụ thuộc.
Đặc tả: docs/brainstorming.md, docs/screen-design.md, docs/architecture.md; yêu cầu cố định trong tickets/CRM-142.md và docs/import-contract.md.
Ràng buộc chung
Chỉ thay ứng dụng tại: index.html, style.css, src/app.js, tệp mới src/import.js. Kiểm thử, mẫu, ticket, máy chủ, gói, AGENTS.md và hai mô-đun hỗ trợ là bất biến. Không dịch vụ ngoài, đổi phụ thuộc, chạy companion, cài toàn cục, commit, triển khai hay xuất bản. Không đăng nhập, cơ sở dữ liệu máy chủ, trình ánh xạ, tải lên từ xa, tài khoản CRM thật hoặc tác vụ nền. Giới hạn 100,000 byte UTF-8 và 500 bản ghi không trống; hiển thị trước tải lên. Xem trước không lưu bền. Search/Add vẫn hoạt động. Giai đoạn này không có quyền triển khai.
Trọng tâm xem xét
Kiểm thử mô-đun bất biến bao phủ nhiều ca phân tích nhưng không chứng minh UI/lưu trữ. Thêm kiểm tra thủ công mục tiêu này vào nhiệm vụ tương ứng:
- Nhiệm vụ 3: Add/Import nhanh và xác nhận hai lần không đua ghi hoặc làm mất liên hệ.
- Nhiệm vụ 3: lưu lỗi giữ xem trước và dữ liệu đã lưu cũ; thử lại thêm mỗi liên hệ một lần.
- Nhiệm vụ 2: đọc tệp muộn sau chọn/hủy không được khôi phục xem trước cũ.
- Nhiệm vụ 4: ô giống HTML vẫn là văn bản trong xem trước và danh sách; không phần tử được chèn.
- Nhiệm vụ 4: tiêu điểm/trạng thái bàn phím và giá trị dài vẫn dùng được ở 320px.
Nhiệm vụ 1: xem trước và hợp nhất CSV thuần
Tệp: Tạo src/import.js; dùng phần không đổi src/contacts.js; xác minh phần không đổi tests/import.test.js và tests/contacts.test.js. Giao diện: Tạo previewCSV(text: string, existing: Contact[]): Preview và mergeReady(preview: Preview, existing: Contact[]): {contacts: Contact[], added: number, skipped: number}. Contact là dạng hiện có {id,name,email,company} ; Preview là {rows: {record:number,name:string,email:string,company:string,status:'ready'|'invalid'|'duplicate',reason:string}[], counts:{ready:number,invalid:number,duplicate:number}}. Dùng normalEmail, validContact và addContact từ src/contacts.js.
- [ ] Chạy
node --test tests/import.test.jstrước triển khai; với cây hiện tại dự kiến thiếusrc/import.js, không phải lỗi hành vi sản phẩm. Không sửa kiểm thử. - [ ] Triển khai previewCSV với bộ quét/giới hạn/quyết định tiêu đề trong
docs/architecture.md; hoàn thành xác minh cấu trúc trước khi trả xem trước. Phân loại trường trước theo dõi trùng, giữ mọi bản ghi không trống và số đếm chính xác. - [ ] Triển khai mergeReady, xét lại Ready với liên hệ hiện tại được truyền. Giữ đầu vào và liên hệ cũ, thêm ID bằng addContact, tính mọi bản ghi tệp không được thêm là bỏ qua.
- [ ] Chạy
node --test tests/import.test.js tests/contacts.test.js; dự kiến cả 17 kiểm thử đạt (12 nhập, 5 liên hệ). Chứng minh ngoặc kép, BOM/CRLF, company tùy chọn, bản ghi trống, cấu trúc lỗi, trùng với hàng lỗi đầu, xác minh lại khi hợp nhất, bất biến và giới hạn. - [ ] Chạy kiểm tra assertion mô-đun Node inline, không lưu chỉnh sửa kiểm thử: đúng 100,000 byte ASCII được nhận, 100,001 bị từ chối; tiêu đề cắt khoảng trắng và hoa/thường hỗn hợp được nhận; tiêu đề chuẩn hóa trùng bị từ chối; bản ghi chỉ khoảng trắng bị bỏ; bản ghi ngắn được đệm; giá trị tiêu đề lạ bị bỏ; ký tự sau ngoặc đóng bị từ chối. Theo quyết định đã ghi. Phân tích
samples/customer-contacts.csvvới initialContacts và xác nhận số lượng{ready:3,invalid:1,duplicate:2}, rows dài 6,, hợp nhất added 3/skipped 3/contacts dài 5.
Nhiệm vụ 2: tải tích hợp và xem xét đầy đủ
Tệp: Sửa index.html, src/app.js, style.css duy nhất; phụ thuộc nhiệm vụ 1. Giao diện: Dùng previewCSV và Preview từ nhiệm vụ 1. Tạo phần cục bộ ứng dụng renderImport() và trạng thái mô tả trong docs/architecture.md; giữ phần hiện có #search, #contacts, #status, #add và hành vi. Thêm phần tích hợp có nhãn và ô tệp; tên truy cập được của điều khiển là Import contacts, CSV file, Review CSV, Import ready contacts, Cancel import, Back to contacts, Fail next save.
- [ ] Thêm điều khiển, giới hạn/trợ giúp và vùng xem theo
docs/screen-design.md; giữ xác nhận tắt đến khi nối đường lưu trong nhiệm vụ 3. - [ ] Nối mở, chọn tệp, Review CSV rõ ràng, hủy và Back; bảo vệ đọc bất đồng bộ bằng token thế hệ và xóa lỗi/xem trước cũ khi có đầu vào mới. Dùng textContent hiển thị từng bản ghi với trường/trạng thái/lý do và số lượng.
- [ ] Thêm kiểu đáp ứng hẹp cho thẻ có nhãn, nút xuống dòng và văn bản dài, giữ kiểu liên hệ và tiêu điểm nhìn thấy.
- [ ] Với
npm startvàhttp://127.0.0.1:8830, chọn mẫu: dự kiến sáu thẻ và số 3/1/2. Cancel/Back phải giữ danh sách, tìm kiếm và giá trị lưu. Chọn tệp lỗi: có lỗi và xác nhận tắt; chọn tệp hợp lệ sau: lỗi cũ biến mất. Chọn/hủy nhanh trong khi đọc: không xem trước cũ quay lại. Đây là kiểm tra trình duyệt cho lần thực thi sau, không phải kết quả hiện tại.
Nhiệm vụ 3: xác nhận tuần tự, thất bại và thử lại
Tệp: Sửa src/app.js duy nhất (điều khiển đã có ở nhiệm vụ 2). Phụ thuộc nhiệm vụ 1–2. Giao diện: Dùng mergeReady và addContact/readContacts/saveContacts hiện có. Tạo bộ điều phối cục bộ ứng dụng persistContacts(makeNext) , trong đó makeNext nhận Contact[] hiện tại đã xác nhận và trả {contacts:Contact[], added:number, skipped:number} (Add điều chỉnh hàng mới vào dạng này). Promise trả kết quả sau lưu; từ chối khi bận/lỗi. Không API export mới hay sửa bộ điều hợp lưu trữ.
- [ ] Đưa các lần lưu Add và Import qua bảo vệ bận đồng bộ và bộ điều phối trong
docs/architecture.md. Import hợp nhất lại khi xác nhận; không tính trước liên hệ kế tiếp lúc tải lên. Tắt điều khiển sửa khi lưu. Chỉ công bố rows sau khi lưu thành công; không thông báo thành công trong catch/finally. - [ ] Nối Fail next save như checkbox có nhãn được dùng bởi lần lưu thật tiếp theo. Lỗi giữ xem trước và danh sách xác nhận, giải phóng busy và cho thử lại; hủy không bao giờ ghi. Khi không thêm sau xác minh lại, bỏ lưu và báo số thực.
- [ ] Nhập thành công xóa xem trước và tìm kiếm, kết xuất cùng danh sách, báo số thêm/bỏ qua thực tế và trả tiêu điểm. Xác nhận tắt khi không Ready và bảo vệ đồng bộ phải ngăn gửi lặp.
- [ ] Trong trình duyệt mới nhập mẫu: dự kiến added 3/skipped 3,, tổng 5,; tìm kiếm thấy Jordan, Add vẫn thêm liên hệ duy nhất và tải lại giữ mọi liên hệ lưu.
- [ ] Ngữ cảnh mới: xem mẫu, thêm Jordan thủ công rồi xác nhận; dự kiến added 2/skipped 4 và giữ liên hệ thủ công. Nhấp đúp xác nhận, thử Add trong 180ms lưu: không nhập trùng, ghi chồng hoặc mất liên hệ.
- [ ] Ngữ cảnh mới: xem mẫu, bật Fail next save và xác nhận; danh sách/lưu trữ không đổi, giữ xem trước sáu bản ghi với trạng thái lỗi. Thử lại: dự kiến added 3/skipped 3 một lần, rồi tải lại và xác minh năm liên hệ. Thử cả Add thất bại để tránh hồi quy. Không Ready thì không được xác nhận.
Nhiệm vụ 4: xác minh chấp nhận và xem xét phạm vi
Tệp: Chỉ xem tệp ứng dụng được phép; kiểm thử không đổi. Phụ thuộc nhiệm vụ 1–3. Giao diện: Toàn bộ UI Contacts hiện có cùng điều khiển nhiệm vụ 2; không API mới.
- [ ] Chạy
npm test; dự kiến cả 17 kiểm thử bất biến đạt. Không coi là độ bao phủ lưu trữ/khả năng truy cập trình duyệt. - [ ] Kiểm tra trình duyệt bản ghi mẫu 6 trong xem trước và danh sách nhập: nguyên văn
<img src=x onerror=alert(1)>, không phần tử img hay thực thi. Kiểm tra company nhiều dòng trong ngoặc và email/company dài có xuống dòng dễ đọc. - [ ] Ở 320px và desktop, dùng chỉ bàn phím để tải/xem/hủy/xác nhận và Add. Kiểm tra nhãn, Tab logic, tiêu điểm nhìn thấy, tiêu điểm tiêu đề, số/lỗi/thành công trực tiếp, trả tiêu điểm khi hủy và không cuộn ngang trang. Không bẫy tiêu điểm; truy cập được cả sáu bản ghi.
- [ ] Xác minh tiêu đề/ngoặc/trường thừa sai và đầu vào quá lớn không đổi liên hệ lưu; 501 bản ghi bị từ chối và 500 được nhận. Gồm bản xem trước không Ready.
- [ ] Xem diff: chỉ bốn đường dẫn ứng dụng được phép thay đổi; tệp bất biến và sản phẩm chọn nguồn không đổi. Báo kết quả kiểm thử/trình duyệt thực cùng giới hạn. Dừng cho người vận hành xem xét; không commit hay triển khai.
Tệp gốc và chi tiết nguồn
output/docs/implementation-plan.md.txt · SHA-256 f20d8893150a7b9a6b0a44bfdc711d92b535cf9f8fcb7de224c4892b75c5cbf1
# CRM-142 Import Contacts Implementation Plan
> For agentic workers: planning only in this run. Later execution stays native and single-agent; no additional skills, commits, worktrees or delegation. User/project instructions override the upstream execution-subskill template.
**Goal:** Import reviewed CSV contacts into the existing local list without losing contacts or saving during preview.
**Architecture:** Pure parsing/classification/merge in a new import module; inline panel and serialized writes in the existing app. Existing contacts/storage adapters remain unchanged.
**Tech Stack:** Plain HTML/CSS/browser ES modules, localStorage; Node >=24 built-in test runner, no dependencies.
**Spec:** `docs/brainstorming.md`, `docs/screen-design.md`, `docs/architecture.md`; fixed requirements in `tickets/CRM-142.md` and `docs/import-contract.md`.
## Global constraints
Application changes only: `index.html`, `style.css`, `src/app.js`, new `src/import.js`. Tests, samples, ticket, server, package, AGENTS.md and both existing helper modules are immutable. No external services, dependency changes, companion execution, global installation, commits, deployment or publishing. No login, server database, field-mapping wizard, remote uploads, live CRM account or background jobs. Limit 100,000 UTF-8 bytes and 500 nonblank data records; display limits before upload. Preview never persists. Existing search/Add remain working. No implementation is authorized in this phase.
## Review focus
The immutable module tests cover many parsing cases but do not prove UI/storage behavior. Include these targeted manual checks in the owning tasks:
1. Task 3: rapid Add/Import and double confirmation cannot race a save or lose contacts.
2. Task 3: failed save retains preview and old persisted data; retry adds each contact once.
3. Task 2: late file reads after selection/cancel cannot restore stale previews.
4. Task 4: HTML-looking cells remain literal text in both preview and list; no injected element.
5. Task 4: keyboard focus/status and long values remain usable at 320px.
## Task 1: Pure CSV preview and merge
**Files:** Create `src/import.js`; consume unchanged `src/contacts.js`; verify unchanged `tests/import.test.js` and `tests/contacts.test.js`.
**Interfaces:** Produce `previewCSV(text: string, existing: Contact[]): Preview` and `mergeReady(preview: Preview, existing: Contact[]): {contacts: Contact[], added: number, skipped: number}`. Contact is the existing `{id,name,email,company}` shape; Preview is `{rows: {record:number,name:string,email:string,company:string,status:'ready'|'invalid'|'duplicate',reason:string}[], counts:{ready:number,invalid:number,duplicate:number}}`. Consume normalEmail, validContact and addContact from `src/contacts.js`.
- [ ] Run `node --test tests/import.test.js` before implementation; with the current tree expect missing `src/import.js`, not a product behavior failure. Do not edit tests.
- [ ] Implement previewCSV with scanner/limits/header decisions in `docs/architecture.md`; ensure structural validation completes before returning any preview. Classify fields before duplicate tracking, keeping every nonblank record and exact counts.
- [ ] Implement mergeReady, rechecking Ready records against its supplied current contacts. Preserve inputs and old contacts, add IDs through addContact, count every non-added file record as skipped.
- [ ] Run `node --test tests/import.test.js tests/contacts.test.js`; expect all 17 existing tests to pass (12 import, 5 contacts). These prove quoting, BOM/CRLF, optional company, blank records, malformed structures, invalid-first duplicates, merge revalidation, immutability and limits.
- [ ] Run an inline Node module assertion probe (no saved test edits): exact 100,000-byte ASCII input accepted and 100,001 rejected; trimmed mixed-case headers accepted; duplicate normalized headers rejected; whitespace-only records ignored; short records padded; unknown header values ignored; characters after closing quote rejected. Match the documented choices. Parse `samples/customer-contacts.csv` with initialContacts and assert counts `{ready:3,invalid:1,duplicate:2}`, rows length 6, merged added 3/skipped 3/contacts length 5.
## Task 2: Inline upload and complete review
**Files:** Modify `index.html`, `src/app.js`, `style.css` only; depends on Task 1.
**Interfaces:** Consume previewCSV and Preview from Task 1. Produce app-local `renderImport()` and state described in `docs/architecture.md`; keep existing `#search`, `#contacts`, `#status`, `#add` and their behavior. Add a labelled inline section and file input; control accessible names are `Import contacts`, `CSV file`, `Review CSV`, `Import ready contacts`, `Cancel import`, `Back to contacts`, `Fail next save`.
- [ ] Add controls, limit/help text and review container according to `docs/screen-design.md`; keep confirmation disabled until the save path in Task 3 is connected.
- [ ] Connect opening, file selection, explicit Review CSV, cancellation and Back; guard asynchronous reads with a generation token and clear prior errors/preview on new input. Show every record with fields/status/reason and counts using textContent.
- [ ] Add narrow responsive styles for labelled record cards, wrapped buttons and long text, retaining existing contact styling and visible focus.
- [ ] With `npm start` and `http://127.0.0.1:8830`, select the sample: expect six cards and 3/1/2 counts. Cancel/Back must preserve list, search and stored value. Select a malformed file: expect error and disabled confirmation; select a valid file afterward: old error disappears. Select/cancel rapidly while reading: no stale preview returns. These are browser checks for later execution, not claims of current results.
## Task 3: Serialized confirmation, failure and retry
**Files:** Modify `src/app.js` only (controls already defined in Task 2). Depends on Tasks 1–2.
**Interfaces:** Consume mergeReady and existing addContact/readContacts/saveContacts. Produce app-local `persistContacts(makeNext)` coordinator, where makeNext receives current committed Contact[] and returns `{contacts:Contact[], added:number, skipped:number}` (Add adapts its one new row to this shape). Promise resolves that result after storage; rejects on busy/failure. No new exported API or storage adapter changes.
- [ ] Route existing Add and Import saves through the synchronous busy guard and coordinator in `docs/architecture.md`. Import remerges at confirmation; do not precompute next contacts during upload. Disable mutation controls while saving. Publish rows only after successful storage; no success message in catch/finally.
- [ ] Wire Fail next save as a labelled checkbox consumed by the next actual adapter save. Failure retains preview and committed list, releases busy and restores retry; cancellation never writes. For zero additions after revalidation skip storage and report actual counts.
- [ ] On successful import clear preview and search, render the same list, announce actual added/skipped counts and restore focus. Ensure disabled zero-Ready confirmation and synchronous guarding prevent repeat submissions.
- [ ] In a fresh browser import the sample: expect added 3/skipped 3, total 5, search finds Jordan, Add still adds a unique contact, and reload retains all saved contacts.
- [ ] Fresh context: preview sample, Add Jordan manually, then confirm; expect added 2/skipped 4 and preservation of the manual contact. Double-click confirm and attempt Add during the 180ms save: no duplicate import, overlapping save or lost contact.
- [ ] Fresh context: preview sample, arm Fail next save and confirm; expect unchanged list/storage and retained six-record preview with failure status. Retry: expect added 3/skipped 3 once, then reload and verify five contacts. Also exercise failed Add to ensure no regression. Zero-Ready preview must not allow confirmation.
## Task 4: Acceptance verification and scope review
**Files:** Review only allowed application files; tests remain unchanged. Depends on Tasks 1–3.
**Interfaces:** Full existing Contacts UI plus the Task 2 controls; no new API.
- [ ] Run `npm test`; expect all 17 immutable tests to pass. Do not equate this with browser storage/accessibility coverage.
- [ ] Browser check sample record 6 in preview and imported list: literal `<img src=x onerror=alert(1)>`, no img element or execution. Check a quoted multiline company and a long email/company for readable wrapping.
- [ ] At 320px and desktop, operate upload/review/cancel/confirm and Add using keyboard only. Verify labels, logical tab order, visible focus, review-heading focus, live counts/errors/success, cancellation focus restoration and no horizontal page scroll. No focus trap; all six sample records reachable.
- [ ] Check malformed headers/quotes/extra fields and oversized input leave stored contacts unchanged; confirm 501 records rejected and 500 accepted. Include a preview with no Ready rows.
- [ ] Inspect diff: only the four allowed application paths changed during implementation; immutable files and selection artifacts unchanged. Report actual test/browser outcomes and any limitation. Stop for operator review; do not commit or deploy.