---
entity: caisson_node
workspace_id: ws_quickstart
workspace_slug: quickstart
node_id: nd_qs_spec_counter
node_type: SpecAndTests
status: verified
environment_version: node:22-ts:5.8
environment_lock_hash: 902c6c679663f6d8cd1f4a7a11f7c66b06f7ca39be1d1b5cf5cae8b9a5af2728
spec_hash: 5be2a4a3bd31320f9debf6f55e372b300a2b1ee2c2565fc9450a20f4782b13d4
---

# SpecAndTests: Sliding Window Counter (`nd_qs_spec_counter`)

Frozen Vitest acceptance spec for src/windowCounter.ts.

## Frozen Target Specification
```ts
// spec/target_statements/qs-window-counter.acceptance.test.ts
// Frozen SpecAndTests for nd_qs_window_counter:
// Implement src/windowCounter.ts exporting `countWithinWindow(timestampsMs: number[], nowMs: number, windowMs: number): number`.
// Rules:
//   1. Return the count of timestamps t satisfying `nowMs - windowMs < t && t <= nowMs`.
//   2. If `windowMs <= 0`, return 0.
//   3. Do not mutate the input array.

import { describe, expect, it } from 'vitest';
import { countWithinWindow } from '../src/windowCounter.js';

describe('Quickstart Micro-Task 2: Sliding Window Counter', () => {
  it('counts events strictly inside (nowMs - windowMs, nowMs] without mutating input', () => {
    const input = [1000, 4000, 5000, 9999, 10000, 10001];
    const snapshot = [...input];
    expect(countWithinWindow(input, 10000, 6000)).toBe(3); // 5000, 9999, 10000 (4000 is on exclusive lower boundary)
    expect(input).toEqual(snapshot);
  });

  it('returns 0 when windowMs <= 0 or input is empty', () => {
    expect(countWithinWindow([100, 200], 200, 0)).toBe(0);
    expect(countWithinWindow([100, 200], 200, -50)).toBe(0);
    expect(countWithinWindow([], 200, 1000)).toBe(0);
  });
});
```

## Verified Solution
```ts
// spec/target_statements/qs-window-counter.acceptance.test.ts
// Frozen SpecAndTests for nd_qs_window_counter:
// Implement src/windowCounter.ts exporting `countWithinWindow(timestampsMs: number[], nowMs: number, windowMs: number): number`.
// Rules:
//   1. Return the count of timestamps t satisfying `nowMs - windowMs < t && t <= nowMs`.
//   2. If `windowMs <= 0`, return 0.
//   3. Do not mutate the input array.

import { describe, expect, it } from 'vitest';
import { countWithinWindow } from '../src/windowCounter.js';

describe('Quickstart Micro-Task 2: Sliding Window Counter', () => {
  it('counts events strictly inside (nowMs - windowMs, nowMs] without mutating input', () => {
    const input = [1000, 4000, 5000, 9999, 10000, 10001];
    const snapshot = [...input];
    expect(countWithinWindow(input, 10000, 6000)).toBe(3); // 5000, 9999, 10000 (4000 is on exclusive lower boundary)
    expect(input).toEqual(snapshot);
  });

  it('returns 0 when windowMs <= 0 or input is empty', () => {
    expect(countWithinWindow([100, 200], 200, 0)).toBe(0);
    expect(countWithinWindow([100, 200], 200, -50)).toBe(0);
    expect(countWithinWindow([], 200, 1000)).toBe(0);
  });
});
```

## Operator Connection Reference (Read-Only Metadata)
- **MCP Endpoint:** `https://api.caisson.tools/mcp` (Streamable HTTP, Bearer token issued by workspace operator)
- **Workspace ID:** `ws_quickstart`
- **Verification Policy:** No local toolchain required: server-side verification handles compilation and replay.
- **Read-Only Discovery:** `https://caisson.tools/llms.txt`
