Skip to content

Stand up unit tests for connector-directline #17

Description

@AimTune

packages/connector-directline has zero unit tests today. After the read-tick semantics fix (commit 7a305d0) we rely on manual sandbox verification, which means a future regression won't be caught by CI.

Why this is needed

The fix in 7a305d0 is non-trivial:

  • sendMessage emits "sent" synchronously before postActivity resolves.
  • The activity stream's echo branch silently consumes nothing — does not touch pendingIds.
  • flushPendingToRead() only fires on actual bot message activities (not typing, not events, not the user's own echo).

A naive refactor would easily reintroduce the original "echo → instant double-tick" bug.

Approach

The blocker is mocking botframework-directlinejs. The connector imports DirectLine, ConnectionStatus, and an Activity type from there. The real SDK opens a WebSocket on connect() so it can't run in jsdom directly.

Sketch:

// __tests__/_directLineMock.ts
import { Subject } from "rxjs"; // or hand-rolled minimal Observable

export class FakeDirectLine {
  connectionStatus$ = new Subject<number>();
  activity$ = new Subject<Activity>();
  postActivity = vi.fn(() => of({}));
  end = vi.fn();
}

vi.mock("botframework-directlinejs", () => ({
  DirectLine: vi.fn().mockImplementation(() => new FakeDirectLine()),
  ConnectionStatus: { Online: 2, ExpiredToken: 4, FailedToConnect: 5, Ended: 5 },
}));

Then write tests that:

  • Drive activity$.next(...) to simulate echo / typing / message arrivals.
  • Assert messageStatusHandler calls in order via vi.invocationCallOrder.

Tests to add

  • sendMessage emits "sent" synchronously (before postActivity resolves).
  • WebSocket echo of user's own message does not trigger messageStatusHandler.
  • First bot message activity flushes every still-pending user id to "read".
  • Bot typing activity does not flush — pending stays at "sent".
  • Bot event activity (with name) does not flush.
  • Send → echo → typing → message keeps "read" order; never downgrades.
  • Multiple pending sends: a single bot message flushes all of them at once.

Acceptance criteria

  • Tests live in packages/connector-directline/src/__tests__/ and run with pnpm -F @chativa/connector-directline test.
  • Tests run in jsdom (no real network).
  • Coverage ≥ 70% for the file.

Spun out of todos.md.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or requesthelp wantedExtra attention is needed

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions