/** * 14-textedit: 文本编辑——FS 保存/另存为/取消关闭/跨 App 同步 * 原测试: Playwright browser → 改为 FS + WM composable 层测试 */ import { describe, it, expect, beforeEach, afterEach } from 'vitest' import { fs } from '../../src/composables/useFS' describe('14-textedit — 文本编辑核心', () => { beforeEach(() => { localStorage.clear(); (fs as any).root = null; fs.init() }) const DOC = '/Users/guest/Documents' // ===== 新建 → 保存 ===== it('write 创建文件并 read 读取一致', () => { fs.write(DOC + '/note-a.txt', '第一行内容') expect(fs.exists(DOC + '/note-a.txt')).toBe(true) expect(fs.read(DOC + '/note-a.txt')).toBe('第一行内容') }) // ===== 修改内容(覆盖) ===== it('write 覆盖已有文件', () => { fs.write(DOC + '/note-a.txt', '原始') fs.write(DOC + '/note-a.txt', '修改后') expect(fs.read(DOC + '/note-a.txt')).toBe('修改后') }) // ===== 多行内容 ===== it('多行内容正确存储和读取', () => { fs.write(DOC + '/multi.txt', '第一行\n第二行\n第三行') expect(fs.read(DOC + '/multi.txt')).toBe('第一行\n第二行\n第三行') fs.remove(DOC + '/multi.txt') }) // ===== 另存为 → 两个文件存在 ===== it('copy 实现另存为效果', () => { fs.write(DOC + '/note-a.txt', '内容A') const np = fs.copy(DOC + '/note-a.txt', DOC) expect(fs.exists(DOC + '/note-a.txt')).toBe(true) // 原文件仍在 expect(fs.exists(np)).toBe(true) // 新文件存在 expect(fs.read(np)).toBe('内容A') fs.remove(np) fs.remove(DOC + '/note-a.txt') }) // ===== 取消关闭 → 不落盘 ===== it('文件内容在未 write 时不改变', () => { fs.write(DOC + '/note-a.txt', '原始内容') // 在应用层,用户编辑了但没保存——不应调用 write // 所以磁盘内容仍然是 "原始内容" expect(fs.read(DOC + '/note-a.txt')).toBe('原始内容') fs.remove(DOC + '/note-a.txt') }) // ===== 跨应用同步 → 重开显示最新 ===== it('write 后其他读取者能看到最新内容', () => { fs.write(DOC + '/shared.txt', '版本1') // 另一个"应用"读取 expect(fs.read(DOC + '/shared.txt')).toBe('版本1') // 更新 fs.write(DOC + '/shared.txt', '版本2') expect(fs.read(DOC + '/shared.txt')).toBe('版本2') fs.remove(DOC + '/shared.txt') }) // ===== 取消另存为 → 不写盘 ===== it('不调用 write 不会创建文件', () => { // 另存为取消 = 不调用 write expect(fs.exists(DOC + '/never-saved.txt')).toBe(false) }) // ===== 边界情况 ===== it('read 不存在文件抛错', () => { expect(() => fs.read('/no')).toThrow() }) it('write 同名目录抛错', () => { fs.mkdir(DOC + '/conflict') expect(() => fs.write(DOC + '/conflict', 'x')).toThrow('同名文件夹') fs.remove(DOC + '/conflict') }) it('FS.size 非零', () => { expect(fs.size()).toBeGreaterThan(0) }) it('list Documents 存在购物清单', () => { const items = fs.list(DOC) expect(items.some((i: any) => i.name.includes('购物清单'))).toBe(true) }) // 清理 afterEach(() => { try { fs.remove(DOC + '/note-a.txt') } catch {} try { fs.remove(DOC + '/note-b.txt') } catch {} }) })