Files
superwork/vscode/webview-ui/src/codeReader/sharing.test.ts
T

80 lines
3.8 KiB
TypeScript

import type { UnhashedBlock } from './testBlocks'
import { describe, expect, it } from 'vitest'
import { shareBlocks, shareUnchanged } from './sharing'
import { hashed, NO_SPAN } from './testBlocks'
describe('新文档里指纹没变的块沿用上一份的对象', () => {
function card(id: string, text: string, children: UnhashedBlock[] = []): UnhashedBlock {
return { id, anchor: id, kind: 'statement', span: NO_SPAN, attributes: {}, concepts: [], children, translation: [{ text, role: 'plain' }] }
}
const PREV = hashed([card('0', 'a'), card('1', 'b', [card('1.0', 'c'), card('1.1', 'd')]), card('2', 'e')])
it('整份一样:交回上一份本身;没有上一份时原样交回', () => {
const same = hashed([card('0', 'a'), card('1', 'b', [card('1.0', 'c'), card('1.1', 'd')]), card('2', 'e')])
expect(shareBlocks(PREV, same)).toBe(PREV)
expect(shareBlocks(undefined, same)).toBe(same)
})
it('子块改了:它和包着它的块是新的,别的块(连同改了的块旁边没变的子块)还是原来的对象', () => {
const next = hashed([card('0', 'a'), card('1', 'b', [card('1.0', 'c'), card('1.1', 'D')]), card('2', 'e')])
const shared = shareBlocks(PREV, next)
expect(shared).toEqual(next)
expect(shared).not.toBe(PREV)
expect(shared[0]).toBe(PREV[0])
expect(shared[2]).toBe(PREV[2])
expect(shared[1]).not.toBe(PREV[1])
expect(shared[1].children[0]).toBe(PREV[1].children[0])
expect(shared[1].children[1]).not.toBe(PREV[1].children[1])
})
it('多了、少了块:那一层是新的数组,没变的块照样沿用', () => {
const longer = shareBlocks(PREV, hashed([card('0', 'a'), card('1', 'b', [card('1.0', 'c'), card('1.1', 'd')]), card('2', 'e'), card('3', 'f')]))
expect(longer).not.toBe(PREV)
expect(longer.slice(0, 3)).toEqual(PREV)
longer.slice(0, 3).forEach((block, index) => expect(block).toBe(PREV[index]))
const shorter = shareBlocks(PREV, hashed([card('0', 'a'), card('1', 'b', [card('1.0', 'c')]), card('2', 'e')]))
expect(shorter[1]).not.toBe(PREV[1])
expect(shorter[1].children[0]).toBe(PREV[1].children[0])
expect(shorter[2]).toBe(PREV[2])
})
})
describe('新消息里没变的部分沿用上一份的对象', () => {
const prev = {
version: 1,
blocks: [
{ id: '0', translation: [{ text: 'a', offset: 1 }], children: [] },
{ id: '1', translation: [{ text: 'b', offset: 5 }], children: [{ id: '1.0', children: [] }] },
],
}
it('整份一样:交回上一份本身', () => {
expect(shareUnchanged(prev, structuredClone(prev))).toBe(prev)
})
it('只改了一块:那一块(连同它的各层父对象)是新的,别的块还是原来的对象', () => {
const next = structuredClone(prev)
next.version = 2
next.blocks[1].translation[0].offset = 6
const shared = shareUnchanged(prev, next)
expect(shared).not.toBe(prev)
expect(shared).toEqual(next)
expect(shared.blocks).not.toBe(prev.blocks)
expect(shared.blocks[0]).toBe(prev.blocks[0])
expect(shared.blocks[1]).not.toBe(prev.blocks[1])
expect(shared.blocks[1].children).toBe(prev.blocks[1].children)
})
it('多了、少了项或键都算变了;没有上一份时原样交回', () => {
const longer = { ...structuredClone(prev), blocks: [...structuredClone(prev.blocks), { id: '2', translation: [], children: [] }] }
const shared = shareUnchanged(prev, longer)
expect(shared.blocks).not.toBe(prev.blocks)
expect(shared.blocks[0]).toBe(prev.blocks[0])
const withKey = { ...structuredClone(prev), error: 'x' }
expect(shareUnchanged(prev, withKey)).not.toBe(prev)
const { version: _, ...withoutKey } = structuredClone(prev)
expect(shareUnchanged(prev, withoutKey)).not.toBe(prev)
expect(shareUnchanged(undefined, withKey)).toBe(withKey)
})
})