Skip to content

Build your own AI office with Univer Office SDK. Bring editing, calculation, collaboration, and file processing into your product for people and AI agents. Built on an extensible open-source core.

6.8 KB
Raw Blame
/**
 * Copyright 2023-present DreamNum Co., Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import { ContextService, DesktopLogService, IContextService, ILogService, Injector, IUniverInstanceService, UniverInstanceService } from '@univerjs/core';
import { describe, expect, it } from 'vitest';
import { DefinedNamesService, IDefinedNamesService } from '../defined-names.service';

const worksheet = { id: 'sheet-a' };

class TestUniverInstanceService {
    getUnit() {
        return {
            getSheetBySheetName: (sheetName: string) => (sheetName === 'Sheet1' ? worksheet : null),
        };
    }
}

function createDefinedNamesService() {
    const injector = new Injector();
    injector.add([IUniverInstanceService, { useClass: TestUniverInstanceService as never }]);
    injector.add([IDefinedNamesService, { useClass: DefinedNamesService }]);
    const service = injector.get(IDefinedNamesService);
    return {
        service,
        worksheet,
    };
}

describe('DefinedNamesService', () => {
    it.each(['__proto__', 'constructor', 'prototype', 'toString'])('keeps special identifiers (%s) out of the prototype chain after loading data', (key) => {
        const injector = new Injector([
            [DefinedNamesService],
            [IUniverInstanceService, { useClass: UniverInstanceService }],
            [IContextService, { useClass: ContextService }],
            [ILogService, { useClass: DesktopLogService }],
        ]);
        const service = injector.get(DefinedNamesService);
        const marker = '__formula_pollution__';
        try {
            service.registerDefinedName(key, { id: marker, name: 'Total', formulaOrRefString: '=1' });
            service.registerDefinedNames('unit', JSON.parse('{}'));
            service.registerDefinedName('unit', { id: key, name: key, formulaOrRefString: '=2' });
            expect(service.getValueById(key, marker)?.formulaOrRefString).toBe('=1');
            expect(service.getValueByName('unit', key)?.formulaOrRefString).toBe('=2');
            expect(Object.getOwnPropertyDescriptor(Object.prototype, marker)).toBeUndefined();
        } finally {
            Reflect.deleteProperty(Object.prototype, marker);
            Reflect.deleteProperty(Object, marker);
            injector.dispose();
        }
    });

    it('should register/query/remove defined names and update cache', () => {
        const { service } = createDefinedNamesService();

        service.registerDefinedName('unit-1', {
            id: 'id-1',
            name: 'Total',
            formulaOrRefString: 'Sheet1!A1',
        });
        expect(service.getValueById('unit-1', 'id-1')?.name).toBe('Total');
        expect(service.getValueByName('unit-1', 'Total')?.id).toBe('id-1');
        expect(service.getValueByName('unit-1', 'total')?.id).toBe('id-1');
        expect(service.hasDefinedName('unit-1')).toBe(true);

        service.removeDefinedName('unit-1', 'id-1');
        expect(service.getValueById('unit-1', 'id-1')).toBeUndefined();
        expect(service.hasDefinedName('unit-1')).toBe(false);
    });

    it('should support batch register and unit cleanup', () => {
        const { service } = createDefinedNamesService();
        service.registerDefinedNames('unit-2', {
            a: {
                id: 'a',
                name: 'NameA',
                formulaOrRefString: 'Sheet1!A1',
            },
            b: {
                id: 'b',
                name: 'NameB',
                formulaOrRefString: 'Sheet1!B1',
            },
        });

        expect(service.getDefinedNameMap('unit-2')).toBeDefined();
        expect(service.getAllDefinedNames()['unit-2']).toBeDefined();
        expect(service.getDefinedNameByRefString('unit-2', 'Sheet1!B1')?.id).toBe('b');

        service.removeUnitDefinedName('unit-2');
        expect(service.getDefinedNameMap('unit-2')).toBeUndefined();
    });

    it('should prefer sheet-local defined names over workbook-scope names', () => {
        const { service } = createDefinedNamesService();
        service.registerDefinedNames('unit-local', {
            local: {
                id: 'local',
                name: 'date_begin',
                localSheetId: 'sheet-weekly',
                formulaOrRefString: 'Weekly!$F$4',
            },
            global: {
                id: 'global',
                name: 'date_begin',
                localSheetId: 'AllDefaultWorkbook',
                formulaOrRefString: 'Report!$F$4',
            },
        });

        expect(service.getValueByName('unit-local', 'date_begin', 'sheet-weekly')?.id).toBe('local');
        expect(service.getValueByName('unit-local', 'date_begin', 'sheet-report')?.id).toBe('global');
        expect(service.getValueByName('unit-local', 'DATE_BEGIN')?.id).toBe('global');
    });

    it('should emit update/current/focus streams', () => {
        const { service } = createDefinedNamesService();
        let updateCount = 0;
        const ranges: any[] = [];
        const focused: any[] = [];

        service.update$.subscribe(() => updateCount++);
        service.currentRange$.subscribe((range) => ranges.push(range));
        service.focusRange$.subscribe((payload) => focused.push(payload));

        service.registerDefinedName('unit-3', {
            id: 'id-focus',
            name: 'F',
            formulaOrRefString: 'Sheet1!A1',
        });
        service.setCurrentRange({
            unitId: 'unit-3',
            sheetId: 'sheet-1',
            range: {
                startRow: 1,
                endRow: 2,
                startColumn: 3,
                endColumn: 4,
            },
        });
        service.focusRange('unit-3', 'id-focus');
        service.focusRange('unit-3', 'missing-id');

        expect(updateCount).toBeGreaterThan(0);
        expect(ranges[0]?.unitId).toBe('unit-3');
        expect(service.getCurrentRangeForString()).toBe('D2:E3');
        expect(focused).toEqual([
            {
                unitId: 'unit-3',
                id: 'id-focus',
                name: 'F',
                formulaOrRefString: 'Sheet1!A1',
            },
        ]);
    });

    it('should resolve worksheet by reference string', () => {
        const { service, worksheet } = createDefinedNamesService();

        expect(service.getWorksheetByRef('unit-1', 'Sheet1!A1')).toBe(worksheet);
        expect(service.getWorksheetByRef('unit-1', 'Missing!A1')).toBeNull();
    });
});