@sqaoss/flowy 1.5.0 → 1.6.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +115 -33
- package/package.json +1 -1
- package/skills/using-flowy/SKILL.md +127 -55
- package/src/commands/export.test.ts +242 -0
- package/src/commands/export.ts +130 -0
- package/src/commands/import.test.ts +287 -0
- package/src/commands/import.ts +181 -0
- package/src/commands/setup.test.ts +90 -5
- package/src/commands/setup.ts +33 -15
- package/src/index.test.ts +23 -0
- package/src/index.ts +4 -0
- package/src/util/manifest.test.ts +159 -0
- package/src/util/manifest.ts +192 -0
|
@@ -0,0 +1,242 @@
|
|
|
1
|
+
import { beforeEach, describe, expect, test, vi } from 'vitest'
|
|
2
|
+
|
|
3
|
+
vi.mock('../util/client.ts', () => ({
|
|
4
|
+
graphql: vi.fn(),
|
|
5
|
+
}))
|
|
6
|
+
|
|
7
|
+
vi.mock('../util/config.ts', () => ({
|
|
8
|
+
requireProject: vi.fn(() => ({ id: 'srv_proj', name: 'Demo' })),
|
|
9
|
+
}))
|
|
10
|
+
|
|
11
|
+
vi.mock('../util/format.ts', () => ({
|
|
12
|
+
output: vi.fn(),
|
|
13
|
+
outputError: vi.fn(),
|
|
14
|
+
}))
|
|
15
|
+
|
|
16
|
+
vi.mock('node:fs', () => ({
|
|
17
|
+
writeFileSync: vi.fn(),
|
|
18
|
+
}))
|
|
19
|
+
|
|
20
|
+
const FLOWY_KEY_FIELD = '__flowyKey'
|
|
21
|
+
|
|
22
|
+
/** A server node row stamped with its client-key only (no edges in metadata). */
|
|
23
|
+
function srv(
|
|
24
|
+
id: string,
|
|
25
|
+
type: string,
|
|
26
|
+
title: string,
|
|
27
|
+
key: string,
|
|
28
|
+
extraMeta: Record<string, unknown> = {},
|
|
29
|
+
): Record<string, unknown> {
|
|
30
|
+
return {
|
|
31
|
+
id,
|
|
32
|
+
type,
|
|
33
|
+
title,
|
|
34
|
+
description: null,
|
|
35
|
+
status: 'draft',
|
|
36
|
+
metadata: JSON.stringify({ ...extraMeta, [FLOWY_KEY_FIELD]: key }),
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* Build a graphql mock backed by a node set and a real-edge-model edge set.
|
|
42
|
+
* `edges` are SERVER-id triples; the `edges()` query returns connected nodes
|
|
43
|
+
* (with metadata) so export can resolve their client-keys.
|
|
44
|
+
*/
|
|
45
|
+
function mockServer(
|
|
46
|
+
graphql: ReturnType<typeof vi.fn>,
|
|
47
|
+
project: Record<string, unknown>,
|
|
48
|
+
descendants: Array<Record<string, unknown>>,
|
|
49
|
+
edges: Array<{ source: string; target: string; relation: string }> = [],
|
|
50
|
+
) {
|
|
51
|
+
const byId = new Map<string, Record<string, unknown>>()
|
|
52
|
+
for (const n of [project, ...descendants]) byId.set(n.id as string, n)
|
|
53
|
+
graphql.mockImplementation(
|
|
54
|
+
async (query: string, variables?: Record<string, unknown>) => {
|
|
55
|
+
if (query.includes('descendants')) return { descendants }
|
|
56
|
+
if (query.includes('node(')) return { node: project }
|
|
57
|
+
if (query.includes('edges(')) {
|
|
58
|
+
const out = edges
|
|
59
|
+
.filter(
|
|
60
|
+
(e) =>
|
|
61
|
+
e.source === variables?.nodeId &&
|
|
62
|
+
e.relation === variables?.relation,
|
|
63
|
+
)
|
|
64
|
+
.map((e) => {
|
|
65
|
+
const n = byId.get(e.target)
|
|
66
|
+
return { id: e.target, metadata: n?.metadata ?? null }
|
|
67
|
+
})
|
|
68
|
+
return { edges: out }
|
|
69
|
+
}
|
|
70
|
+
return {}
|
|
71
|
+
},
|
|
72
|
+
)
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
describe('export command', () => {
|
|
76
|
+
beforeEach(() => {
|
|
77
|
+
vi.clearAllMocks()
|
|
78
|
+
})
|
|
79
|
+
|
|
80
|
+
test('exports a no-argument export command (optional output path)', async () => {
|
|
81
|
+
const { exportCommand } = await import('./export.ts')
|
|
82
|
+
expect(exportCommand.name()).toBe('export')
|
|
83
|
+
})
|
|
84
|
+
|
|
85
|
+
test('emits a manifest with client-keys and edges read from the edge model', async () => {
|
|
86
|
+
const { graphql } = await import('../util/client.ts')
|
|
87
|
+
const { output } = await import('../util/format.ts')
|
|
88
|
+
const { exportCommand } = await import('./export.ts')
|
|
89
|
+
|
|
90
|
+
const project = srv('srv_proj', 'project', 'Demo', 'proj')
|
|
91
|
+
const feature = srv('srv_feat-1', 'feature', 'Auth', 'feat-1')
|
|
92
|
+
const task1 = srv('srv_task-1', 'task', 'Login', 'task-1', {
|
|
93
|
+
priority: 'high',
|
|
94
|
+
})
|
|
95
|
+
const task2 = srv('srv_task-2', 'task', 'Logout', 'task-2')
|
|
96
|
+
|
|
97
|
+
mockServer(
|
|
98
|
+
vi.mocked(graphql),
|
|
99
|
+
project,
|
|
100
|
+
[feature, task1, task2],
|
|
101
|
+
[
|
|
102
|
+
{ source: 'srv_feat-1', target: 'srv_proj', relation: 'part_of' },
|
|
103
|
+
{ source: 'srv_task-1', target: 'srv_feat-1', relation: 'part_of' },
|
|
104
|
+
{ source: 'srv_task-2', target: 'srv_feat-1', relation: 'part_of' },
|
|
105
|
+
{ source: 'srv_task-2', target: 'srv_task-1', relation: 'blocks' },
|
|
106
|
+
],
|
|
107
|
+
)
|
|
108
|
+
|
|
109
|
+
await exportCommand.parseAsync([], { from: 'user' })
|
|
110
|
+
|
|
111
|
+
const manifest = vi.mocked(output).mock.calls.at(-1)?.[0] as {
|
|
112
|
+
version: number
|
|
113
|
+
nodes: Array<Record<string, unknown>>
|
|
114
|
+
edges: Array<Record<string, unknown>>
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
expect(manifest.version).toBe(1)
|
|
118
|
+
expect(manifest.nodes).toHaveLength(4)
|
|
119
|
+
|
|
120
|
+
const byKey = Object.fromEntries(manifest.nodes.map((n) => [n.key, n]))
|
|
121
|
+
expect(byKey.proj).toMatchObject({ type: 'project', title: 'Demo' })
|
|
122
|
+
// The reserved client-key field is stripped from exported user metadata.
|
|
123
|
+
expect(byKey['task-1']).toMatchObject({
|
|
124
|
+
type: 'task',
|
|
125
|
+
parent: 'feat-1',
|
|
126
|
+
metadata: { priority: 'high' },
|
|
127
|
+
})
|
|
128
|
+
expect(
|
|
129
|
+
(byKey['task-1'] as { metadata: Record<string, unknown> }).metadata
|
|
130
|
+
.__flowyKey,
|
|
131
|
+
).toBeUndefined()
|
|
132
|
+
|
|
133
|
+
// 3 part_of edges + 1 blocks edge, expressed in client-keys.
|
|
134
|
+
expect(manifest.edges).toHaveLength(4)
|
|
135
|
+
expect(manifest.edges).toContainEqual({
|
|
136
|
+
source: 'task-2',
|
|
137
|
+
target: 'task-1',
|
|
138
|
+
relation: 'blocks',
|
|
139
|
+
})
|
|
140
|
+
expect(manifest.edges).toContainEqual({
|
|
141
|
+
source: 'feat-1',
|
|
142
|
+
target: 'proj',
|
|
143
|
+
relation: 'part_of',
|
|
144
|
+
})
|
|
145
|
+
})
|
|
146
|
+
|
|
147
|
+
test('captures an externally-created (task block) edge, not just import edges', async () => {
|
|
148
|
+
const { graphql } = await import('../util/client.ts')
|
|
149
|
+
const { output } = await import('../util/format.ts')
|
|
150
|
+
const { exportCommand } = await import('./export.ts')
|
|
151
|
+
|
|
152
|
+
const project = srv('srv_proj', 'project', 'Demo', 'proj')
|
|
153
|
+
const task1 = srv('srv_task-1', 'task', 'A', 'task-1')
|
|
154
|
+
const task2 = srv('srv_task-2', 'task', 'B', 'task-2')
|
|
155
|
+
|
|
156
|
+
mockServer(
|
|
157
|
+
vi.mocked(graphql),
|
|
158
|
+
project,
|
|
159
|
+
[task1, task2],
|
|
160
|
+
[
|
|
161
|
+
{ source: 'srv_task-1', target: 'srv_proj', relation: 'part_of' },
|
|
162
|
+
{ source: 'srv_task-2', target: 'srv_proj', relation: 'part_of' },
|
|
163
|
+
// This edge exists only in the edge model (e.g. created by `task block`),
|
|
164
|
+
// never recorded in any node metadata — export must still capture it.
|
|
165
|
+
{ source: 'srv_task-1', target: 'srv_task-2', relation: 'blocks' },
|
|
166
|
+
],
|
|
167
|
+
)
|
|
168
|
+
|
|
169
|
+
await exportCommand.parseAsync([], { from: 'user' })
|
|
170
|
+
|
|
171
|
+
const manifest = vi.mocked(output).mock.calls.at(-1)?.[0] as {
|
|
172
|
+
edges: Array<Record<string, unknown>>
|
|
173
|
+
}
|
|
174
|
+
expect(manifest.edges).toContainEqual({
|
|
175
|
+
source: 'task-1',
|
|
176
|
+
target: 'task-2',
|
|
177
|
+
relation: 'blocks',
|
|
178
|
+
})
|
|
179
|
+
})
|
|
180
|
+
|
|
181
|
+
test('writes to a file when an output path is given', async () => {
|
|
182
|
+
const { graphql } = await import('../util/client.ts')
|
|
183
|
+
const { writeFileSync } = await import('node:fs')
|
|
184
|
+
const { exportCommand } = await import('./export.ts')
|
|
185
|
+
|
|
186
|
+
const project = srv('srv_proj', 'project', 'Demo', 'proj')
|
|
187
|
+
mockServer(vi.mocked(graphql), project, [])
|
|
188
|
+
|
|
189
|
+
await exportCommand.parseAsync(['out.json'], { from: 'user' })
|
|
190
|
+
|
|
191
|
+
expect(writeFileSync).toHaveBeenCalledWith(
|
|
192
|
+
'out.json',
|
|
193
|
+
expect.stringContaining('"version": 1'),
|
|
194
|
+
)
|
|
195
|
+
})
|
|
196
|
+
|
|
197
|
+
test('export output round-trips: it is a valid import manifest', async () => {
|
|
198
|
+
const { graphql } = await import('../util/client.ts')
|
|
199
|
+
const { output } = await import('../util/format.ts')
|
|
200
|
+
const { parseManifest, serializeManifest } = await import(
|
|
201
|
+
'../util/manifest.ts'
|
|
202
|
+
)
|
|
203
|
+
const { exportCommand } = await import('./export.ts')
|
|
204
|
+
|
|
205
|
+
const project = srv('srv_proj', 'project', 'Demo', 'proj')
|
|
206
|
+
const feature = srv('srv_feat-1', 'feature', 'Auth', 'feat-1')
|
|
207
|
+
const task = srv('srv_task-1', 'task', 'Login', 'task-1')
|
|
208
|
+
|
|
209
|
+
mockServer(
|
|
210
|
+
vi.mocked(graphql),
|
|
211
|
+
project,
|
|
212
|
+
[feature, task],
|
|
213
|
+
[
|
|
214
|
+
{ source: 'srv_feat-1', target: 'srv_proj', relation: 'part_of' },
|
|
215
|
+
{ source: 'srv_task-1', target: 'srv_feat-1', relation: 'part_of' },
|
|
216
|
+
{ source: 'srv_task-1', target: 'srv_feat-1', relation: 'blocks' },
|
|
217
|
+
],
|
|
218
|
+
)
|
|
219
|
+
|
|
220
|
+
await exportCommand.parseAsync([], { from: 'user' })
|
|
221
|
+
|
|
222
|
+
const manifest = vi.mocked(output).mock.calls.at(-1)?.[0]
|
|
223
|
+
// Re-parsing the exported manifest through the import parser must succeed
|
|
224
|
+
// and preserve structure — the export→import contract.
|
|
225
|
+
const reparsed = parseManifest(serializeManifest(manifest as never))
|
|
226
|
+
expect(reparsed).toEqual(manifest)
|
|
227
|
+
})
|
|
228
|
+
|
|
229
|
+
test('reports errors via outputError', async () => {
|
|
230
|
+
const { graphql } = await import('../util/client.ts')
|
|
231
|
+
const { outputError } = await import('../util/format.ts')
|
|
232
|
+
const { exportCommand } = await import('./export.ts')
|
|
233
|
+
|
|
234
|
+
vi.mocked(graphql).mockRejectedValueOnce(new Error('boom'))
|
|
235
|
+
|
|
236
|
+
await exportCommand.parseAsync([], { from: 'user' })
|
|
237
|
+
|
|
238
|
+
expect(outputError).toHaveBeenCalledWith(
|
|
239
|
+
expect.objectContaining({ message: 'boom' }),
|
|
240
|
+
)
|
|
241
|
+
})
|
|
242
|
+
})
|
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
import { writeFileSync } from 'node:fs'
|
|
2
|
+
import { Command } from 'commander'
|
|
3
|
+
import { graphql } from '../util/client.ts'
|
|
4
|
+
import { requireProject } from '../util/config.ts'
|
|
5
|
+
import { output, outputError } from '../util/format.ts'
|
|
6
|
+
import {
|
|
7
|
+
MANIFEST_VERSION,
|
|
8
|
+
type Manifest,
|
|
9
|
+
type ManifestEdge,
|
|
10
|
+
type ManifestNode,
|
|
11
|
+
readClientKey,
|
|
12
|
+
serializeManifest,
|
|
13
|
+
stripClientKey,
|
|
14
|
+
} from '../util/manifest.ts'
|
|
15
|
+
|
|
16
|
+
/** Relations export captures from the real edge model. */
|
|
17
|
+
const RELATIONS = ['part_of', 'blocks'] as const
|
|
18
|
+
|
|
19
|
+
interface ServerNode {
|
|
20
|
+
id: string
|
|
21
|
+
type: string
|
|
22
|
+
title: string
|
|
23
|
+
description: string | null
|
|
24
|
+
status: string
|
|
25
|
+
metadata: string | null
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
const PROJECT_QUERY = `query ExportProject($id: String!) {
|
|
29
|
+
node(id: $id) { id type title description status metadata }
|
|
30
|
+
}`
|
|
31
|
+
|
|
32
|
+
const DESCENDANTS_QUERY = `query ExportDescendants($nodeId: String!, $relation: String, $maxDepth: Int) {
|
|
33
|
+
descendants(nodeId: $nodeId, relation: $relation, maxDepth: $maxDepth) {
|
|
34
|
+
id type title description status metadata
|
|
35
|
+
}
|
|
36
|
+
}`
|
|
37
|
+
|
|
38
|
+
const EDGES_QUERY = `query ExportEdges($nodeId: String!, $relation: String!) {
|
|
39
|
+
edges(nodeId: $nodeId, relation: $relation, direction: "outgoing") {
|
|
40
|
+
id metadata
|
|
41
|
+
}
|
|
42
|
+
}`
|
|
43
|
+
|
|
44
|
+
export const exportCommand = new Command('export')
|
|
45
|
+
.description(
|
|
46
|
+
'Dump the active project (nodes + edges, with client-keys) as a manifest',
|
|
47
|
+
)
|
|
48
|
+
.argument('[output]', 'Write to this file instead of stdout')
|
|
49
|
+
.action(async (outputPath: string | undefined) => {
|
|
50
|
+
try {
|
|
51
|
+
const project = requireProject()
|
|
52
|
+
const root = await graphql<{ node: ServerNode | null }>(PROJECT_QUERY, {
|
|
53
|
+
id: project.id,
|
|
54
|
+
})
|
|
55
|
+
if (!root.node) {
|
|
56
|
+
throw new Error(`Active project ${project.id} not found.`)
|
|
57
|
+
}
|
|
58
|
+
const descendants = await graphql<{ descendants: ServerNode[] }>(
|
|
59
|
+
DESCENDANTS_QUERY,
|
|
60
|
+
{ nodeId: project.id, relation: 'part_of', maxDepth: 100 },
|
|
61
|
+
)
|
|
62
|
+
|
|
63
|
+
const serverNodes = [root.node, ...descendants.descendants]
|
|
64
|
+
|
|
65
|
+
// Map server id -> client-key so edges (which the server returns by id)
|
|
66
|
+
// can be expressed in the manifest's client-key space. A node without a
|
|
67
|
+
// recorded key falls back to its server id so it still round-trips.
|
|
68
|
+
const keyOf = (id: string, metadata: string | null) =>
|
|
69
|
+
readClientKey(metadata) ?? id
|
|
70
|
+
const keyById = new Map<string, string>()
|
|
71
|
+
for (const sn of serverNodes)
|
|
72
|
+
keyById.set(sn.id, keyOf(sn.id, sn.metadata))
|
|
73
|
+
|
|
74
|
+
const nodes: ManifestNode[] = serverNodes.map((sn) => {
|
|
75
|
+
const node: ManifestNode = {
|
|
76
|
+
key: keyById.get(sn.id) ?? sn.id,
|
|
77
|
+
type: sn.type,
|
|
78
|
+
title: sn.title,
|
|
79
|
+
}
|
|
80
|
+
if (sn.description != null) node.description = sn.description
|
|
81
|
+
if (sn.status != null) node.status = sn.status
|
|
82
|
+
const userMeta = stripClientKey(sn.metadata)
|
|
83
|
+
if (userMeta) node.metadata = userMeta
|
|
84
|
+
return node
|
|
85
|
+
})
|
|
86
|
+
|
|
87
|
+
// Read edges back through the real edge model, so we capture ALL edges,
|
|
88
|
+
// including ones created outside import (e.g. `task block`), not just
|
|
89
|
+
// those import created.
|
|
90
|
+
const edges: ManifestEdge[] = []
|
|
91
|
+
const seen = new Set<string>()
|
|
92
|
+
for (const sn of serverNodes) {
|
|
93
|
+
const sourceKey = keyById.get(sn.id) ?? sn.id
|
|
94
|
+
for (const relation of RELATIONS) {
|
|
95
|
+
const data = await graphql<{
|
|
96
|
+
edges: Array<{ id: string; metadata: string | null }>
|
|
97
|
+
}>(EDGES_QUERY, { nodeId: sn.id, relation })
|
|
98
|
+
for (const target of data.edges) {
|
|
99
|
+
const targetKey =
|
|
100
|
+
keyById.get(target.id) ?? keyOf(target.id, target.metadata)
|
|
101
|
+
const k = `${sourceKey}|${targetKey}|${relation}`
|
|
102
|
+
if (seen.has(k)) continue
|
|
103
|
+
seen.add(k)
|
|
104
|
+
// part_of is surfaced as the node's `parent` so import re-derives it,
|
|
105
|
+
// and is also kept in the edge list for a complete dependency graph.
|
|
106
|
+
if (relation === 'part_of') {
|
|
107
|
+
const node = nodes.find((n) => n.key === sourceKey)
|
|
108
|
+
if (node) node.parent = targetKey
|
|
109
|
+
}
|
|
110
|
+
edges.push({ source: sourceKey, target: targetKey, relation })
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
const manifest: Manifest = { version: MANIFEST_VERSION, nodes, edges }
|
|
116
|
+
|
|
117
|
+
if (outputPath) {
|
|
118
|
+
writeFileSync(outputPath, serializeManifest(manifest))
|
|
119
|
+
output({
|
|
120
|
+
exported: nodes.length,
|
|
121
|
+
edges: edges.length,
|
|
122
|
+
file: outputPath,
|
|
123
|
+
})
|
|
124
|
+
} else {
|
|
125
|
+
output(manifest)
|
|
126
|
+
}
|
|
127
|
+
} catch (error) {
|
|
128
|
+
outputError(error)
|
|
129
|
+
}
|
|
130
|
+
})
|
|
@@ -0,0 +1,287 @@
|
|
|
1
|
+
import { beforeEach, describe, expect, test, vi } from 'vitest'
|
|
2
|
+
|
|
3
|
+
vi.mock('../util/client.ts', () => ({
|
|
4
|
+
graphql: vi.fn(),
|
|
5
|
+
}))
|
|
6
|
+
|
|
7
|
+
vi.mock('../util/format.ts', () => ({
|
|
8
|
+
output: vi.fn(),
|
|
9
|
+
outputError: vi.fn(),
|
|
10
|
+
}))
|
|
11
|
+
|
|
12
|
+
vi.mock('node:fs', () => ({
|
|
13
|
+
readFileSync: vi.fn(),
|
|
14
|
+
}))
|
|
15
|
+
|
|
16
|
+
const FLOWY_KEY_FIELD = '__flowyKey'
|
|
17
|
+
|
|
18
|
+
/** A small representative backlog: 1 project, 1 feature, 2 tasks, 1 blocks edge. */
|
|
19
|
+
const MANIFEST = {
|
|
20
|
+
version: 1,
|
|
21
|
+
nodes: [
|
|
22
|
+
{ key: 'proj', type: 'project', title: 'Demo' },
|
|
23
|
+
{ key: 'feat-1', type: 'feature', title: 'Auth', parent: 'proj' },
|
|
24
|
+
{
|
|
25
|
+
key: 'task-1',
|
|
26
|
+
type: 'task',
|
|
27
|
+
title: 'Login',
|
|
28
|
+
parent: 'feat-1',
|
|
29
|
+
status: 'draft',
|
|
30
|
+
metadata: { priority: 'high' },
|
|
31
|
+
},
|
|
32
|
+
{ key: 'task-2', type: 'task', title: 'Logout', parent: 'feat-1' },
|
|
33
|
+
],
|
|
34
|
+
edges: [{ source: 'task-2', target: 'task-1', relation: 'blocks' }],
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/** Parse the JSON metadata string a CLI mutation sent to the server. */
|
|
38
|
+
function metaOf(variables: Record<string, unknown>): Record<string, unknown> {
|
|
39
|
+
return JSON.parse(variables.metadata as string)
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
describe('import command', () => {
|
|
43
|
+
beforeEach(() => {
|
|
44
|
+
vi.clearAllMocks()
|
|
45
|
+
})
|
|
46
|
+
|
|
47
|
+
test('exports a single-argument import command', async () => {
|
|
48
|
+
const { importCommand } = await import('./import.ts')
|
|
49
|
+
expect(importCommand.name()).toBe('import')
|
|
50
|
+
})
|
|
51
|
+
|
|
52
|
+
test('metadata carries only the client-key, never edge data', async () => {
|
|
53
|
+
const { graphql } = await import('../util/client.ts')
|
|
54
|
+
const { readFileSync } = await import('node:fs')
|
|
55
|
+
const { importCommand } = await import('./import.ts')
|
|
56
|
+
|
|
57
|
+
vi.mocked(readFileSync).mockReturnValue(JSON.stringify(MANIFEST))
|
|
58
|
+
vi.mocked(graphql).mockImplementation(async (query: string) => {
|
|
59
|
+
if (query.includes('nodes(')) return { nodes: [] }
|
|
60
|
+
if (query.includes('createNode')) return { createNode: { id: 'srv_x' } }
|
|
61
|
+
if (query.includes('createEdge')) return { createEdge: {} }
|
|
62
|
+
return {}
|
|
63
|
+
})
|
|
64
|
+
|
|
65
|
+
await importCommand.parseAsync(['manifest.json'], { from: 'user' })
|
|
66
|
+
|
|
67
|
+
for (const [q, vars] of vi.mocked(graphql).mock.calls) {
|
|
68
|
+
if (!q.includes('createNode')) continue
|
|
69
|
+
const meta = metaOf(vars ?? {})
|
|
70
|
+
expect(typeof meta[FLOWY_KEY_FIELD]).toBe('string')
|
|
71
|
+
// The dropped edge-stamp hack must not reappear in any form.
|
|
72
|
+
expect(meta.__flowy).toBeUndefined()
|
|
73
|
+
expect(meta.edges).toBeUndefined()
|
|
74
|
+
}
|
|
75
|
+
})
|
|
76
|
+
|
|
77
|
+
test('first run: creates every node and edge, returns a key→id map', async () => {
|
|
78
|
+
const { graphql } = await import('../util/client.ts')
|
|
79
|
+
const { output } = await import('../util/format.ts')
|
|
80
|
+
const { readFileSync } = await import('node:fs')
|
|
81
|
+
const { importCommand } = await import('./import.ts')
|
|
82
|
+
|
|
83
|
+
vi.mocked(readFileSync).mockReturnValue(JSON.stringify(MANIFEST))
|
|
84
|
+
|
|
85
|
+
// No existing nodes for any type.
|
|
86
|
+
vi.mocked(graphql).mockImplementation(
|
|
87
|
+
async (query: string, variables?: Record<string, unknown>) => {
|
|
88
|
+
if (query.includes('nodes(')) return { nodes: [] }
|
|
89
|
+
if (query.includes('createNode')) {
|
|
90
|
+
const key = metaOf(variables ?? {})[FLOWY_KEY_FIELD] as string
|
|
91
|
+
return { createNode: { id: `srv_${key}` } }
|
|
92
|
+
}
|
|
93
|
+
if (query.includes('updateNode')) {
|
|
94
|
+
return { updateNode: { id: variables?.id } }
|
|
95
|
+
}
|
|
96
|
+
// No pre-existing nodes → the existing-edges query should never run.
|
|
97
|
+
if (query.includes('edges(')) {
|
|
98
|
+
throw new Error(
|
|
99
|
+
'edges() should not be queried with no existing nodes',
|
|
100
|
+
)
|
|
101
|
+
}
|
|
102
|
+
if (query.includes('createEdge')) return { createEdge: {} }
|
|
103
|
+
return {}
|
|
104
|
+
},
|
|
105
|
+
)
|
|
106
|
+
|
|
107
|
+
await importCommand.parseAsync(['manifest.json'], { from: 'user' })
|
|
108
|
+
|
|
109
|
+
const calls = vi.mocked(graphql).mock.calls
|
|
110
|
+
const created = calls.filter(([q]) => q.includes('createNode'))
|
|
111
|
+
const updated = calls.filter(([q]) => q.includes('updateNode'))
|
|
112
|
+
const edges = calls.filter(([q]) => q.includes('createEdge'))
|
|
113
|
+
|
|
114
|
+
// All 4 nodes created, none updated on a clean import.
|
|
115
|
+
expect(created).toHaveLength(4)
|
|
116
|
+
expect(updated).toHaveLength(0)
|
|
117
|
+
|
|
118
|
+
// part_of edges (feat-1→proj, task-1→feat-1, task-2→feat-1) + 1 blocks = 4.
|
|
119
|
+
expect(edges).toHaveLength(4)
|
|
120
|
+
|
|
121
|
+
// Output is the key→id map.
|
|
122
|
+
const mapArg = vi.mocked(output).mock.calls.at(-1)?.[0] as {
|
|
123
|
+
map: Record<string, string>
|
|
124
|
+
}
|
|
125
|
+
expect(mapArg.map).toMatchObject({
|
|
126
|
+
proj: 'srv_proj',
|
|
127
|
+
'feat-1': 'srv_feat-1',
|
|
128
|
+
'task-1': 'srv_task-1',
|
|
129
|
+
'task-2': 'srv_task-2',
|
|
130
|
+
})
|
|
131
|
+
})
|
|
132
|
+
|
|
133
|
+
test('re-import is idempotent: existing keys update, never re-create', async () => {
|
|
134
|
+
const { graphql } = await import('../util/client.ts')
|
|
135
|
+
const { readFileSync } = await import('node:fs')
|
|
136
|
+
const { importCommand } = await import('./import.ts')
|
|
137
|
+
|
|
138
|
+
vi.mocked(readFileSync).mockReturnValue(JSON.stringify(MANIFEST))
|
|
139
|
+
|
|
140
|
+
// Every manifest node already exists (carrying its client-key), and every
|
|
141
|
+
// edge already exists in the real edge model.
|
|
142
|
+
const existing = (type: string) => {
|
|
143
|
+
const byType: Record<string, Array<Record<string, unknown>>> = {
|
|
144
|
+
project: [node('proj', 'srv_proj')],
|
|
145
|
+
feature: [node('feat-1', 'srv_feat-1')],
|
|
146
|
+
task: [node('task-1', 'srv_task-1'), node('task-2', 'srv_task-2')],
|
|
147
|
+
}
|
|
148
|
+
return byType[type] ?? []
|
|
149
|
+
}
|
|
150
|
+
const edgesOf = (nodeId: string, relation: string) =>
|
|
151
|
+
EDGES.filter((e) => e.source === nodeId && e.relation === relation).map(
|
|
152
|
+
(e) => ({ id: e.target }),
|
|
153
|
+
)
|
|
154
|
+
|
|
155
|
+
vi.mocked(graphql).mockImplementation(
|
|
156
|
+
async (query: string, variables?: Record<string, unknown>) => {
|
|
157
|
+
if (query.includes('nodes(')) {
|
|
158
|
+
return { nodes: existing(variables?.type as string) }
|
|
159
|
+
}
|
|
160
|
+
if (query.includes('edges(')) {
|
|
161
|
+
return {
|
|
162
|
+
edges: edgesOf(
|
|
163
|
+
variables?.nodeId as string,
|
|
164
|
+
variables?.relation as string,
|
|
165
|
+
),
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
if (query.includes('updateNode')) {
|
|
169
|
+
return { updateNode: { id: variables?.id } }
|
|
170
|
+
}
|
|
171
|
+
if (query.includes('createNode')) {
|
|
172
|
+
throw new Error('createNode must not be called on re-import')
|
|
173
|
+
}
|
|
174
|
+
if (query.includes('createEdge')) return { createEdge: {} }
|
|
175
|
+
return {}
|
|
176
|
+
},
|
|
177
|
+
)
|
|
178
|
+
|
|
179
|
+
await importCommand.parseAsync(['manifest.json'], { from: 'user' })
|
|
180
|
+
|
|
181
|
+
const calls = vi.mocked(graphql).mock.calls
|
|
182
|
+
const created = calls.filter(([q]) => q.includes('createNode'))
|
|
183
|
+
const updated = calls.filter(([q]) => q.includes('updateNode'))
|
|
184
|
+
const edges = calls.filter(([q]) => q.includes('createEdge'))
|
|
185
|
+
|
|
186
|
+
// No node is re-created; all four are updated in place.
|
|
187
|
+
expect(created).toHaveLength(0)
|
|
188
|
+
expect(updated).toHaveLength(4)
|
|
189
|
+
|
|
190
|
+
// Every edge already exists in the edge model → none re-created.
|
|
191
|
+
expect(edges).toHaveLength(0)
|
|
192
|
+
})
|
|
193
|
+
|
|
194
|
+
test('does not re-create an edge that already exists in the edge model', async () => {
|
|
195
|
+
const { graphql } = await import('../util/client.ts')
|
|
196
|
+
const { readFileSync } = await import('node:fs')
|
|
197
|
+
const { importCommand } = await import('./import.ts')
|
|
198
|
+
|
|
199
|
+
vi.mocked(readFileSync).mockReturnValue(JSON.stringify(MANIFEST))
|
|
200
|
+
|
|
201
|
+
const existing = (type: string) => {
|
|
202
|
+
const byType: Record<string, Array<Record<string, unknown>>> = {
|
|
203
|
+
project: [node('proj', 'srv_proj')],
|
|
204
|
+
feature: [node('feat-1', 'srv_feat-1')],
|
|
205
|
+
task: [node('task-1', 'srv_task-1'), node('task-2', 'srv_task-2')],
|
|
206
|
+
}
|
|
207
|
+
return byType[type] ?? []
|
|
208
|
+
}
|
|
209
|
+
// Only the part_of edges exist server-side; the blocks edge does not.
|
|
210
|
+
const present = EDGES.filter((e) => e.relation === 'part_of')
|
|
211
|
+
const edgesOf = (nodeId: string, relation: string) =>
|
|
212
|
+
present
|
|
213
|
+
.filter((e) => e.source === nodeId && e.relation === relation)
|
|
214
|
+
.map((e) => ({ id: e.target }))
|
|
215
|
+
|
|
216
|
+
vi.mocked(graphql).mockImplementation(
|
|
217
|
+
async (query: string, variables?: Record<string, unknown>) => {
|
|
218
|
+
if (query.includes('nodes(')) {
|
|
219
|
+
return { nodes: existing(variables?.type as string) }
|
|
220
|
+
}
|
|
221
|
+
if (query.includes('edges(')) {
|
|
222
|
+
return {
|
|
223
|
+
edges: edgesOf(
|
|
224
|
+
variables?.nodeId as string,
|
|
225
|
+
variables?.relation as string,
|
|
226
|
+
),
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
if (query.includes('updateNode')) {
|
|
230
|
+
return { updateNode: { id: variables?.id } }
|
|
231
|
+
}
|
|
232
|
+
if (query.includes('createEdge')) return { createEdge: {} }
|
|
233
|
+
return {}
|
|
234
|
+
},
|
|
235
|
+
)
|
|
236
|
+
|
|
237
|
+
await importCommand.parseAsync(['manifest.json'], { from: 'user' })
|
|
238
|
+
|
|
239
|
+
const edges = vi
|
|
240
|
+
.mocked(graphql)
|
|
241
|
+
.mock.calls.filter(([q]) => q.includes('createEdge'))
|
|
242
|
+
expect(edges).toHaveLength(1)
|
|
243
|
+
expect(edges[0]?.[1]).toMatchObject({
|
|
244
|
+
sourceId: 'srv_task-2',
|
|
245
|
+
targetId: 'srv_task-1',
|
|
246
|
+
relation: 'blocks',
|
|
247
|
+
})
|
|
248
|
+
})
|
|
249
|
+
|
|
250
|
+
test('reports a parse error via outputError', async () => {
|
|
251
|
+
const { outputError } = await import('../util/format.ts')
|
|
252
|
+
const { readFileSync } = await import('node:fs')
|
|
253
|
+
const { importCommand } = await import('./import.ts')
|
|
254
|
+
|
|
255
|
+
vi.mocked(readFileSync).mockReturnValue('{not json')
|
|
256
|
+
|
|
257
|
+
await importCommand.parseAsync(['bad.json'], { from: 'user' })
|
|
258
|
+
|
|
259
|
+
expect(outputError).toHaveBeenCalledWith(
|
|
260
|
+
expect.objectContaining({
|
|
261
|
+
message: expect.stringMatching(/invalid json/i),
|
|
262
|
+
}),
|
|
263
|
+
)
|
|
264
|
+
})
|
|
265
|
+
})
|
|
266
|
+
|
|
267
|
+
/** The full edge set this manifest implies, expressed in SERVER ids. */
|
|
268
|
+
const EDGES = [
|
|
269
|
+
{ source: 'srv_feat-1', target: 'srv_proj', relation: 'part_of' },
|
|
270
|
+
{ source: 'srv_task-1', target: 'srv_feat-1', relation: 'part_of' },
|
|
271
|
+
{ source: 'srv_task-2', target: 'srv_feat-1', relation: 'part_of' },
|
|
272
|
+
{ source: 'srv_task-2', target: 'srv_task-1', relation: 'blocks' },
|
|
273
|
+
]
|
|
274
|
+
|
|
275
|
+
/**
|
|
276
|
+
* Build an existing server node row stamped with its client-key only.
|
|
277
|
+
* Edges are NOT stored in metadata — they live in the real edge model and are
|
|
278
|
+
* mocked separately via the `edges()` query.
|
|
279
|
+
*/
|
|
280
|
+
function node(key: string, id: string): Record<string, unknown> {
|
|
281
|
+
return {
|
|
282
|
+
id,
|
|
283
|
+
type: 'x',
|
|
284
|
+
title: key,
|
|
285
|
+
metadata: JSON.stringify({ [FLOWY_KEY_FIELD]: key }),
|
|
286
|
+
}
|
|
287
|
+
}
|