diff --git a/changes/feat-account-remove.md b/changes/feat-account-remove.md new file mode 100644 index 0000000..63c4de1 --- /dev/null +++ b/changes/feat-account-remove.md @@ -0,0 +1,7 @@ +### Added + +- **`notion account remove` signs a profile out and forgets it on this computer.** It forgets the Notion + token, the profile's settings, its chosen pages and its sync checkpoint. `--keep-data` leaves the copied pages in the + store, where search still finds them; `--delete-data` deletes them. It asks first, or takes `--yes`; `--dry-run` lists + what it would remove. Why: WireCat Desktop removes an account with this command, and notion could only sign out with + `session end`. The token itself stays valid at Notion: revoke it on Notion's integrations page. diff --git a/contracts/wirecat.json b/contracts/wirecat.json index 89cd13b..d0b8097 100644 --- a/contracts/wirecat.json +++ b/contracts/wirecat.json @@ -71,6 +71,19 @@ "local": "read" } }, + { + "path": [ + "account", + "remove" + ], + "summary": "sign this profile out and forget it, keeping or deleting its data", + "effects": { + "remote": "none", + "local": "write" + }, + "permission": "account.remove", + "entity": "account_removal" + }, { "path": [ "folders", diff --git a/docs/commands.md b/docs/commands.md index 52f5f6c..4f20476 100644 --- a/docs/commands.md +++ b/docs/commands.md @@ -107,6 +107,30 @@ notion session list **Example:** `notion session list`, `notion session list --json` +## `notion account` + +this profile on this computer + +### `notion account remove` + +sign the profile out, then forget it on this computer: its settings, its saved sign-in and its files; --keep-data leaves its messages in the store, where search still finds them, --delete-data deletes them; asks first, or --yes + +**Changes something on this computer only.** + +```sh +notion account remove [options] +``` + +| Option | What it does | +|---|---| +| `--keep-data` | leave what the store holds of this account, still searchable. | +| `--delete-data` | delete what the store and the tool's own files hold of this account. | +| `--dry-run` | list what it would remove and keep, and change nothing. | + +**Returns:** account removal — profile, data, dry_run, account, revoked, config_file, removed, kept; every field: notion commands info account remove + +**Example:** `notion personal account remove --keep-data --dry-run`, `notion personal account remove --delete-data --yes` + ## `notion folders` choose the Notion pages and databases sync copies; none chosen copies everything the token sees diff --git a/docs/dev/test-matrix.md b/docs/dev/test-matrix.md index 114000f..120ee5e 100644 --- a/docs/dev/test-matrix.md +++ b/docs/dev/test-matrix.md @@ -7,7 +7,7 @@ ran it · ⛔ not tested offline, with the reason and where it is checked instea Measured from the test run (`coverage/argv.jsonl`), not searched for — see [TESTING.md](TESTING.md) for how, and for the states and failures that cut across commands. -**191 ✅ · 206 ⛔ · 0 ❌** — 149 commands, 248 options. +**195 ✅ · 206 ⛔ · 0 ❌** — 150 commands, 251 options. | Command | Option | | Note | |---|---|---|---| @@ -19,6 +19,10 @@ Measured from the test run (`coverage/argv.jsonl`), not searched for — see | `session status` | `--check` | ✅ | | | `session end` | | ✅ | | | `session list` | | ✅ | | +| `account remove` | | ✅ | | +| `account remove` | `--keep-data` | ✅ | | +| `account remove` | `--delete-data` | ✅ | | +| `account remove` | `--dry-run` | ✅ | | | `folders add` | | ✅ | | | `folders list` | | ✅ | | | `folders browse` | | ✅ | | diff --git a/src/commands/account.ts b/src/commands/account.ts new file mode 100644 index 0000000..fbcdb81 --- /dev/null +++ b/src/commands/account.ts @@ -0,0 +1,35 @@ +import { annotate } from "@wirecat/cli-base/commands" +import { + type AccountRemoval, + accountRemoveCommand, + LOCAL_READ, + LOCAL_WRITE, + ProfileTokenStore, +} from "@wirecat/cli-core/cli" +import { Command } from "commander" +import { configuration } from "../config.js" +import { IDENTITY } from "../identity.js" +import { PROVIDER, profileDir } from "../library.js" + +const removal: AccountRemoval = { + // A personal access token is revoked only on Notion's own integrations page, so nothing is revoked here. + signOut: async (profile, { env, keyring }) => { + const gone = new ProfileTokenStore({ app: IDENTITY, env, ...(keyring ? { keyring } : {}) }).remove(profile) + return { revoked: null, removed: gone.length ? [`the Notion token of ${profile}`] : [] } + }, + // The chosen folders, the sync's checkpoint and its page snapshots; the store's copy is kept or deleted by the flag. + dataFiles: (profile, { env }) => [profileDir(env, profile)], +} + +export const accountCommand = (): Command => + annotate(new Command("account"), { effects: LOCAL_READ }) + .description("this profile on this computer") + .addCommand( + annotate( + accountRemoveCommand(IDENTITY, removal, { + provider: PROVIDER.name, + resolveSettings: configuration.resolveSettings, + }), + { effects: LOCAL_WRITE }, + ), + ) diff --git a/src/library.test.ts b/src/library.test.ts index 409dff2..d98025b 100644 --- a/src/library.test.ts +++ b/src/library.test.ts @@ -910,3 +910,47 @@ describe("every shared command over one synced copy", () => { await ok(["identities", "list", ROADMAP]) }) }) + +describe("account remove", () => { + const documents = async (env: NodeJS.ProcessEnv) => { + const store = await openStore(env, false) + try { + return (await searchNotesQuery(store, { text: "date>2000-01-01", limit: 50, timezone: "UTC" })).items.length + } finally { + await store.close() + } + } + + it("forgets the token and keeps the copy with --keep-data, deletes it and the profile's folder with --delete-data", async () => { + const dir = mkdtempSync(join(tmpdir(), "notion-remove-")) + const env = { MESSAGING_STORE: join(dir, "wirecat.db"), WCNOTION_STATE_DIR: join(dir, "state") } + const notion = fakeNotion({ workspace: workspace() }) + const keyring = memoryKeyring() + const run = (argv: string[], stdin?: string) => + runCli(["personal", ...argv, "--json"], { notion, keyring, env, ...(stdin ? { stdin } : {}) }) + const token = () => keyring.get(IDENTITY.appName, "token:personal") + + expect((await run(["session", "start"], FAKE_TOKEN)).code).toBe(0) + expect((await run(["sync"])).code).toBe(0) + const synced = await documents(env) + expect(synced).toBeGreaterThan(0) + + const dry = await run(["account", "remove", "--delete-data", "--dry-run"]) + expect(dry.code, dry.err).toBe(0) + expect(token()).toBe(FAKE_TOKEN) + expect(await documents(env)).toBe(synced) + + const kept = await run(["account", "remove", "--keep-data", "--yes"]) + expect(kept.code, kept.err).toBe(0) + expect(kept.json().item).toMatchObject({ data: "kept", revoked: null }) + expect(token()).toBeNull() + expect(await documents(env)).toBe(synced) + + expect((await run(["session", "start"], FAKE_TOKEN)).code).toBe(0) + const deleted = await run(["account", "remove", "--delete-data", "--yes"]) + expect(deleted.code, deleted.err).toBe(0) + expect(token()).toBeNull() + expect(await documents(env)).toBe(0) + expect(existsSync(join(dir, "state", "profiles", "personal"))).toBe(false) + }) +}) diff --git a/src/library.ts b/src/library.ts index 55bdb5d..acee0bb 100644 --- a/src/library.ts +++ b/src/library.ts @@ -54,7 +54,7 @@ export const withStore = async ( } } -const profileDir = (env: NodeJS.ProcessEnv, profile: string) => +export const profileDir = (env: NodeJS.ProcessEnv, profile: string) => join(resolvePaths({ appName: IDENTITY.appName, prefix: IDENTITY.envPrefix, env }).state, "profiles", profile) const readJson = (path: string): T | undefined => { diff --git a/src/program.ts b/src/program.ts index c79d1cc..81a0142 100644 --- a/src/program.ts +++ b/src/program.ts @@ -19,6 +19,7 @@ import { } from "@wirecat/cli-core/cli" import { appUpdate } from "@wirecat/cli-core/update" import type { Command } from "commander" +import { accountCommand } from "./commands/account.js" import { apiCommand } from "./commands/api.js" import { commentsCommand } from "./commands/comments.js" import { databasesCommand } from "./commands/databases.js" @@ -87,6 +88,7 @@ export const definition = (): ProgramDefinition => ({ commands: () => [ commandsCommand(IDENTITY), sessionCommand(), + accountCommand(), ...libraryCommands(), databasesCommand(), commentsCommand(),