tweak the code of getting last session, without splice events array

This commit is contained in:
Yanzhen Yu
2026-04-01 12:00:00 +08:00
committed by yz-yu
parent 19acba745a
commit ae71cf106a
4 changed files with 179 additions and 152 deletions

View File

@@ -5,115 +5,7 @@ import * as path from 'path';
import * as puppeteer from 'puppeteer';
import { expect } from 'chai';
import { Suite } from 'mocha';
import {
EventType,
eventWithTime,
IncrementalSource,
MouseInteractions,
} from '../src/types';
import { Replayer } from '../src';
import { launchPuppeteer } from './utils';
const now = Date.now();
const events: eventWithTime[] = [
{
type: EventType.DomContentLoaded,
data: {},
timestamp: now,
},
{
type: EventType.Load,
data: {},
timestamp: now + 1000,
},
{
type: EventType.Meta,
data: {
href: 'http://localhost',
width: 1000,
height: 800,
},
timestamp: now + 1000,
},
{
type: EventType.FullSnapshot,
data: {
node: {
type: 0,
childNodes: [
{
type: 2,
tagName: 'html',
attributes: {},
childNodes: [
{
type: 2,
tagName: 'head',
attributes: {},
childNodes: [],
id: 3,
},
{
type: 2,
tagName: 'body',
attributes: {},
childNodes: [],
id: 4,
},
],
id: 2,
},
],
id: 1,
},
initialOffset: {
top: 0,
left: 0,
},
},
timestamp: now + 1000,
},
{
type: EventType.IncrementalSnapshot,
data: {
source: IncrementalSource.MouseInteraction,
type: MouseInteractions.Click,
id: 1,
x: 0,
y: 0,
},
timestamp: now + 2000,
},
{
type: EventType.IncrementalSnapshot,
data: {
source: IncrementalSource.MouseInteraction,
type: MouseInteractions.Click,
id: 1,
x: 0,
y: 0,
},
timestamp: now + 3000,
},
{
type: EventType.IncrementalSnapshot,
data: {
source: IncrementalSource.MouseInteraction,
type: MouseInteractions.Click,
id: 1,
x: 0,
y: 0,
},
timestamp: now + 4000,
},
];
interface IWindow extends Window {
rrweb: {
Replayer: typeof Replayer;
};
}
import { launchPuppeteer, sampleEvents as events } from './utils';
interface ISuite extends Suite {
code: string;
@@ -121,7 +13,7 @@ interface ISuite extends Suite {
page: puppeteer.Page;
}
describe('replayer', function(this: ISuite) {
describe('replayer', function (this: ISuite) {
before(async () => {
this.browser = await launchPuppeteer();
@@ -136,7 +28,7 @@ describe('replayer', function(this: ISuite) {
await page.evaluate(`const events = ${JSON.stringify(events)}`);
this.page = page;
page.on('console', msg => console.log('PAGE LOG:', msg.text()));
page.on('console', (msg) => console.log('PAGE LOG:', msg.text()));
});
afterEach(async () => {
@@ -148,60 +40,62 @@ describe('replayer', function(this: ISuite) {
});
it('can get meta data', async () => {
const meta = await this.page.evaluate(() => {
const { Replayer } = ((window as unknown) as IWindow).rrweb;
const meta = await this.page.evaluate(`
const { Replayer } = rrweb;
const replayer = new Replayer(events);
return replayer.getMetaData();
});
replayer.getMetaData();
`);
expect(meta).to.deep.equal({
startTime: events[0].timestamp,
endTime: events[events.length - 1].timestamp,
totalTime: events[events.length - 1].timestamp - events[0].timestamp,
});
});
it('will start actions when play', async () => {
const actionLength = await this.page.evaluate(() => {
const { Replayer } = ((window as unknown) as IWindow).rrweb;
const actionLength = await this.page.evaluate(`
const { Replayer } = rrweb;
const replayer = new Replayer(events);
replayer.play();
return replayer['timer']['actions'].length;
});
replayer['timer']['actions'].length;
`);
expect(actionLength).to.equal(events.length);
});
it('will clean actions when pause', async () => {
const actionLength = await this.page.evaluate(() => {
const { Replayer } = ((window as unknown) as IWindow).rrweb;
const actionLength = await this.page.evaluate(`
const { Replayer } = rrweb;
const replayer = new Replayer(events);
replayer.play();
replayer.pause();
return replayer['timer']['actions'].length;
});
replayer['timer']['actions'].length;
`);
expect(actionLength).to.equal(0);
});
it('can play at any time offset', async () => {
const actionLength = await this.page.evaluate(() => {
const { Replayer } = ((window as unknown) as IWindow).rrweb;
const actionLength = await this.page.evaluate(`
const { Replayer } = rrweb;
const replayer = new Replayer(events);
replayer.play(1500);
return replayer['timer']['actions'].length;
});
replayer['timer']['actions'].length;
`);
expect(actionLength).to.equal(
events.filter(e => e.timestamp - events[0].timestamp >= 1500).length,
events.filter((e) => e.timestamp - events[0].timestamp >= 1500).length,
);
});
it('can resume at any time offset', async () => {
const actionLength = await this.page.evaluate(() => {
const { Replayer } = ((window as unknown) as IWindow).rrweb;
const actionLength = await this.page.evaluate(`
const { Replayer } = rrweb;
const replayer = new Replayer(events);
replayer.play(1500);
replayer.pause();
replayer.resume(1500);
return replayer['timer']['actions'].length;
});
replayer['timer']['actions'].length;
`);
expect(actionLength).to.equal(
events.filter(e => e.timestamp - events[0].timestamp >= 1500).length,
events.filter((e) => e.timestamp - events[0].timestamp >= 1500).length,
);
});
});