刘光辉
8 小时以前 cf11e63b659afcc9792ab3e395f1b45d844431a8
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
import { describe, expect, it } from 'vitest';
 
import { insertAnnotationControlCommand, parseInsertResult, stageFlowVersionPropertiesCommand } from '../../src/onlyoffice/commands';
 
describe('签批页标注 OnlyOffice 命令', () => {
  it('命令只使用编辑器 Api 与 Asc.scope', () => {
    const source = insertAnnotationControlCommand.toString();
    expect(source).toContain('Asc.scope.dmsQianpiyeInsert');
    expect(source).toContain('Api.GetDocument()');
    expect(source).not.toContain('window.');
  });
 
  it('规范化无效编辑器返回值', () => {
    expect(parseInsertResult('not-json')).toEqual({ ok: false, error: '签批页标注命令返回无效' });
    expect(parseInsertResult(JSON.stringify({ ok: false, error: '无法插入' }))).toEqual({ ok: false, error: '无法插入' });
  });
 
  it('接受 AddInlineLvlSdt 的 void 返回值,允许重复插入同一字段并以写后校验确认成功', () => {
    const controls: Array<{ GetTag: () => string; SetAlias: () => boolean; SetAppearance: () => void; SetLock: () => boolean; SetTag: (value: string) => boolean; AddElement: () => boolean }> = [];
    const control = {
      AddElement: () => true,
      GetTag: () => tag,
      SetAlias: () => true,
      SetAppearance: () => undefined,
      SetLock: () => true,
      SetTag: (value: string) => {
        tag = value;
        return true;
      },
    };
    let tag = '';
    const paragraph: { AddInlineLvlSdt: (value: typeof control) => void; control?: typeof control } = {
      AddInlineLvlSdt(value) {
        this.control = value;
      },
    };
    const originalAsc = (globalThis as any).Asc;
    const originalApi = (globalThis as any).Api;
    try {
      (globalThis as any).Asc = { scope: { dmsQianpiyeInsert: { alias: '申请人', placeholder: '{{申请人}}', tag: 'dms.qianpiye.flow.start.applicant' } } };
      (globalThis as any).Api = {
        CreateInlineLvlSdt: () => control,
        CreateParagraph: () => paragraph,
        CreateRun: () => ({ AddText: () => true }),
        GetDocument: () => ({
          GetAllContentControls: () => controls,
          InsertContent: () => {
            controls.push(paragraph.control!);
            return true;
          },
        }),
      };
 
      expect(JSON.parse(insertAnnotationControlCommand())).toEqual({ ok: true });
      expect(JSON.parse(insertAnnotationControlCommand())).toEqual({ ok: true });
      expect(controls).toHaveLength(2);
    } finally {
      (globalThis as any).Asc = originalAsc;
      (globalThis as any).Api = originalApi;
    }
  });
 
  it('暂存流程版本属性,并由服务端在保存时归一化重复属性', () => {
    const properties = new Map<string, string>([
      ['DMS_NEW_FLOW_VERSION_ID', 'old-new'],
      ['DMS_EDIT_FLOW_VERSION_ID', 'old-revision'],
    ]);
    const originalAsc = (globalThis as any).Asc;
    const originalApi = (globalThis as any).Api;
    try {
      (globalThis as any).Asc = { scope: { dmsQianpiyeFlowVersionProperties: {
        newFlowVersionId: 'new-version', revisionFlowVersionId: 'revision-version',
      } } };
      (globalThis as any).Api = { GetDocument: () => ({
        GetCustomProperties: () => ({
          Add: (key: string, value: string) => { properties.set(key, value); return true; },
          Get: (key: string) => properties.get(key),
        }),
      }) };
 
      expect(JSON.parse(stageFlowVersionPropertiesCommand())).toEqual({ ok: true });
      expect(properties).toEqual(new Map([
        ['DMS_NEW_FLOW_VERSION_ID', 'new-version'],
        ['DMS_EDIT_FLOW_VERSION_ID', 'revision-version'],
      ]));
    } finally {
      (globalThis as any).Asc = originalAsc;
      (globalThis as any).Api = originalApi;
    }
  });
});