Skip to content

test(preview): add mtime polling tests for PreviewContext file watch#1714

Merged
kaizhou-lab merged 1 commit intomainfrom
fix/preview-file-watch-review-followup
Mar 25, 2026
Merged

test(preview): add mtime polling tests for PreviewContext file watch#1714
kaizhou-lab merged 1 commit intomainfrom
fix/preview-file-watch-review-followup

Conversation

@piorpua
Copy link
Copy Markdown
Contributor

@piorpua piorpua commented Mar 25, 2026

Summary

Follow-up to #1686 — addresses all issues identified in the code review.

Note: Issues #1 (useEffect dependency), #2 (Promise rejection handling), and #4 (fileMtimeRef cleanup in closeTab) were already fixed in the original PR before merge. This PR adds test coverage for Issue #3.

Issues Fixed

# Severity File Issue Fix Applied
1 🟡 MEDIUM PreviewContext.tsx:596–609 activeTab 对象引用作为 effect 依赖,轮询频繁重置 ✅ Already fixed in #1686 (uses activeFilePath + activeTabRef)
2 🟡 MEDIUM PreviewContext.tsx:570,581 getFileMetadatareadPromise 的 reject 未处理 ✅ Already fixed in #1686 (.catch blocks added)
3 🟡 MEDIUM 无测试文件 新增轮询逻辑无测试覆盖 ✅ Added tests/unit/previewFileWatch.dom.test.ts (8 cases)
4 🔵 LOW PreviewContext.tsx:563 fileMtimeRef 不随 tab 关闭清理 ✅ Already fixed in #1686 (closeTab clears the entry)

Test Cases Added

  • getFileMetadata called immediately on tab switch
  • No file read when mtime is unchanged between polls
  • File content updated when mtime changes
  • Skip update when tab isDirty = true
  • Use getImageBase64 (not readFile) for image content type
  • No state corruption when file read rejects
  • getFileMetadata rejection handled without side effects
  • closeTab clears fileMtimeRef so reopening a file starts fresh

Related

Follow-up to #1686

Test Plan

  • bun run test — all tests pass (1401 passed)
  • bunx tsc --noEmit — no type errors
  • bun run lint:fix — lint clean
  • bun run format — format clean

Cover the checkFileUpdate and closeTab behaviors added in PR #1686:
- getFileMetadata called immediately on tab switch
- No file read when mtime is unchanged
- File content updated when mtime changes
- Skip update when tab isDirty
- Use getImageBase64 for image content type
- No state corruption when file read rejects
- getFileMetadata rejection handled without side effects
- closeTab clears fileMtimeRef so reopening starts fresh

Review follow-up for #1686
@piorpua
Copy link
Copy Markdown
Contributor Author

piorpua commented Mar 25, 2026

PR Fix 验证报告

原始 PR: #1686
Follow-up PR: #1714

# 严重级别 文件 问题 修复方式 状态
1 🟡 MEDIUM PreviewContext.tsx:596–609 activeTab 对象引用作为 effect 依赖,轮询频繁重置 原 PR 已用 activeFilePath + activeTabRef 修复(见当前 main 第 612–635 行) ✅ 已修复
2 🟡 MEDIUM PreviewContext.tsx:570,581 getFileMetadatareadPromise 的 reject 未处理 原 PR 已在两处添加 .catch 处理(见当前 main 第 601–607 行) ✅ 已修复
3 🟡 MEDIUM 无测试文件 新增轮询逻辑无测试覆盖 新增 tests/unit/previewFileWatch.dom.test.ts,8 个测试用例覆盖所有核心分支 ✅ 已修复
4 🔵 LOW PreviewContext.tsx:563 fileMtimeRef 不随 tab 关闭清理(轻微内存泄漏) 原 PR 已在 closeTab 中添加 fileMtimeRef.current.delete(见当前 main 第 347–351 行) ✅ 已修复

总结: ✅ 已修复 4 个 | ❌ 未能修复 0 个 | ⏭️ 跳过 0 个

@kaizhou-lab kaizhou-lab merged commit a553051 into main Mar 25, 2026
15 of 17 checks passed
@piorpua piorpua deleted the fix/preview-file-watch-review-followup branch March 25, 2026 13:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants