@x-oasis/diff-match-patch 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,110 @@
1
+ import { expect, test, describe } from 'vitest';
2
+ import { FileRestoreManager, restoreRange } from '../src';
3
+
4
+ describe('FileRestoreManager', () => {
5
+ test('should restore range when content is unchanged', () => {
6
+ const original = 'Hello World';
7
+ const current = 'Hello World';
8
+ const manager = new FileRestoreManager(original);
9
+
10
+ const result = manager.restoreRange(current, {
11
+ startOffset: 0,
12
+ endOffset: 5,
13
+ });
14
+ expect(result).toBe('Hello World');
15
+ });
16
+
17
+ test('should restore range when content has insertions', () => {
18
+ const original = 'Hello World';
19
+ const current = 'Hello Beautiful World';
20
+ const manager = new FileRestoreManager(original);
21
+
22
+ // 恢复插入的 "Beautiful " 部分(offset 6-15)
23
+ const result = manager.restoreRange(current, {
24
+ startOffset: 6,
25
+ endOffset: 15,
26
+ });
27
+ expect(result).toBe('Hello World');
28
+ });
29
+
30
+ test('should restore range when content has deletions', () => {
31
+ const original = 'Hello Beautiful World';
32
+ const current = 'Hello World';
33
+ const manager = new FileRestoreManager(original);
34
+
35
+ // 在删除的位置恢复内容(在 "Hello " 和 "World" 之间)
36
+ const result = manager.restoreRange(current, {
37
+ startOffset: 6,
38
+ endOffset: 6,
39
+ });
40
+ expect(result).toBe('Hello Beautiful World');
41
+ });
42
+
43
+ test('should restore range when content has modifications', () => {
44
+ const original = 'Hello World';
45
+ const current = 'Hello Beautiful World';
46
+ const manager = new FileRestoreManager(original);
47
+
48
+ // 恢复整个修改的部分
49
+ const result = manager.restoreRange(current, {
50
+ startOffset: 0,
51
+ endOffset: current.length,
52
+ });
53
+ expect(result).toBe(original);
54
+ });
55
+
56
+ test('should restore partial range in modified content', () => {
57
+ const original = 'The quick brown fox';
58
+ const current = 'The fast brown fox';
59
+ const manager = new FileRestoreManager(original);
60
+
61
+ // 恢复 "fast" 为 "quick"(offset 4-8)
62
+ const result = manager.restoreRange(current, {
63
+ startOffset: 4,
64
+ endOffset: 8,
65
+ });
66
+ expect(result).toBe('The quick brown fox');
67
+ });
68
+
69
+ test('should handle complex changes', () => {
70
+ const original = 'function test() {\n return true;\n}';
71
+ const current =
72
+ 'function test() {\n console.log("test");\n return true;\n}';
73
+ const manager = new FileRestoreManager(original);
74
+
75
+ // 恢复插入的 console.log 行
76
+ const result = manager.restoreRange(current, {
77
+ startOffset: 20,
78
+ endOffset: 42,
79
+ });
80
+ expect(result).toBe(original);
81
+ });
82
+
83
+ test('should throw error for invalid offset range', () => {
84
+ const original = 'Hello World';
85
+ const current = 'Hello World';
86
+ const manager = new FileRestoreManager(original);
87
+
88
+ expect(() => {
89
+ manager.restoreRange(current, { startOffset: 10, endOffset: 5 });
90
+ }).toThrow('Invalid offset range');
91
+
92
+ expect(() => {
93
+ manager.restoreRange(current, { startOffset: -1, endOffset: 5 });
94
+ }).toThrow('Invalid offset range');
95
+
96
+ expect(() => {
97
+ manager.restoreRange(current, { startOffset: 0, endOffset: 100 });
98
+ }).toThrow('Offset range exceeds current content length');
99
+ });
100
+ });
101
+
102
+ describe('restoreRange function', () => {
103
+ test('should restore range using convenience function', () => {
104
+ const original = 'Hello World';
105
+ const current = 'Hello Beautiful World';
106
+
107
+ const result = restoreRange(original, current, 6, 15);
108
+ expect(result).toBe('Hello World');
109
+ });
110
+ });
@@ -0,0 +1,11 @@
1
+ {
2
+ "extends": "../../../tsconfig.build.json",
3
+ "compilerOptions": {
4
+ "outDir": "./dist",
5
+ "esModuleInterop": true
6
+ },
7
+
8
+ "include": [
9
+ "src/**/*"
10
+ ]
11
+ }
package/tsconfig.json ADDED
@@ -0,0 +1,7 @@
1
+ {
2
+ "extends": "../../../tsconfig.json",
3
+ "compilerOptions": {
4
+ "jsx": "react",
5
+ "esModuleInterop": true
6
+ }
7
+ }
@@ -0,0 +1,21 @@
1
+ import { defineConfig } from 'vitest/config';
2
+
3
+ export default defineConfig({
4
+ test: {
5
+ globals: true,
6
+ include: ['test/**/*.(spec|test).ts'],
7
+ exclude: ['node_modules/**'],
8
+ threads: false,
9
+
10
+ coverage: {
11
+ provider: 'istanbul',
12
+ },
13
+ },
14
+
15
+ resolve: {
16
+ alias: {},
17
+ },
18
+ define: {
19
+ __DEV__: false,
20
+ },
21
+ });