Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions changes/feat-account-remove.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
### Added

- **`notion <profile> 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.
13 changes: 13 additions & 0 deletions contracts/wirecat.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
24 changes: 24 additions & 0 deletions docs/commands.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 5 additions & 1 deletion docs/dev/test-matrix.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 |
|---|---|---|---|
Expand All @@ -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` | | ✅ | |
Expand Down
35 changes: 35 additions & 0 deletions src/commands/account.ts
Original file line number Diff line number Diff line change
@@ -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 },
),
)
44 changes: 44 additions & 0 deletions src/library.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
})
2 changes: 1 addition & 1 deletion src/library.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ export const withStore = async <T>(
}
}

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 = <T>(path: string): T | undefined => {
Expand Down
2 changes: 2 additions & 0 deletions src/program.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -87,6 +88,7 @@ export const definition = (): ProgramDefinition => ({
commands: () => [
commandsCommand(IDENTITY),
sessionCommand(),
accountCommand(),
...libraryCommands(),
databasesCommand(),
commentsCommand(),
Expand Down
Loading