Files
makelore/tests/unit/reference-tokens.test.ts

40 lines
1.5 KiB
TypeScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import { describe, expect, it } from 'vitest';
import {
findUnboundReferenceNumbers,
findReferenceMentionRange,
insertReferenceToken,
promptReferencesImage,
removeReferenceTokenAndShift,
} from '@/pages/ImageCanvas/reference-tokens';
describe('Canvas reference tokens', () => {
it('recognizes explicit numbered image aliases only', () => {
expect(promptReferencesImage('参考 @图片1 和 @图2', 0)).toBe(true);
expect(promptReferencesImage('参考 @图片1 和 @图2', 1)).toBe(true);
expect(promptReferencesImage('普通的 @图片 文字', 0)).toBe(false);
});
it('reports aliases that do not have a bound reference', () => {
expect(findUnboundReferenceNumbers('参考 @图片1、@图3 和 @图片2再看 @图片3', 1))
.toEqual([2, 3]);
expect(findUnboundReferenceNumbers('不要使用 @图片0', 2)).toEqual([0]);
});
it('finds an unfinished @ mention at the cursor', () => {
expect(findReferenceMentionRange('让 @图', 4)).toEqual({ start: 2, end: 4 });
expect(findReferenceMentionRange('让 @图 继续', 7)).toBeNull();
});
it('inserts a token at the mention range', () => {
expect(insertReferenceToken('让 @ 作为主体', 1, { start: 2, end: 3 })).toEqual({
value: '让 @图片2 作为主体',
cursor: 6,
});
});
it('removes the deleted token and shifts later aliases with their bindings', () => {
expect(removeReferenceTokenAndShift('参考 @图片1结合 @图片2 的光线', 0))
.toBe('参考 结合 @图片1 的光线');
});
});