import { describe, expect, it } from 'vitest' import { changedKeys, dropNotes, moveNotes, normalizeKey, notesUnder, placeNotes, toNoteKey, withNote } from './noteMap' describe('注释的键', () => { it('相对工作区文件夹、用 / 分隔', () => { expect(toNoteKey('/work/luffy', '/work/luffy/backend/src/campaign')).toBe('backend/src/campaign') expect(toNoteKey('/work/luffy/', '/work/luffy/a.ts')).toBe('a.ts') // Windows 上 Uri.path 也是 / 分隔,盘符在最前面 expect(toNoteKey('/c:/work/luffy', '/c:/work/luffy/src/a.ts')).toBe('src/a.ts') }) it('工作区文件夹本身和外面的路径没有键', () => { expect(toNoteKey('/work/luffy', '/work/luffy')).toBeUndefined() expect(toNoteKey('/work/luffy', '/work/other/a.ts')).toBeUndefined() expect(toNoteKey('/work/luffy', '/work/luffy-2/a.ts')).toBeUndefined() }) it('手写的键规范成同一种写法,不合法的丢掉', () => { expect(normalizeKey('./src/a.ts')).toBe('src/a.ts') expect(normalizeKey('src//campaign/')).toBe('src/campaign') expect(normalizeKey('src/x/../a.ts')).toBe('src/a.ts') expect(normalizeKey('')).toBeUndefined() expect(normalizeKey('.')).toBeUndefined() expect(normalizeKey('../a.ts')).toBeUndefined() expect(normalizeKey('/etc/passwd')).toBeUndefined() }) }) describe('保存注释', () => { it('去掉首尾空白,不动别的注释', () => { expect(withNote({ 'b.ts': 'B' }, 'a.ts', ' 营销活动 ')).toEqual({ 'a.ts': '营销活动', 'b.ts': 'B' }) }) it('改已有的注释', () => { expect(withNote({ 'a.ts': '旧' }, 'a.ts', '新')).toEqual({ 'a.ts': '新' }) }) it('清空或只有空白就删掉这条', () => { expect(withNote({ 'a.ts': 'A', 'b.ts': 'B' }, 'a.ts', '')).toEqual({ 'b.ts': 'B' }) expect(withNote({ 'a.ts': 'A' }, 'a.ts', ' ')).toEqual({}) }) it('不改传进来的那份', () => { const notes = { 'a.ts': 'A' } withNote(notes, 'a.ts', '') expect(notes).toEqual({ 'a.ts': 'A' }) }) }) describe('改名、挪位置', () => { const notes = { 'campaign': '营销活动', 'campaign/benefits.py': '权益发放', 'campaign/console/views.py': '后台', 'campaign2/x.py': '名字只是前缀相同', 'user/routes.py': '用户接口', } it('文件改名,注释跟着走', () => { expect(moveNotes(notes, 'user/routes.py', 'user/api.py')).toEqual({ 'campaign': '营销活动', 'campaign/benefits.py': '权益发放', 'campaign/console/views.py': '后台', 'campaign2/x.py': '名字只是前缀相同', 'user/api.py': '用户接口', }) }) it('目录挪位置,里面各层的注释一起走,名字只是前缀相同的不动', () => { expect(moveNotes(notes, 'campaign', 'marketing/campaign')).toEqual({ 'marketing/campaign': '营销活动', 'marketing/campaign/benefits.py': '权益发放', 'marketing/campaign/console/views.py': '后台', 'campaign2/x.py': '名字只是前缀相同', 'user/routes.py': '用户接口', }) }) it('没有注释的目录挪走,里面有注释的文件照样跟着走', () => { expect(moveNotes({ 'src/a/b.ts': 'B' }, 'src/a', 'lib/a')).toEqual({ 'lib/a/b.ts': 'B' }) }) it('挪到另一个工作区文件夹:先取出再放进去', () => { const moving = notesUnder(notes, 'campaign') expect(moving).toEqual({ '': '营销活动', 'benefits.py': '权益发放', 'console/views.py': '后台' }) expect(placeNotes({ 'other.ts': 'O' }, 'pkg/campaign', moving)).toEqual({ 'other.ts': 'O', 'pkg/campaign': '营销活动', 'pkg/campaign/benefits.py': '权益发放', 'pkg/campaign/console/views.py': '后台', }) }) }) describe('删除', () => { it('删目录连带里面各层的注释,名字只是前缀相同的留着', () => { expect(dropNotes({ 'campaign': '营销活动', 'campaign/benefits.py': '权益发放', 'campaign/console/views.py': '后台', 'campaign2/x.py': '留着', }, 'campaign')).toEqual({ 'campaign2/x.py': '留着' }) }) it('删文件只删它自己', () => { expect(dropNotes({ 'a.ts': 'A', 'b.ts': 'B' }, 'a.ts')).toEqual({ 'b.ts': 'B' }) }) }) describe('变了哪些行', () => { it('增、删、改都算,没变的不算', () => { expect(changedKeys({ a: '1', b: '2', c: '3' }, { a: '1', b: '二', d: '4' }).sort()).toEqual(['b', 'c', 'd']) expect(changedKeys({ a: '1' }, { a: '1' })).toEqual([]) }) })