This repository has been archived on 2026-11-17. You can view files and clone it, but cannot push or open issues or pull requests.
gitgram-ai/aitimemachine/api-server/tests/integration/shared-checkpoints.test.ts
caelum0x 49b183818c Unify gitgram + aitimemachine into the monorepo (de-submodule) and merge vendored engines
Convert gitgram/ and aitimemachine/ from embedded git repos (which showed as
opaque submodule gitlinks on GitHub) into fully-tracked first-party content.

AITM now merges its 7 vendored repos as first-party engines:
- memory: beads (Go task DAG) + context7 (docs SDK), unified into saved_contexts
- git: vendored C git (bundled binary) + gitnexus (in-process code intelligence)
- agentic: a2a (@a2a-js/sdk) + pal (AITM-managed Python subprocess)

Includes the full session's product build: Gitgram web/mobile/backend features,
AITM website/CLI/MCP, the unified memory layer, engine build tooling
(scripts/build-engines.sh, Dockerfile, CI), docs (README, ENGINES.md, ARCHITECTURE.md),
and extensive test suites.

Build artifacts, node_modules, external/, and gemini-cli are gitignored.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-06-23 14:14:09 +03:00

281 lines
8.9 KiB
TypeScript

/**
* Integration Tests: Shared Checkpoints Routes
*
* Tests the /v1/shared-checkpoints endpoints:
* - GET /v1/shared-checkpoints - List shared checkpoints
* - GET /v1/shared-checkpoints/:id - Get specific shared checkpoint
* - POST /v1/shared-checkpoints/:id/share - Share a checkpoint with an org
* - DELETE /v1/shared-checkpoints/:id/unshare - Unshare a checkpoint
* - GET /v1/shared-checkpoints/org/:orgId - List org checkpoints
*/
import { describe, it, expect, beforeAll, beforeEach } from "vitest";
import request from "supertest";
import type { Express } from "express";
import {
seedTestData,
createTestApp,
TEST_API_KEY,
mockCheckpoints,
mockOrgMembers,
mockOrganizations,
} from "../setup.js";
let app: Express;
beforeAll(async () => {
app = await createTestApp();
});
beforeEach(() => {
seedTestData();
});
/**
* Seed an organization membership for the test user so shared-checkpoint
* queries can resolve org-scoped data.
*/
function seedOrgMembership() {
mockOrganizations.push({
id: "org_test_001",
name: "Test Org",
slug: "test-org",
ownerId: "user_test_001",
plan: "team",
createdAt: new Date(),
updatedAt: new Date(),
} as any);
mockOrgMembers.push({
id: "mem_test_001",
orgId: "org_test_001",
userId: "user_test_001",
role: "owner",
invitedBy: null,
joinedAt: new Date(),
} as any);
}
/**
* Seed a checkpoint owned by the test user with an orgId so it is "shared".
*/
function seedSharedCheckpoint() {
mockCheckpoints.push({
id: "cp_shared_001",
projectId: "proj_test_001",
userId: "user_test_001",
orgId: "org_test_001",
name: "Shared Checkpoint",
description: "A checkpoint shared with the org",
triggerType: "manual",
rootTreeHash: null,
createdAt: new Date(),
updatedAt: new Date(),
metadata: {},
} as any);
}
/**
* Seed an unshared checkpoint owned by the test user.
*/
function seedOwnedCheckpoint() {
mockCheckpoints.push({
id: "cp_owned_001",
projectId: "proj_test_001",
userId: "user_test_001",
orgId: null,
name: "Owned Checkpoint",
description: "A private checkpoint",
triggerType: "manual",
rootTreeHash: null,
createdAt: new Date(),
updatedAt: new Date(),
metadata: {},
} as any);
}
describe("Shared Checkpoints Routes - /v1/shared-checkpoints", () => {
// ========================================================================
// GET /v1/shared-checkpoints - List shared checkpoints
// ========================================================================
describe("GET /v1/shared-checkpoints", () => {
it("should require authentication", async () => {
const res = await request(app).get("/v1/shared-checkpoints");
expect(res.status).toBe(401);
});
it("should return empty when user has no org memberships", async () => {
const res = await request(app)
.get("/v1/shared-checkpoints")
.set("X-API-Key", TEST_API_KEY);
expect(res.status).toBe(200);
expect(res.body).toHaveProperty("checkpoints");
expect(res.body.checkpoints).toHaveLength(0);
expect(res.body).toHaveProperty("message");
});
it("should return shared checkpoints when user is in an org", async () => {
seedOrgMembership();
seedSharedCheckpoint();
const res = await request(app)
.get("/v1/shared-checkpoints")
.set("X-API-Key", TEST_API_KEY);
expect(res.status).toBe(200);
expect(res.body).toHaveProperty("checkpoints");
expect(res.body).toHaveProperty("total");
expect(res.body).toHaveProperty("organizations");
expect(res.body.organizations).toContain("org_test_001");
});
});
// ========================================================================
// GET /v1/shared-checkpoints/:id - Get specific shared checkpoint
// ========================================================================
describe("GET /v1/shared-checkpoints/:id", () => {
it("should require authentication", async () => {
const res = await request(app).get("/v1/shared-checkpoints/cp_shared_001");
expect(res.status).toBe(401);
});
it("should return 404 for a non-existent checkpoint", async () => {
const res = await request(app)
.get("/v1/shared-checkpoints/cp_nonexistent")
.set("X-API-Key", TEST_API_KEY);
expect(res.status).toBe(404);
});
it("should return checkpoint details for an owned checkpoint", async () => {
seedOwnedCheckpoint();
const res = await request(app)
.get("/v1/shared-checkpoints/cp_owned_001")
.set("X-API-Key", TEST_API_KEY);
expect(res.status).toBe(200);
expect(res.body).toHaveProperty("checkpoint");
expect(res.body.checkpoint).toHaveProperty("id", "cp_owned_001");
expect(res.body.checkpoint).toHaveProperty("is_own", true);
});
});
// ========================================================================
// POST /v1/shared-checkpoints/:id/share - Share checkpoint with org
// ========================================================================
describe("POST /v1/shared-checkpoints/:id/share", () => {
it("should require authentication", async () => {
const res = await request(app)
.post("/v1/shared-checkpoints/cp_owned_001/share")
.send({ org_id: "org_test_001" });
expect(res.status).toBe(401);
});
it("should return 400 when org_id is missing", async () => {
seedOwnedCheckpoint();
const res = await request(app)
.post("/v1/shared-checkpoints/cp_owned_001/share")
.set("X-API-Key", TEST_API_KEY)
.send({});
expect(res.status).toBe(400);
expect(res.body).toHaveProperty("error");
});
it("should return 404 when checkpoint does not exist", async () => {
const res = await request(app)
.post("/v1/shared-checkpoints/cp_nonexistent/share")
.set("X-API-Key", TEST_API_KEY)
.send({ org_id: "org_test_001" });
expect(res.status).toBe(404);
});
it("should share a checkpoint with an org", async () => {
seedOrgMembership();
seedOwnedCheckpoint();
const res = await request(app)
.post("/v1/shared-checkpoints/cp_owned_001/share")
.set("X-API-Key", TEST_API_KEY)
.send({ org_id: "org_test_001" });
expect(res.status).toBe(200);
expect(res.body).toHaveProperty("shared", true);
expect(res.body).toHaveProperty("checkpoint_id", "cp_owned_001");
expect(res.body).toHaveProperty("org_id", "org_test_001");
});
});
// ========================================================================
// DELETE /v1/shared-checkpoints/:id/unshare - Unshare checkpoint
// ========================================================================
describe("DELETE /v1/shared-checkpoints/:id/unshare", () => {
it("should require authentication", async () => {
const res = await request(app)
.delete("/v1/shared-checkpoints/cp_shared_001/unshare");
expect(res.status).toBe(401);
});
it("should return 404 when checkpoint does not exist", async () => {
const res = await request(app)
.delete("/v1/shared-checkpoints/cp_nonexistent/unshare")
.set("X-API-Key", TEST_API_KEY);
expect(res.status).toBe(404);
});
it("should unshare a shared checkpoint", async () => {
seedOrgMembership();
seedSharedCheckpoint();
const res = await request(app)
.delete("/v1/shared-checkpoints/cp_shared_001/unshare")
.set("X-API-Key", TEST_API_KEY);
expect(res.status).toBe(200);
expect(res.body).toHaveProperty("unshared", true);
expect(res.body).toHaveProperty("checkpoint_id", "cp_shared_001");
});
});
// ========================================================================
// GET /v1/shared-checkpoints/org/:orgId - List org checkpoints
// ========================================================================
describe("GET /v1/shared-checkpoints/org/:orgId", () => {
it("should require authentication", async () => {
const res = await request(app).get("/v1/shared-checkpoints/org/org_test_001");
expect(res.status).toBe(401);
});
it("should return 403 when user is not a member of the org", async () => {
const res = await request(app)
.get("/v1/shared-checkpoints/org/org_nonexistent")
.set("X-API-Key", TEST_API_KEY);
expect(res.status).toBe(403);
});
it("should return org checkpoints when user is a member", async () => {
seedOrgMembership();
seedSharedCheckpoint();
const res = await request(app)
.get("/v1/shared-checkpoints/org/org_test_001")
.set("X-API-Key", TEST_API_KEY);
expect(res.status).toBe(200);
expect(res.body).toHaveProperty("checkpoints");
expect(res.body).toHaveProperty("org_id", "org_test_001");
expect(res.body).toHaveProperty("total");
});
});
});