Skip to content

Commit

Permalink
test: update traceable execution tests
Browse files Browse the repository at this point in the history
test: update tests
  • Loading branch information
tabkram committed Nov 22, 2023
1 parent ed3c62f commit c51a210
Showing 1 changed file with 92 additions and 85 deletions.
177 changes: 92 additions & 85 deletions src/trace/traceableExecution.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { TraceableExecution } from "./traceableExecution";
import { NodeData } from "./trace.model";

describe("TraceableExecution", () => {
let traceableExecution: TraceableExecution;
Expand Down Expand Up @@ -49,91 +50,97 @@ describe("TraceableExecution", () => {

// Perform assertions on the finalTrace
expect(finalTrace?.length).toEqual(5);

expect(finalTrace?.filter((n) => n.group === "nodes")?.length).toEqual(3);
expect(finalTrace?.filter((n) => n.group === "edges")?.length).toEqual(2);
});

// it('should create a trace of fetching data simultaneously from two functions using Promise.all', async () => {
// async function fetchCurrentTemperature(city: string) {
// return Promise.resolve(`Current Temperature in ${city}: 25°C`);
// }
//
// async function fetchDailyForecast(city: string) {
// return Promise.resolve(`Daily Forecast in ${city}: Sunny`);
// }
//
// async function getWeatherInformation(city: string, trace?: NodeData) {
// const [temperature, forecast] = await Promise.all([
// (
// await traceableExecution.run(fetchCurrentTemperature, [city], {
// trace: { parent: trace?.id },
// config: { parallel: true, traceExecution: true }
// })
// )?.outputs,
// (
// await traceableExecution.run(fetchDailyForecast, [city], {
// trace: { parent: trace?.id },
// config: { parallel: true, traceExecution: true }
// })
// )?.outputs
// ]);
//
// return Promise.resolve(`Weather information: ${temperature}, ${forecast}`);
// }
//
// // Simulate some parallel execution
// await traceableExecution.run(getWeatherInformation, ['Paris']);
// // Retrieve the trace
// const finalTrace = traceableExecution.getTrace();
// // Perform assertions on the finalTrace
// expect(finalTrace).toMatchObject([
// {
// data: {
// id: expect.stringMatching(/^getWeatherInformation_.*$/),
// label: 'getWeatherInformation',
// parallel: false,
// abstract: false,
// createTime: expect.any(Date),
// inputs: ['Paris'],
// outputs: 'Weather information: Current Temperature in Paris: 25°C, Daily Forecast in Paris: Sunny',
// startTime: expect.any(Date),
// endTime: expect.any(Date),
// duration: expect.any(Number),
// updateTime: expect.any(Date)
// },
// group: 'nodes'
// },
// {
// data: {
// inputs: ['Paris'],
// outputs: 'Current Temperature in Paris: 25°C',
// startTime: expect.any(Date),
// endTime: expect.any(Date),
// duration: expect.any(Number),
// id: expect.stringMatching(/^fetchCurrentTemperature_.*$/),
// label: '1 - fetchCurrentTemperature',
// parent: expect.stringMatching(/^getWeatherInformation_.*$/),
// parallel: true,
// abstract: false,
// createTime: expect.any(Date)
// },
// group: 'nodes'
// },
// {
// data: {
// inputs: ['Paris'],
// outputs: 'Daily Forecast in Paris: Sunny',
// startTime: expect.any(Date),
// endTime: expect.any(Date),
// duration: expect.any(Number),
// id: expect.stringMatching(/^fetchDailyForecast_.*$/),
// label: '3 - fetchDailyForecast',
// parent: expect.stringMatching(/^getWeatherInformation_.*$/),
// parallel: true,
// abstract: false,
// createTime: expect.any(Date)
// },
// group: 'nodes'
// }
// ]);
// });
it("should create a trace of fetching data simultaneously from two functions using Promise.all", async () => {
async function fetchCurrentTemperature(city: string) {
return Promise.resolve(`Current Temperature in ${city}: 25°C`);
}

async function fetchDailyForecast(city: string) {
return Promise.resolve(`Daily Forecast in ${city}: Sunny`);
}

async function getWeatherInformation(city: string, trace?: NodeData) {
const [temperature, forecast] = await Promise.all([
(
await traceableExecution.run(fetchCurrentTemperature, [city], {
trace: { parent: trace?.id },
config: { parallel: true, traceExecution: true },
})
)?.outputs,
(
await traceableExecution.run(fetchDailyForecast, [city], {
trace: { parent: trace?.id },
config: { parallel: true, traceExecution: true },
})
)?.outputs,
]);

return Promise.resolve(
`Weather information: ${temperature}, ${forecast}`
);
}

// Simulate some parallel execution
await traceableExecution.run(getWeatherInformation, ["Paris"]);
// Retrieve the trace
const finalTrace = traceableExecution.getTrace();
expect(finalTrace?.filter((n) => n.group === "nodes")?.length).toEqual(3);
expect(finalTrace?.filter((n) => n.group === "edges")?.length).toEqual(0);
// Perform assertions on the finalTrace
expect(finalTrace).toMatchObject([
{
data: {
id: expect.stringMatching(/^getWeatherInformation_.*$/),
label: "getWeatherInformation",
parallel: false,
abstract: false,
createTime: expect.any(Date),
inputs: ["Paris"],
outputs:
"Weather information: Current Temperature in Paris: 25°C, Daily Forecast in Paris: Sunny",
startTime: expect.any(Date),
endTime: expect.any(Date),
duration: expect.any(Number),
updateTime: expect.any(Date),
},
group: "nodes",
},
{
data: {
inputs: ["Paris"],
outputs: "Current Temperature in Paris: 25°C",
startTime: expect.any(Date),
endTime: expect.any(Date),
duration: expect.any(Number),
id: expect.stringMatching(/^fetchCurrentTemperature_.*$/),
label: "1 - fetchCurrentTemperature",
parent: expect.stringMatching(/^getWeatherInformation_.*$/),
parallel: true,
abstract: false,
createTime: expect.any(Date),
},
group: "nodes",
},
{
data: {
inputs: ["Paris"],
outputs: "Daily Forecast in Paris: Sunny",
startTime: expect.any(Date),
endTime: expect.any(Date),
duration: expect.any(Number),
id: expect.stringMatching(/^fetchDailyForecast_.*$/),
label: "3 - fetchDailyForecast",
parent: expect.stringMatching(/^getWeatherInformation_.*$/),
parallel: true,
abstract: false,
createTime: expect.any(Date),
},
group: "nodes",
},
]);
});
});

1 comment on commit c51a210

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Coverage report

St.
Category Percentage Covered / Total
🔴 Statements 57.95% 113/195
🔴 Branches 49.23% 64/130
🟡 Functions 63.16% 36/57
🔴 Lines 58.73% 111/189

Test suite run success

11 tests passing in 3 suites.

Report generated by 🧪jest coverage report action from c51a210

Please sign in to comment.