Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
95 changes: 94 additions & 1 deletion src/__test__/unit/sandbox-lifecycle.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ describe('deriveSandboxLifecycleFromEvents', () => {
expect(lifecycle.endedAt).toBeNull()
})

it('uses first created event and latest killed event', () => {
it('uses first created event and the last killed event', () => {
const events: SandboxEventDTO[] = [
createLifecycleEvent({
id: '1',
Expand Down Expand Up @@ -118,6 +118,99 @@ describe('deriveSandboxLifecycleFromEvents', () => {
expect(lifecycle.endedAt).toBe('2026-03-06T19:15:00.000Z')
})

it('uses the last paused or killed event to constrain the lifecycle', () => {
const events: SandboxEventDTO[] = [
createLifecycleEvent({
id: '1',
type: 'sandbox.lifecycle.created',
timestamp: '2026-03-06T19:00:00.000Z',
}),
createLifecycleEvent({
id: '2',
type: 'sandbox.lifecycle.paused',
timestamp: '2026-03-06T19:05:00.000Z',
}),
createLifecycleEvent({
id: '3',
type: 'sandbox.lifecycle.paused',
timestamp: '2026-03-06T19:06:00.000Z',
}),
createLifecycleEvent({
id: '4',
type: 'sandbox.lifecycle.killed',
timestamp: '2026-03-06T19:07:00.000Z',
}),
createLifecycleEvent({
id: '5',
type: 'sandbox.lifecycle.resumed',
timestamp: '2026-03-06T19:08:00.000Z',
}),
createLifecycleEvent({
id: '6',
type: 'sandbox.lifecycle.killed',
timestamp: '2026-03-06T19:09:00.000Z',
}),
]

const lifecycle = deriveSandboxLifecycleFromEvents(events)

expect(lifecycle.createdAt).toBe('2026-03-06T19:00:00.000Z')
expect(lifecycle.pausedAt).toBeNull()
expect(lifecycle.endedAt).toBe('2026-03-06T19:09:00.000Z')
})

it('does not constrain when the last event is resumed', () => {
const events: SandboxEventDTO[] = [
createLifecycleEvent({
id: '1',
type: 'sandbox.lifecycle.created',
timestamp: '2026-03-06T19:00:00.000Z',
}),
createLifecycleEvent({
id: '2',
type: 'sandbox.lifecycle.paused',
timestamp: '2026-03-06T19:05:00.000Z',
}),
createLifecycleEvent({
id: '3',
type: 'sandbox.lifecycle.resumed',
timestamp: '2026-03-06T19:06:00.000Z',
}),
]

const lifecycle = deriveSandboxLifecycleFromEvents(events)

expect(lifecycle.createdAt).toBe('2026-03-06T19:00:00.000Z')
expect(lifecycle.pausedAt).toBeNull()
expect(lifecycle.endedAt).toBeNull()
})

it('does not constrain when the last event is updated', () => {
const events: SandboxEventDTO[] = [
createLifecycleEvent({
id: '1',
type: 'sandbox.lifecycle.created',
timestamp: '2026-03-06T19:00:00.000Z',
}),
createLifecycleEvent({
id: '2',
type: 'sandbox.lifecycle.paused',
timestamp: '2026-03-06T19:05:00.000Z',
}),
createLifecycleEvent({
id: '3',
type: 'sandbox.lifecycle.updated',
timestamp: '2026-03-06T19:06:00.000Z',
}),
]

const lifecycle = deriveSandboxLifecycleFromEvents(events)

expect(lifecycle.createdAt).toBe('2026-03-06T19:00:00.000Z')
expect(lifecycle.pausedAt).toBeNull()
expect(lifecycle.endedAt).toBeNull()
})

it('ignores non-lifecycle events', () => {
const events: SandboxEventDTO[] = [
createLifecycleEvent({
Expand Down
183 changes: 183 additions & 0 deletions src/__test__/unit/sandbox-monitoring-chart-model.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,189 @@ describe('buildMonitoringChartModel', () => {
])
})

it('treats killed periods as inactive gaps until the next resume', () => {
const metrics: SandboxMetric[] = [
{
...baseMetric,
timestampUnix: 10,
cpuUsedPct: 10,
memUsed: 100,
diskUsed: 200,
},
{
...baseMetric,
timestampUnix: 50,
cpuUsedPct: 50,
memUsed: 500,
diskUsed: 1_000,
},
]

const lifecycleEvents: SandboxEventDTO[] = [
createLifecycleEvent({
id: 'pause-1',
type: 'sandbox.lifecycle.paused',
timestamp: '1970-01-01T00:00:20.000Z',
}),
createLifecycleEvent({
id: 'kill-1',
type: 'sandbox.lifecycle.killed',
timestamp: '1970-01-01T00:00:40.000Z',
}),
createLifecycleEvent({
id: 'kill-2',
type: 'sandbox.lifecycle.killed',
timestamp: '1970-01-01T00:00:45.000Z',
}),
createLifecycleEvent({
id: 'resume',
type: 'sandbox.lifecycle.resumed',
timestamp: '1970-01-01T00:00:55.000Z',
}),
]

const result = buildMonitoringChartModel({
metrics,
lifecycleEvents,
startMs: 0,
endMs: 60_000,
})

expect(result.resourceSeries[0]?.data).toEqual([
[10_000, 10, null],
[50_000, null, null],
])
expect(result.resourceSeries[0]?.connectors).toEqual([
{
from: [10_000, 10],
to: [20_000, 10],
},
])
})

it('draws a synthetic dashed connector across an active lifecycle window when no metrics were collected', () => {
const lifecycleEvents: SandboxEventDTO[] = [
createLifecycleEvent({
id: 'created',
type: 'sandbox.lifecycle.created',
timestamp: '1970-01-01T00:00:01.000Z',
}),
createLifecycleEvent({
id: 'paused',
type: 'sandbox.lifecycle.paused',
timestamp: '1970-01-01T00:00:04.000Z',
}),
createLifecycleEvent({
id: 'resumed',
type: 'sandbox.lifecycle.resumed',
timestamp: '1970-01-01T00:00:08.000Z',
}),
]

const result = buildMonitoringChartModel({
metrics: [
{
...baseMetric,
timestampUnix: 10,
cpuUsedPct: 50,
memUsed: 500,
diskUsed: 1_000,
},
],
lifecycleEvents,
startMs: 0,
endMs: 12_000,
})

expect(result.resourceSeries[0]?.data).toEqual([[10_000, 50, null]])
expect(result.resourceSeries[0]?.connectors).toEqual([
{
from: [8_000, 50],
to: [10_000, 50],
},
{
from: [1_000, 0],
to: [4_000, 0],
isSynthetic: true,
},
])
})

it('draws a dashed connector from created to the first metric when the range starts at created', () => {
const lifecycleEvents: SandboxEventDTO[] = [
createLifecycleEvent({
id: 'created',
type: 'sandbox.lifecycle.created',
timestamp: '1970-01-01T00:00:01.000Z',
}),
]

const result = buildMonitoringChartModel({
metrics: [
{
...baseMetric,
timestampUnix: 10,
cpuUsedPct: 50,
memUsed: 500,
diskUsed: 1_000,
},
],
lifecycleEvents,
startMs: 1_000,
endMs: 12_000,
})

expect(result.resourceSeries[0]?.data).toEqual([[10_000, 50, null]])
expect(result.resourceSeries[0]?.connectors).toEqual([
{
from: [1_000, 50],
to: [10_000, 50],
},
])
})

it('draws a dashed connector from created to the first metric when the range starts before created', () => {
const lifecycleEvents: SandboxEventDTO[] = [
createLifecycleEvent({
id: 'created',
type: 'sandbox.lifecycle.created',
timestamp: '1970-01-01T00:00:01.000Z',
}),
createLifecycleEvent({
id: 'killed',
type: 'sandbox.lifecycle.killed',
timestamp: '1970-01-01T00:00:20.000Z',
}),
]

const result = buildMonitoringChartModel({
metrics: [
{
...baseMetric,
timestampUnix: 10,
cpuUsedPct: 50,
memUsed: 500,
diskUsed: 1_000,
},
],
lifecycleEvents,
startMs: 0,
endMs: 22_000,
})

expect(result.resourceSeries[0]?.data).toEqual([[10_000, 50, null]])
expect(result.resourceSeries[0]?.connectors).toEqual([
{
from: [10_000, 50],
to: [20_000, 50],
},
{
from: [1_000, 50],
to: [10_000, 50],
},
])
})

it('builds visible lifecycle event markers for created, paused, resumed, and killed only', () => {
const lifecycleEvents: SandboxEventDTO[] = [
createLifecycleEvent({
Expand Down
Loading
Loading