刘光辉
6 小时以前 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
import { describe, expect, it } from 'vitest';
 
import { parsePluginContext } from '../../src/domain/plugin-context';
 
const context = {
  apiBaseUrl: 'http://localhost:30000',
  documentId: 'file-1',
  bizDataId: 'record-1',
  bizModule: 'eln.template',
  bizScene: 'eln.template.annotate',
  pluginVersion: '0.1.0',
  scope: 'onlyoffice:eln-template-annotate',
  ticket: 'opaque-ticket',
  canAnnotate: true,
};
 
describe('plugin context', () => {
  it('从 ONLYOFFICE 传给当前插件的扁平 options 读取运行上下文副本', () => {
    const result = parsePluginContext(context);
 
    expect(result).toEqual(context);
    expect(result).not.toBe(context);
  });
 
  it('保留后端签名配置中的可选修复绑定提示', () => {
    expect(parsePluginContext({ ...context, repairBindingId: 'binding-1' })).toMatchObject({ repairBindingId: 'binding-1' });
  });
 
  it('只读定位模式允许省略 scope 和 ticket,并拒绝可写定位模式', () => {
    const { scope: _scope, ticket: _ticket, ...locateContext } = context;
    expect(parsePluginContext({ ...locateContext, canAnnotate: false, locateControlTag: 'eln.field.orphan' })).toMatchObject({ locateControlTag: 'eln.field.orphan' });
    expect(() => parsePluginContext({ ...context, locateControlTag: 'eln.field.orphan' })).toThrow('插件运行上下文无效');
  });
 
  it.each([
    undefined,
    {},
    { ...context, documentId: '' },
    { ...context, bizModule: 'other' },
    { ...context, bizScene: 'other' },
    { ...context, pluginVersion: '0.2.0' },
    { ...context, scope: 'other' },
    { ...context, ticket: '' },
    { ...context, ticket: '  ' },
    { ...context, canAnnotate: false, scope: undefined },
    { ...context, canAnnotate: false, ticket: undefined },
    { ...context, repairBindingId: 1 },
    { ...context, canAnnotate: false, locateControlTag: 1 },
    { ...context, canAnnotate: 'true' },
    { ...context, apiBaseUrl: '' },
    { ...context, apiBaseUrl: 'javascript:alert(1)' },
  ])('拒绝缺失或非法的运行上下文', (options) => {
    expect(() => parsePluginContext(options)).toThrow('插件运行上下文无效');
  });
});