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
11 changes: 6 additions & 5 deletions source/cli.test.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { join, resolve } from 'node:path'
import { createRouter } from '@dotpm/api'
import { FakeApi } from '@dotpm/api/testing'
import { beforeEach, describe, expect, it } from 'vitest'
import { runCli, type CliDeps } from './cli.ts'

const TOKEN = 'secret-token-0123456789'
const VAULT = '/vault'
const SETTINGS = `${VAULT}/.obsidian/plugins/project-manager/data.json`
const VAULT = resolve('/vault')
const SETTINGS = join(VAULT, '.obsidian', 'plugins', 'project-manager', 'data.json')

interface Run {
code: number
Expand Down Expand Up @@ -40,10 +41,10 @@ describe('runCli', () => {
}
const deps: CliDeps = {
env,
cwd: `${VAULT}/Projects`,
cwd: join(VAULT, 'Projects'),
tty,
version: '1.2.3',
exists: (path) => path === `${VAULT}/.obsidian` || path in files,
exists: (path) => path === join(VAULT, '.obsidian') || path in files,
readFile: (path) => files[path] ?? null,
readStdin: async () => stdin,
stdinLines: async function* () {},
Expand Down Expand Up @@ -96,7 +97,7 @@ describe('runCli', () => {
expect(task.out).toContain('title First')
const status = await run(['status'])
expect(status.out).toContain('server dotpm 9.9.9')
expect(status.out).toContain('from the vault at /vault')
expect(status.out).toContain(`from the vault at ${VAULT}`)
expect((await run(['--json', 'status'])).json).toMatchObject({ ok: true, url: 'http://127.0.0.1:27151' })
})

Expand Down
22 changes: 14 additions & 8 deletions source/connection.test.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
import { join, resolve } from 'node:path'
import { describe, expect, it } from 'vitest'
import { resolveConnection, type ConnectionInput } from './connection.ts'

const VAULT = '/home/me/Notes'
const SETTINGS = `${VAULT}/.obsidian/plugins/project-manager/data.json`
const VAULT = resolve('/home/me/Notes')
const SETTINGS = join(VAULT, '.obsidian', 'plugins', 'project-manager', 'data.json')
const ELSEWHERE = resolve('/elsewhere')

function input(overrides: Partial<ConnectionInput> = {}, files: Record<string, string> = {}): ConnectionInput {
const present = new Set([`${VAULT}/.obsidian`, ...Object.keys(files)])
const present = new Set([join(VAULT, '.obsidian'), ...Object.keys(files)])
return {
env: {},
cwd: `${VAULT}/Projects/Website_tasks`,
cwd: join(VAULT, 'Projects', 'Website_tasks'),
exists: (path) => present.has(path),
readFile: (path) => files[path] ?? null,
...overrides
Expand Down Expand Up @@ -40,13 +42,17 @@ describe('resolveConnection', () => {
})

it('reads a vault named by flag or environment', () => {
expect(resolveConnection(input({ vault: VAULT, cwd: '/elsewhere' }, { [SETTINGS]: ON })).token).toBe('tok')
expect(resolveConnection(input({ env: { DOTPM_VAULT: VAULT }, cwd: '/' }, { [SETTINGS]: ON })).token).toBe('tok')
expect(() => resolveConnection(input({ vault: '/nope' }, { [SETTINGS]: ON }))).toThrow('not an Obsidian vault')
expect(resolveConnection(input({ vault: VAULT, cwd: ELSEWHERE }, { [SETTINGS]: ON })).token).toBe('tok')
expect(resolveConnection(input({ env: { DOTPM_VAULT: VAULT }, cwd: resolve('/') }, { [SETTINGS]: ON })).token).toBe(
'tok'
)
expect(() => resolveConnection(input({ vault: resolve('/nope') }, { [SETTINGS]: ON }))).toThrow(
'not an Obsidian vault'
)
})

it('explains what is missing in a vault', () => {
expect(() => resolveConnection(input({ cwd: '/tmp' }))).toThrow('no Obsidian vault holds /tmp')
expect(() => resolveConnection(input({ cwd: ELSEWHERE }))).toThrow(`no Obsidian vault holds ${ELSEWHERE}`)
expect(() => resolveConnection(input())).toThrow('dotpm is not installed')
expect(() => resolveConnection(input({}, { [SETTINGS]: '{oops' }))).toThrow('could not be read')
expect(() => resolveConnection(input({}, { [SETTINGS]: '{"localApiEnabled":false}' }))).toThrow(
Expand Down