Skip to content

DeepTutor: Lifelong Personalized Tutoring. https://deeptutor.info/.

4.5 KB
Raw Blame
import { readingSessionIdFromPath } from "../lib/learning-routes";
import assert from "node:assert/strict";
import test from "node:test";

import {
  masteryPathIdOf,
  readingWorkspaceIdOf,
  sessionRoute,
} from "../lib/mastery-session";
import type { SessionSummary } from "../lib/session-api";
import { workspaceActionNeedsConfiguration } from "../lib/workspace-mode";

function session(preferences: SessionSummary["preferences"]): SessionSummary {
  return {
    id: "session-1",
    session_id: "session-1",
    title: "Learning session",
    created_at: 1,
    updated_at: 1,
    message_count: 1,
    last_message: "hello",
    preferences,
  };
}

test("mastery ownership survives switching the per-turn action", () => {
  const item = session({
    workspace_mode: "mastery_path",
    mastery_path_id: "path-42",
    capability: "deep_research",
  });

  assert.equal(masteryPathIdOf(item), "path-42");
});

test("reading ownership survives switching the per-turn action", () => {
  const item = session({
    workspace_mode: "immersive_reading",
    reading_workspace_id: "reading-42",
    capability: "visualize",
  });

  assert.equal(readingWorkspaceIdOf(item), "reading-42");
});

test("capability and retired session-kind fields do not claim workspace ownership", () => {
  assert.equal(
    masteryPathIdOf(
      session({ capability: "mastery_path", mastery_path_id: "legacy-path" }),
    ),
    "",
  );
  assert.equal(
    readingWorkspaceIdOf(
      session({
        capability: "immersive_reading",
        session_kind: "immersive_reading",
        reading_workspace_id: "legacy-reading",
      }),
    ),
    "",
  );
});

test("unrelated sessions do not inherit stale learning ids", () => {
  assert.equal(
    masteryPathIdOf(
      session({ capability: "chat", mastery_path_id: "stale-path" }),
    ),
    "",
  );
  assert.equal(
    readingWorkspaceIdOf(
      session({ capability: "chat", reading_workspace_id: "stale-reading" }),
    ),
    "",
  );
});

test("configured workspace actions do not bypass their settings", () => {
  assert.equal(workspaceActionNeedsConfiguration("deep_question"), true);
  assert.equal(workspaceActionNeedsConfiguration("visualize"), true);
  assert.equal(workspaceActionNeedsConfiguration("deep_research"), true);
  assert.equal(workspaceActionNeedsConfiguration("deep_solve"), false);
  assert.equal(workspaceActionNeedsConfiguration(""), false);
});

/* ── Reading URLs, both directions ──────────────────────────────────────
   The workspace writes the first turn's session id into the address bar with
   the native history API, because moving between `/learning/reading/<ws>` and
   `/learning/reading/<ws>/sessions/<id>` through the router unmounts the whole reader
   mid-answer. That only holds together if reading the URL back agrees with
   writing it: were they to disagree, the first question of a conversation
   would land on a URL the workspace then reads as "start a new one". */

test("a reading conversation route round-trips through the path parser", () => {
  const route = sessionRoute(
    session({
      workspace_mode: "immersive_reading",
      reading_workspace_id: "rw_42",
    }),
  );

  assert.equal(route, "/learning/reading/rw_42/sessions/session-1?dt_workspace=");
  assert.equal(readingSessionIdFromPath(route), "session-1");
});

test("a bare collection URL means a new conversation, not a stored one", () => {
  assert.equal(readingSessionIdFromPath("/learning/reading/rw_42"), null);
  assert.equal(readingSessionIdFromPath("/learning/reading/rw_42/sessions"), null);
  assert.equal(readingSessionIdFromPath("/learning/reading/rw_42/sessions/"), null);
  assert.equal(readingSessionIdFromPath("/learning/reading"), null);
});

test("reading session ids survive URL encoding and trailing path noise", () => {
  assert.equal(
    readingSessionIdFromPath("/learning/reading/rw%2042/sessions/unified_1_a%2Fb"),
    "unified_1_a/b",
  );
  assert.equal(
    readingSessionIdFromPath("/learning/reading/rw_42/sessions/abc?tab=notes"),
    "abc",
  );
});

test("only reading URLs are read as reading conversations", () => {
  assert.equal(readingSessionIdFromPath("/chat/abc"), null);
  assert.equal(readingSessionIdFromPath("/learning/mastery/p1/sessions/abc"), null);
});

test("a note companion conversation opens back on its page with its thread", () => {
  const item = session({ note_document_id: "0c2d24985c4f" });
  assert.match(sessionRoute(item), /^\/co-writer\/0c2d24985c4f\?chat=session-1/);
});