diff --git a/packages/client/src/effect/api/api.ts b/packages/client/src/effect/api/api.ts index df7962cfef8..cfed7a61ef0 100644 --- a/packages/client/src/effect/api/api.ts +++ b/packages/client/src/effect/api/api.ts @@ -121,6 +121,7 @@ export type SessionListInput = { readonly order?: "asc" | "desc" | undefined readonly search?: string | undefined readonly parentID?: Session.ID | null | undefined + readonly archived?: boolean | undefined readonly directory?: AbsolutePath | undefined readonly project?: Project.ID | undefined readonly subpath?: RelativePath | undefined @@ -257,6 +258,7 @@ export type SessionUpdateInput = { readonly title?: string | undefined readonly metadata?: Session.Metadata | undefined readonly permissions?: Permission.Ruleset | undefined + readonly archived?: boolean | undefined } export type SessionUpdateOutput = void export type SessionUpdateOperation = (input: SessionUpdateInput) => Effect.Effect @@ -543,6 +545,28 @@ export type SessionLogOutput = | undefined readonly data: { readonly sessionID: Session.ID; readonly idle: number } } + | { + readonly id: Event.ID + readonly created: number + readonly metadata?: { readonly [x: string]: unknown } | undefined + readonly type: "session.archived" + readonly durable: { readonly aggregateID: string; readonly seq: Event.Seq; readonly version: Event.Version } + readonly location?: + | { readonly directory: AbsolutePath; readonly workspaceID?: Workspace.ID | undefined } + | undefined + readonly data: { readonly sessionID: Session.ID } + } + | { + readonly id: Event.ID + readonly created: number + readonly metadata?: { readonly [x: string]: unknown } | undefined + readonly type: "session.unarchived" + readonly durable: { readonly aggregateID: string; readonly seq: Event.Seq; readonly version: Event.Version } + readonly location?: + | { readonly directory: AbsolutePath; readonly workspaceID?: Workspace.ID | undefined } + | undefined + readonly data: { readonly sessionID: Session.ID } + } | { readonly id: Event.ID readonly created: number diff --git a/packages/client/src/effect/generated/client.ts b/packages/client/src/effect/generated/client.ts index 8fa86e433c7..bcff357c9da 100644 --- a/packages/client/src/effect/generated/client.ts +++ b/packages/client/src/effect/generated/client.ts @@ -383,6 +383,7 @@ const EndpointSessionList = (raw: RawClient["server.session"]) => (input?: Sessi order: input?.["order"], search: input?.["search"], parentID: input?.["parentID"], + archived: input?.["archived"], directory: input?.["directory"], project: input?.["project"], subpath: input?.["subpath"], @@ -491,7 +492,12 @@ const EndpointSessionUpdate = (raw: RawClient["server.session"]) => (input: Sess preserveEffect()( raw["session.update"]({ params: { sessionID: input["sessionID"] }, - payload: { title: input["title"], metadata: input["metadata"], permissions: input["permissions"] }, + payload: { + title: input["title"], + metadata: input["metadata"], + permissions: input["permissions"], + archived: input["archived"], + }, }).pipe(Effect.mapError(mapClientError)), ) diff --git a/packages/client/src/promise/generated/client.ts b/packages/client/src/promise/generated/client.ts index 9503bd30e76..ca3c2ad885c 100644 --- a/packages/client/src/promise/generated/client.ts +++ b/packages/client/src/promise/generated/client.ts @@ -550,6 +550,7 @@ export function make(options: ClientOptions) { order: input?.["order"], search: input?.["search"], parentID: input?.["parentID"], + archived: input?.["archived"], directory: input?.["directory"], project: input?.["project"], subpath: input?.["subpath"], @@ -698,7 +699,12 @@ export function make(options: ClientOptions) { { method: "PATCH", path: `/api/session/${encodeURIComponent(input.sessionID)}`, - body: { title: input["title"], metadata: input["metadata"], permissions: input["permissions"] }, + body: { + title: input["title"], + metadata: input["metadata"], + permissions: input["permissions"], + archived: input["archived"], + }, successStatus: 204, declaredStatuses: [400, 401, 404], empty: true, diff --git a/packages/client/src/promise/generated/types.ts b/packages/client/src/promise/generated/types.ts index 7e601e0dcec..5ca3751c108 100644 --- a/packages/client/src/promise/generated/types.ts +++ b/packages/client/src/promise/generated/types.ts @@ -1026,6 +1026,26 @@ export type SessionViewed = { data: { sessionID: SessionID; idle: number } } +export type SessionArchived = { + id: EventID + created: number + metadata?: { [x: string]: any } + type: "session.archived" + durable: { aggregateID: string; seq: EventSeq; version: 1 } + location?: LocationRef + data: { sessionID: SessionID } +} + +export type SessionUnarchived = { + id: EventID + created: number + metadata?: { [x: string]: any } + type: "session.unarchived" + durable: { aggregateID: string; seq: EventSeq; version: 1 } + location?: LocationRef + data: { sessionID: SessionID } +} + export type SessionDeleted = { id: EventID created: number @@ -2515,6 +2535,8 @@ export type SessionEventDurable = | SessionMetadataUpdated | SessionPermissions | SessionViewed + | SessionArchived + | SessionUnarchived | SessionDeleted | SessionForked | SessionInboxDelivered @@ -2578,6 +2600,8 @@ export type V2Event = | SessionMetadataUpdated | SessionPermissions | SessionViewed + | SessionArchived + | SessionUnarchived | SessionUsageUpdated | SessionDeleted | SessionForked @@ -2968,6 +2992,7 @@ export type SessionListInput = { readonly order?: "asc" | "desc" | undefined readonly search?: string | undefined readonly parentID?: string | null | undefined + readonly archived?: boolean | undefined readonly directory?: string | undefined readonly project?: string | undefined readonly subpath?: string | undefined @@ -2978,6 +3003,7 @@ export type SessionListInput = { readonly order?: "asc" | "desc" | undefined readonly search?: string | undefined readonly parentID?: string | null | undefined + readonly archived?: boolean | undefined readonly directory?: string | undefined readonly project?: string | undefined readonly subpath?: string | undefined @@ -2988,6 +3014,7 @@ export type SessionListInput = { readonly order?: "asc" | "desc" | undefined readonly search?: string | undefined readonly parentID?: string | null | undefined + readonly archived?: boolean | undefined readonly directory?: string | undefined readonly project?: string | undefined readonly subpath?: string | undefined @@ -2998,16 +3025,29 @@ export type SessionListInput = { readonly order?: "asc" | "desc" | undefined readonly search?: string | undefined readonly parentID?: string | null | undefined + readonly archived?: boolean | undefined readonly directory?: string | undefined readonly project?: string | undefined readonly subpath?: string | undefined readonly cursor?: string | undefined }["parentID"] + readonly archived?: { + readonly limit?: number | undefined + readonly order?: "asc" | "desc" | undefined + readonly search?: string | undefined + readonly parentID?: string | null | undefined + readonly archived?: boolean | undefined + readonly directory?: string | undefined + readonly project?: string | undefined + readonly subpath?: string | undefined + readonly cursor?: string | undefined + }["archived"] readonly directory?: { readonly limit?: number | undefined readonly order?: "asc" | "desc" | undefined readonly search?: string | undefined readonly parentID?: string | null | undefined + readonly archived?: boolean | undefined readonly directory?: string | undefined readonly project?: string | undefined readonly subpath?: string | undefined @@ -3018,6 +3058,7 @@ export type SessionListInput = { readonly order?: "asc" | "desc" | undefined readonly search?: string | undefined readonly parentID?: string | null | undefined + readonly archived?: boolean | undefined readonly directory?: string | undefined readonly project?: string | undefined readonly subpath?: string | undefined @@ -3028,6 +3069,7 @@ export type SessionListInput = { readonly order?: "asc" | "desc" | undefined readonly search?: string | undefined readonly parentID?: string | null | undefined + readonly archived?: boolean | undefined readonly directory?: string | undefined readonly project?: string | undefined readonly subpath?: string | undefined @@ -3038,6 +3080,7 @@ export type SessionListInput = { readonly order?: "asc" | "desc" | undefined readonly search?: string | undefined readonly parentID?: string | null | undefined + readonly archived?: boolean | undefined readonly directory?: string | undefined readonly project?: string | undefined readonly subpath?: string | undefined @@ -4286,6 +4329,7 @@ export type SessionUpdateInput = { readonly permissions?: | ReadonlyArray<{ readonly action: string; readonly resource: string; readonly effect: "allow" | "deny" | "ask" }> | undefined + readonly archived?: boolean | undefined }["title"] readonly metadata?: { readonly title?: string | undefined @@ -4293,6 +4337,7 @@ export type SessionUpdateInput = { readonly permissions?: | ReadonlyArray<{ readonly action: string; readonly resource: string; readonly effect: "allow" | "deny" | "ask" }> | undefined + readonly archived?: boolean | undefined }["metadata"] readonly permissions?: { readonly title?: string | undefined @@ -4300,7 +4345,16 @@ export type SessionUpdateInput = { readonly permissions?: | ReadonlyArray<{ readonly action: string; readonly resource: string; readonly effect: "allow" | "deny" | "ask" }> | undefined + readonly archived?: boolean | undefined }["permissions"] + readonly archived?: { + readonly title?: string | undefined + readonly metadata?: { readonly [x: string]: JsonValue } | undefined + readonly permissions?: + | ReadonlyArray<{ readonly action: string; readonly resource: string; readonly effect: "allow" | "deny" | "ask" }> + | undefined + readonly archived?: boolean | undefined + }["archived"] } export type SessionUpdateOutput = void diff --git a/packages/client/src/solid/data.ts b/packages/client/src/solid/data.ts index c5036a03f42..8d835fd1250 100644 --- a/packages/client/src/solid/data.ts +++ b/packages/client/src/solid/data.ts @@ -744,6 +744,14 @@ export function createData(config: CreateDataInput) { if (store.session.info[event.data.sessionID]) setStore("session", "info", event.data.sessionID, "permissions", event.data.permissions) return + case "session.archived": + if (store.session.info[event.data.sessionID]) + setStore("session", "info", event.data.sessionID, "time", "archived", event.created) + return + case "session.unarchived": + if (store.session.info[event.data.sessionID]) + setStore("session", "info", event.data.sessionID, "time", "archived", undefined) + return case "session.moved": { const current = store.session.info[event.data.sessionID] if (current) { diff --git a/packages/core/src/session.ts b/packages/core/src/session.ts index 0e24882df13..46a6eb0b8e3 100644 --- a/packages/core/src/session.ts +++ b/packages/core/src/session.ts @@ -183,6 +183,8 @@ export interface Interface { readonly switchAgent: (input: { sessionID: SessionSchema.ID; agent: Agent.ID }) => Effect.Effect readonly switchModel: (input: { sessionID: SessionSchema.ID; model: Model.Ref }) => Effect.Effect readonly rename: (input: { sessionID: SessionSchema.ID; title: string }) => Effect.Effect + readonly archive: (sessionID: SessionSchema.ID) => Effect.Effect + readonly unarchive: (sessionID: SessionSchema.ID) => Effect.Effect readonly setMetadata: (input: { sessionID: SessionSchema.ID metadata: SessionSchema.Metadata @@ -465,6 +467,8 @@ const layer = Layer.effect( switchAgent: (input) => sessions.forSession(input.sessionID).switchAgent(input), switchModel: (input) => sessions.forSession(input.sessionID).switchModel(input), rename: (input) => sessions.forSession(input.sessionID).rename(input), + archive: (sessionID) => sessions.forSession(sessionID).archive(), + unarchive: (sessionID) => sessions.forSession(sessionID).unarchive(), setMetadata: (input) => sessions.forSession(input.sessionID).setMetadata(input), setPermissions: (input) => sessions.forSession(input.sessionID).setPermissions(input), move: moves.move, diff --git a/packages/core/src/session/info.ts b/packages/core/src/session/info.ts index 1cab33c27aa..88ff26f5e42 100644 --- a/packages/core/src/session/info.ts +++ b/packages/core/src/session/info.ts @@ -58,7 +58,7 @@ export function fromRow(row: typeof SessionTable.$inferSelect): SessionSchema.In updated: DateTime.makeUnsafe(row.time_updated), idle: row.time_idle === null ? undefined : DateTime.makeUnsafe(row.time_idle), viewed: row.time_viewed === null ? undefined : DateTime.makeUnsafe(row.time_viewed), - archived: row.time_archived ? DateTime.makeUnsafe(row.time_archived) : undefined, + archived: row.time_archived === null ? undefined : DateTime.makeUnsafe(row.time_archived), }, }) } diff --git a/packages/core/src/session/message-updater.ts b/packages/core/src/session/message-updater.ts index 14143bbfa8e..743a6b8f024 100644 --- a/packages/core/src/session/message-updater.ts +++ b/packages/core/src/session/message-updater.ts @@ -81,6 +81,8 @@ export function update(adapter: Adapter, event: SessionEvent.DurableEvent) { Match.discriminatorsExhaustive("type")({ "session.created": () => Effect.void, "session.viewed": () => Effect.void, + "session.archived": () => Effect.void, + "session.unarchived": () => Effect.void, "session.message.content.updated": (event) => updateOwnedAssistant(event.data.messageID, (draft) => { draft.content = castDraft( diff --git a/packages/core/src/session/projector.ts b/packages/core/src/session/projector.ts index e007d6f4db8..65b3053c866 100644 --- a/packages/core/src/session/projector.ts +++ b/packages/core/src/session/projector.ts @@ -612,6 +612,22 @@ const layer = Layer.effectDiscard( .run() .pipe(Effect.orDie) }) + yield* bus.project(SessionEvent.Archived, (event) => + db + .update(SessionTable) + .set({ time_archived: event.created, time_updated: event.created }) + .where(eq(SessionTable.id, event.data.sessionID)) + .run() + .pipe(Effect.orDie), + ) + yield* bus.project(SessionEvent.Unarchived, (event) => + db + .update(SessionTable) + .set({ time_archived: null, time_updated: event.created }) + .where(eq(SessionTable.id, event.data.sessionID)) + .run() + .pipe(Effect.orDie), + ) yield* bus.project(SessionEvent.MessageContentUpdated, (event) => run(db, event)) yield* bus.project(SessionEvent.UsageRecorded, (event) => applyUsage(db, event.data.sessionID, event.data)) yield* bus.project(SessionEvent.Forked, (event) => projectFork(db, event)) diff --git a/packages/core/src/session/session.ts b/packages/core/src/session/session.ts index 3cd130086dd..0e44a456b4f 100644 --- a/packages/core/src/session/session.ts +++ b/packages/core/src/session/session.ts @@ -72,6 +72,16 @@ export const make = Effect.fn("Session.make")(function* () { yield* get(sessionID) yield* bus.publish(SessionEvent.Renamed, { sessionID, title: input.title }) }) + const archive = Effect.fn("Session.archive")(function* (sessionID: SessionSchema.ID) { + const session = yield* get(sessionID) + if (session.time.archived) return + yield* bus.publish(SessionEvent.Archived, { sessionID }) + }) + const unarchive = Effect.fn("Session.unarchive")(function* (sessionID: SessionSchema.ID) { + const session = yield* get(sessionID) + if (!session.time.archived) return + yield* bus.publish(SessionEvent.Unarchived, { sessionID }) + }) const setMetadata = Effect.fn("Session.setMetadata")(function* ( sessionID: SessionSchema.ID, input: { metadata: SessionSchema.Metadata }, @@ -348,6 +358,8 @@ export const make = Effect.fn("Session.make")(function* () { message, view, rename, + archive, + unarchive, setMetadata, setPermissions, switchAgent, @@ -372,6 +384,8 @@ export const make = Effect.fn("Session.make")(function* () { const message = operations.message.bind(undefined, sessionID) const view = operations.view.bind(undefined, sessionID) const rename = operations.rename.bind(undefined, sessionID) + const archive = operations.archive.bind(undefined, sessionID) + const unarchive = operations.unarchive.bind(undefined, sessionID) const setMetadata = operations.setMetadata.bind(undefined, sessionID) const setPermissions = operations.setPermissions.bind(undefined, sessionID) const switchAgent = operations.switchAgent.bind(undefined, sessionID) @@ -399,6 +413,8 @@ export const make = Effect.fn("Session.make")(function* () { message, view, rename, + archive, + unarchive, setMetadata, setPermissions, switchAgent, diff --git a/packages/core/src/session/store.ts b/packages/core/src/session/store.ts index 53bf9e585d0..d86cd6ab60c 100644 --- a/packages/core/src/session/store.ts +++ b/packages/core/src/session/store.ts @@ -20,6 +20,7 @@ const ListInputBase = { limit: PositiveInt.pipe(Schema.optional), order: Schema.Literals(["asc", "desc"]).pipe(Schema.optional), parentID: Schema.NullOr(Session.ID).pipe(Schema.optional), + archived: Schema.Boolean.pipe(Schema.optional), anchor: Session.ListAnchor.pipe(Schema.optional), } @@ -111,6 +112,8 @@ const layer = Layer.effect( conditions.push( input.parentID === null ? isNull(SessionTable.parent_id) : eq(SessionTable.parent_id, input.parentID), ) + if (input.archived !== undefined) + conditions.push(input.archived ? isNotNull(SessionTable.time_archived) : isNull(SessionTable.time_archived)) if (input.anchor) { conditions.push( order === "asc" diff --git a/packages/core/test/session-archive.test.ts b/packages/core/test/session-archive.test.ts new file mode 100644 index 00000000000..d20ca32a735 --- /dev/null +++ b/packages/core/test/session-archive.test.ts @@ -0,0 +1,81 @@ +import { describe, expect } from "bun:test" +import { Bus } from "@opencode/core/bus" +import { Database } from "@opencode/core/database/database" +import { AppNodeBuilder } from "@opencode/core/effect/app-node-builder" +import { EventTable } from "@opencode/core/event/sql" +import { Location } from "@opencode/core/location" +import { Project } from "@opencode/core/project" +import { AbsolutePath } from "@opencode/core/schema" +import { Session } from "@opencode/core/session" +import { SessionEvent } from "@opencode/core/session/event" +import { SessionExecution } from "@opencode/core/session/execution" +import { SessionProjector } from "@opencode/core/session/projector" +import { SessionStore } from "@opencode/core/session/store" +import { LayerNode } from "@opencode/util/effect/layer-node" +import { Effect } from "effect" +import { eq } from "drizzle-orm" +import { testEffect } from "./lib/effect" +import { globalProjectNode } from "./lib/project" + +const it = testEffect( + AppNodeBuilder.build( + LayerNode.group([Database.node, Bus.node, SessionProjector.node, SessionStore.node, Session.node]), + [ + Bus.node.replace(Bus.configured({ persist: true })), + Project.node.replace(globalProjectNode), + SessionExecution.node.replace(SessionExecution.noopLayer), + ], + ), +) +const location = Location.Ref.make({ directory: AbsolutePath.make("/project") }) + +describe("Session.archive", () => { + it.effect("archives and unarchives through one durable event each", () => + Effect.gen(function* () { + const session = yield* Session.Service + const { db } = yield* Database.Service + const created = yield* session.create({ location }) + expect(created.time.archived).toBeUndefined() + + yield* session.archive(created.id) + yield* session.archive(created.id) + expect((yield* session.get(created.id)).time.archived).toBeDefined() + + yield* session.unarchive(created.id) + yield* session.unarchive(created.id) + expect((yield* session.get(created.id)).time.archived).toBeUndefined() + + const types = (yield* db + .select({ type: EventTable.type }) + .from(EventTable) + .where(eq(EventTable.aggregate_id, created.id)) + .all()).map((event) => event.type) + expect(types.filter((type) => type === Bus.versionedType(SessionEvent.Archived.type, 1))).toHaveLength(1) + expect(types.filter((type) => type === Bus.versionedType(SessionEvent.Unarchived.type, 1))).toHaveLength(1) + }), + ) + + it.effect("filters listed sessions by archive state", () => + Effect.gen(function* () { + const session = yield* Session.Service + const active = yield* session.create({ location }) + const archived = yield* session.create({ location }) + yield* session.archive(archived.id) + + const ids = (input?: Session.ListInput) => + session.list(input).pipe(Effect.map((page) => page.data.map((item) => item.id).toSorted())) + expect(yield* ids()).toEqual([active.id, archived.id].toSorted()) + expect(yield* ids({ archived: false })).toEqual([active.id]) + expect(yield* ids({ archived: true })).toEqual([archived.id]) + }), + ) + + it.effect("rejects an unknown session", () => + Effect.gen(function* () { + const session = yield* Session.Service + const sessionID = Session.ID.make("ses_missing_archive") + expect(yield* Effect.flip(session.archive(sessionID))).toEqual(new Session.NotFoundError({ sessionID })) + expect(yield* Effect.flip(session.unarchive(sessionID))).toEqual(new Session.NotFoundError({ sessionID })) + }), + ) +}) diff --git a/packages/protocol/src/groups/session.ts b/packages/protocol/src/groups/session.ts index 44e3bddcf8e..d7f667a8093 100644 --- a/packages/protocol/src/groups/session.ts +++ b/packages/protocol/src/groups/session.ts @@ -286,6 +286,9 @@ export const makeSessionGroup = value === "true"), + encode: SchemaGetter.transform((value): "true" | "false" => (value ? "true" : "false")), + }), + ) + .annotate({ + description: + "Filter by archive state. Use true to return only archived sessions or false to exclude them. Omit to include both.", + }) + .pipe(Schema.optional), } export const ListFilter = Schema.Struct({ diff --git a/packages/schema/test/event-manifest.test.ts b/packages/schema/test/event-manifest.test.ts index def0e2a0507..2dc6a109505 100644 --- a/packages/schema/test/event-manifest.test.ts +++ b/packages/schema/test/event-manifest.test.ts @@ -111,6 +111,8 @@ describe("public event manifest", () => { "session.metadata.updated.1", "session.permissions.1", "session.viewed.1", + "session.archived.1", + "session.unarchived.1", "session.message.content.updated.1", "session.usage.recorded.1", "session.forked.2", diff --git a/packages/server/src/handlers/session.ts b/packages/server/src/handlers/session.ts index 3119adb48d9..0386bd44931 100644 --- a/packages/server/src/handlers/session.ts +++ b/packages/server/src/handlers/session.ts @@ -244,6 +244,10 @@ export const SessionHandler = HttpApiBuilder.group(Api, "server.session", (handl yield* session .setPermissions({ sessionID: ctx.params.sessionID, permissions: ctx.payload.permissions }) .pipe(Effect.catchTag("Session.NotFoundError", missingSession)) + if (ctx.payload.archived !== undefined) + yield* (ctx.payload.archived ? session.archive : session.unarchive)(ctx.params.sessionID).pipe( + Effect.catchTag("Session.NotFoundError", missingSession), + ) return HttpApiSchema.NoContent.make() }), ) diff --git a/packages/server/test/session-update.test.ts b/packages/server/test/session-update.test.ts index ed64f7611f9..0b27ded4429 100644 --- a/packages/server/test/session-update.test.ts +++ b/packages/server/test/session-update.test.ts @@ -5,28 +5,32 @@ import { it } from "../../core/test/lib/effect" import { ServerFetch } from "../src/fetch" const SessionResponse = Schema.Struct({ data: Schema.toEncoded(Session.Info) }) +const SessionsResponse = Schema.Struct({ data: Schema.Array(Schema.toEncoded(Session.Info)) }) + +const makeRequest = Effect.fn(function* () { + const handler = yield* ServerFetch.make({ + app: { version: "test" }, + database: { path: ":memory:" }, + fs: { filewatcher: false }, + models: { fetch: false }, + }) + return (path: string, method: string, body?: unknown) => + Effect.promise(async () => { + const response = await handler( + new Request(`http://opencode.local${path}`, { + method, + headers: { "content-type": "application/json" }, + body: body === undefined ? undefined : JSON.stringify(body), + }), + ) + expect(response.status).toBe(method === "PATCH" ? 204 : 200) + return response.status === 204 ? undefined : response.json() + }) +}) it.live("updates session metadata through PATCH", () => Effect.gen(function* () { - const handler = yield* ServerFetch.make({ - app: { version: "test" }, - database: { path: ":memory:" }, - fs: { filewatcher: false }, - models: { fetch: false }, - }) - const request = (path: string, method: string, body?: unknown) => - Effect.promise(async () => { - const response = await handler( - new Request(`http://opencode.local${path}`, { - method, - headers: { "content-type": "application/json" }, - body: body === undefined ? undefined : JSON.stringify(body), - }), - ) - expect(response.status).toBe(method === "PATCH" ? 204 : 200) - return response.status === 204 ? undefined : response.json() - }) - + const request = yield* makeRequest() const created = Schema.decodeUnknownSync(SessionResponse)( yield* request("/api/session", "POST", { metadata: { source: "create", stale: true } }), ) @@ -36,3 +40,27 @@ it.live("updates session metadata through PATCH", () => expect(updated.data.metadata).toEqual({ source: "patch" }) }).pipe(Effect.scoped), ) + +it.live("archives and unarchives a session through PATCH", () => + Effect.gen(function* () { + const request = yield* makeRequest() + const created = Schema.decodeUnknownSync(SessionResponse)(yield* request("/api/session", "POST", {})) + const get = () => + request(`/api/session/${created.data.id}`, "GET").pipe( + Effect.map((body) => Schema.decodeUnknownSync(SessionResponse)(body).data), + ) + const listed = (archived: boolean) => + request(`/api/session?archived=${archived}`, "GET").pipe( + Effect.map((body) => Schema.decodeUnknownSync(SessionsResponse)(body).data.map((item) => item.id)), + ) + + yield* request(`/api/session/${created.data.id}`, "PATCH", { archived: true }) + expect((yield* get()).time.archived).toBeNumber() + expect(yield* listed(true)).toContain(created.data.id) + expect(yield* listed(false)).not.toContain(created.data.id) + + yield* request(`/api/session/${created.data.id}`, "PATCH", { archived: false }) + expect((yield* get()).time.archived).toBeUndefined() + expect(yield* listed(false)).toContain(created.data.id) + }).pipe(Effect.scoped), +)