feat: manually mirror opencoze's code from bytedance

Change-Id: I09a73aadda978ad9511264a756b2ce51f5761adf
This commit is contained in:
fanlv
2025-07-20 17:36:12 +08:00
commit 890153324f
14811 changed files with 1923430 additions and 0 deletions

View File

@@ -0,0 +1,76 @@
/*
* Copyright 2025 coze-dev Authors
*
* 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 { expect, describe, test, vi } from 'vitest';
import { dataReporter } from '../src/reporter/data-reporter';
import { DataNamespace } from '../src/constants';
const global = vi.hoisted(() => ({
reportFn: vi.fn(),
}));
vi.stubGlobal('location', {
pathname:
'/space/7313840473481936940/knowledge/7327619796571734060/7347658103988715564',
});
vi.mock('../src/reporter/utils.ts', () => ({
reporterFun: global.reportFn,
}));
vi.mock('@coze-arch/logger', () => ({}));
describe('test report func', () => {
test('errorEvent', () => {
dataReporter.errorEvent(DataNamespace.KNOWLEDGE, {
error: {},
level: 'error',
eventName: 'test',
} as any);
expect(global.reportFn).toHaveBeenCalledWith({
event: {
error: {},
eventName: 'test',
level: 'error',
},
meta: {
documentId: '7347658103988715564',
knowledgeId: '7327619796571734060',
spaceId: '7313840473481936940',
},
namespace: 'knowledge',
type: 'error',
});
});
test('event', () => {
dataReporter.event(DataNamespace.KNOWLEDGE, {
eventName: 'test',
});
expect(global.reportFn).toHaveBeenCalledWith({
event: {
eventName: 'test',
},
meta: {
documentId: '7347658103988715564',
knowledgeId: '7327619796571734060',
spaceId: '7313840473481936940',
},
namespace: 'knowledge',
type: 'custom',
});
});
});

View File

@@ -0,0 +1,122 @@
/*
* Copyright 2025 coze-dev Authors
*
* 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.
*/
/* eslint-disable @typescript-eslint/naming-convention */
import React, { useState } from 'react';
import { expect, describe, test, vi } from 'vitest';
import { fireEvent, render, screen } from '@testing-library/react';
import { DataErrorBoundary } from '../src/components/error-boundary/error-boundary';
import { DataNamespace } from '../src';
class MyErrorBoundary extends React.Component<
{
children: React.ReactElement;
FallbackComponent: () => React.ReactElement;
onError: (...args: any[]) => void;
},
{ hasError: boolean }
> {
constructor(props) {
super(props);
this.state = {
hasError: false,
};
}
static getDerivedStateFromError(_error) {
// Update state so next render shows fallback UI.
return { hasError: true };
}
componentDidCatch(error, errorInfo) {
// Log error to an error reporting service
this.props.onError(error, errorInfo);
}
render() {
const { children, FallbackComponent } = this.props;
if (this.state.hasError) {
return <FallbackComponent />;
}
return children;
}
}
export function ErrorButton() {
const [clicked, setClicked] = useState(false);
if (clicked) {
throw new Error('test');
}
return <button onClick={() => setClicked(true)}>test</button>;
}
const globalFlag = vi.hoisted(() => ({
persistError: vi.fn(),
}));
vi.mock('@coze-arch/logger', () => ({
ErrorBoundary: props => <MyErrorBoundary {...props} />,
logger: {
persist: {
error: globalFlag.persistError,
},
},
}));
vi.mock('@coze-arch/i18n', () => ({
I18n: {
t: vi.fn().mockImplementation((title: string) => title),
},
}));
describe('test DataErrorBoundary', () => {
test('render children', async () => {
await render(
<DataErrorBoundary namespace={DataNamespace.KNOWLEDGE}>
<button>test</button>
</DataErrorBoundary>,
);
const testButton = await screen.queryByText('test');
expect(testButton).not.toBeNull();
});
test('on error callback', async () => {
await render(
<DataErrorBoundary namespace={DataNamespace.KNOWLEDGE}>
<ErrorButton />
</DataErrorBoundary>,
);
const testButton = await screen.queryByText('test');
expect(testButton).not.toBeNull();
await fireEvent.click(testButton);
expect(globalFlag.persistError).toBeCalled();
});
test('fallback render', async () => {
await render(
<DataErrorBoundary namespace={DataNamespace.KNOWLEDGE}>
<ErrorButton />
</DataErrorBoundary>,
);
const testButton = await screen.queryByText('test');
expect(testButton).not.toBeNull();
await fireEvent.click(testButton);
const fallbackComp = await screen.queryByText('data_error_title');
expect(fallbackComp).not.toBeNull();
});
});

View File

@@ -0,0 +1,95 @@
/*
* Copyright 2025 coze-dev Authors
*
* 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 { expect, describe, test, vi } from 'vitest';
import { reporterFun } from '../src/reporter/utils';
import { DataNamespace } from '../src/constants';
const global = vi.hoisted(() => ({
reporter: {
errorEvent: vi.fn(),
event: vi.fn(),
},
}));
vi.mock('@coze-arch/logger', () => ({
reporter: global.reporter,
}));
describe('reporter utils test', () => {
test('reporterFun test errorEvent', () => {
reporterFun({
event: {
error: {
name: 'test',
message: 'test',
},
meta: {
spaceId: '2333',
},
eventName: 'test',
level: 'error',
},
meta: {
documentId: '7347658103988715564',
knowledgeId: '7327619796571734060',
spaceId: '7313840473481936940',
},
type: 'error',
namespace: DataNamespace.KNOWLEDGE,
});
expect(global.reporter.errorEvent).toHaveBeenCalledWith({
error: {
message: 'test',
name: 'test',
},
eventName: 'test',
level: 'error',
meta: {
documentId: '7347658103988715564',
knowledgeId: '7327619796571734060',
spaceId: '2333',
},
namespace: 'knowledge',
});
expect(global.reporter.event).not.toBeCalled();
});
test('reporterFun test event', () => {
reporterFun({
event: {
eventName: 'test',
},
meta: {
documentId: '7347658103988715564',
knowledgeId: '7327619796571734060',
spaceId: '7313840473481936940',
},
type: 'custom',
namespace: DataNamespace.KNOWLEDGE,
});
expect(global.reporter.event).toHaveBeenCalledWith({
eventName: 'test',
meta: {
documentId: '7347658103988715564',
knowledgeId: '7327619796571734060',
spaceId: '7313840473481936940',
},
namespace: 'knowledge',
});
});
});