@yutaura/live-spec 0.1.0 → 0.2.0
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/.claude-plugin/plugin.json +1 -1
- package/README.md +12 -2
- package/dist/cli.js +49 -10
- package/dist/diff.d.ts +92 -2
- package/dist/diff.js +101 -64
- package/dist/ensure-scope.d.ts +9 -0
- package/dist/ensure-scope.js +81 -0
- package/dist/ensure.d.ts +4 -0
- package/dist/ensure.js +27 -2
- package/dist/explore.d.ts +20 -1
- package/dist/explore.js +34 -12
- package/dist/generate.d.ts +6 -0
- package/dist/generate.js +16 -4
- package/dist/load.d.ts +7 -1
- package/dist/load.js +5 -0
- package/dist/model.d.ts +85 -0
- package/dist/model.js +34 -0
- package/dist/mutation.d.ts +3 -0
- package/dist/mutation.js +51 -0
- package/dist/omission.d.ts +6 -0
- package/dist/omission.js +177 -0
- package/dist/project-diff.d.ts +24 -0
- package/dist/project-diff.js +76 -0
- package/dist/render/diff-html.d.ts +2 -0
- package/dist/render/diff-html.js +148 -0
- package/dist/render/diff-markdown.d.ts +11 -0
- package/dist/render/diff-markdown.js +70 -0
- package/dist/render/ensures.d.ts +2 -0
- package/dist/render/ensures.js +21 -0
- package/dist/render/html.d.ts +1 -0
- package/dist/render/html.js +42 -10
- package/dist/render/layout.d.ts +2 -0
- package/dist/render/layout.js +1 -1
- package/dist/render/markdown.js +24 -3
- package/dist/render/review.d.ts +17 -0
- package/dist/render/review.js +17 -0
- package/dist/render/union.d.ts +23 -0
- package/dist/render/union.js +43 -0
- package/dist/spec-loader.d.ts +2 -0
- package/dist/spec-loader.js +24 -0
- package/dist/static.d.ts +5 -0
- package/dist/static.js +34 -0
- package/package.json +7 -1
- package/skills/live-spec-authoring/SKILL.md +32 -2
- package/skills/live-spec-review/SKILL.md +16 -10
package/README.md
CHANGED
|
@@ -129,7 +129,8 @@ npx live-spec specs/ --out docs/spec/ # ドメインオブジ
|
|
|
129
129
|
npx live-spec specs/ --serve # ブラウザで見ながら書く(保存すると自動で再読み込み)
|
|
130
130
|
npx live-spec specs/order.spec.ts # spec が 1 つなら Markdown を標準出力に出す
|
|
131
131
|
npx live-spec specs/ --json # 正規化モデル(差分レビュー用)
|
|
132
|
-
npx live-spec diff specs/
|
|
132
|
+
npx live-spec diff specs/ --base main --head feature # ブランチ間で、仕様の振る舞いの変化を出す(--head を省くと作業ツリー)
|
|
133
|
+
npx live-spec diff specs/ --base main --html --out diff.html # 重ね合わせた状態遷移図と、画面の変更前・変更後を並べた HTML
|
|
133
134
|
```
|
|
134
135
|
|
|
135
136
|
## spec の書き方
|
|
@@ -145,7 +146,8 @@ spec ファイルは素の TypeScript です。export の名前で役割が決
|
|
|
145
146
|
| 上記以外の関数 | | 操作。`(状態, 登場人物, ...入力) => 次の状態`。できないときは `ensure(条件, "理由")` で止める |
|
|
146
147
|
| `invariants` | | 不変条件。`{ 説明: (状態) => boolean }` |
|
|
147
148
|
| `inputs` | | 3 つ目以降の引数の例。`{ 操作名: { 例の名前: [引数...] } }` |
|
|
148
|
-
| `
|
|
149
|
+
| `reviewed` | | 確かめてほしい点のうち、意図どおりと判断したもの。`{ "候補の ID": "理由" }` |
|
|
150
|
+
| `scenarios` | | シナリオ。`{ 名前: [{ do: 操作の関数, by: "登場人物", input?: "入力の例の名前", expect?: { 項目: 値 }, denied?: "理由" }] }`。初期状態から順に実行し、止まったり期待と違ったりしたら検査結果に出る。`denied` を付けた手順は「断られるはず」で、実行できてしまうと失敗する |
|
|
149
151
|
| `questions` | | 未決事項。`[{ ask: "質問", about?: 操作の関数 \| { 項目: 値 }, note?: "補足" }]`。仕様書の上のほうと、図・操作カードに印が出る |
|
|
150
152
|
| `links` | | ほかのドメインオブジェクトとのつながり。`[{ from?: 操作の関数, to: "相手の名前", action?: "相手の操作名", label: "説明" }]`。一覧ページに関係図が出る |
|
|
151
153
|
| `decisions` | | ルールの判断表。`{ 名前: { decide: (入力) => 結果, cases: [{ input, expect? }] } }`。各例を実行して「条件 × 結果」の表を作り、期待と違えば検査結果に出る |
|
|
@@ -160,6 +162,14 @@ spec ファイルは素の TypeScript です。export の名前で役割が決
|
|
|
160
162
|
- `ensure` の理由は、そのまま「できない理由」として表示される。読み手に伝わる文にする
|
|
161
163
|
- 「できない」は `ensure` で表す。普通の `throw` は仕様のバグとして検出される
|
|
162
164
|
|
|
165
|
+
### 漏れを見つける手がかり
|
|
166
|
+
|
|
167
|
+
AI が書いた spec の条件の書き忘れ・書き間違いを、人が見つけやすいように、live-spec は spec を実行した結果から次を出します。
|
|
168
|
+
|
|
169
|
+
- **確かめてほしい点**(ページの上部): 誰でもできる・いつでもできる・入力を見ていない操作、本人と別人を区別しない操作、結果がまったく同じ入力の例や登場人物、どの可否にも影響しない項目、意図がほかに書かれていない条件(`ensure` を外しても不変条件・シナリオ・画面のボタンのどれも気づかないもの)。意図どおりなら `export const reviewed = { "ID": "理由" }` に書くと「確認済み」に移る
|
|
170
|
+
- **条件の「通るのは」**(操作の節): `ensure` ごとに、理由の文と、その条件だけを効かせて実行して求めた「通るのは〜のとき」を並べる。「実行中のときだけ」なのに「通るのは status が queued のとき」なら、条件か理由の書き間違い
|
|
171
|
+
- **外したときの効き目**: その `ensure` を外すと何が起き、何が気づくか
|
|
172
|
+
|
|
163
173
|
## 制約
|
|
164
174
|
|
|
165
175
|
- 型の静的解析は構文解析なので、同じファイルの `type` / `interface` だけを辿ります
|
package/dist/cli.js
CHANGED
|
@@ -2,9 +2,12 @@
|
|
|
2
2
|
import { realpathSync } from "node:fs";
|
|
3
3
|
import { fileURLToPath, pathToFileURL } from "node:url";
|
|
4
4
|
import { parseArgs } from "node:util";
|
|
5
|
-
import { expandSpecPaths, generateProject, writeProjectOutput } from "./generate.js";
|
|
6
|
-
import {
|
|
5
|
+
import { expandSpecPaths, generateProject, writeOutput, writeProjectOutput } from "./generate.js";
|
|
6
|
+
import { diffProject } from "./project-diff.js";
|
|
7
|
+
import { renderDiffHtml } from "./render/diff-html.js";
|
|
8
|
+
import { renderDiffMarkdown } from "./render/diff-markdown.js";
|
|
7
9
|
import { startServer } from "./serve.js";
|
|
10
|
+
import { registerSpecLoader } from "./spec-loader.js";
|
|
8
11
|
import { installSkills } from "./skills.js";
|
|
9
12
|
import { watchSpec } from "./watch.js";
|
|
10
13
|
export { expandSpecPaths, generate, generateProject, specNameOf } from "./generate.js";
|
|
@@ -46,36 +49,66 @@ export const parseCliArgs = (argv) => {
|
|
|
46
49
|
const format = serve || values.html ? "html" : values.json ? "json" : "markdown";
|
|
47
50
|
return { paths: positionals, format, out, watch, serve, port, title: values.title ?? null };
|
|
48
51
|
};
|
|
49
|
-
const USAGE = "usage:
|
|
52
|
+
const USAGE = "usage: live-spec <file.spec.ts | dir>... [--html | --json] [--out <file | dir>] [--watch] [--serve [--port <n>]] [--title <名前>]";
|
|
53
|
+
const wantsHelp = (argv) => argv.includes("--help") || argv.includes("-h");
|
|
50
54
|
const messageOf = (e) => (e instanceof Error ? e.message : String(e));
|
|
51
55
|
// 終了コードを返す関数にしている理由: 失敗したときの終わり方(メッセージと終了コード)をテストで確かめるため。
|
|
52
56
|
// 監視を始めた場合は、プロセスを終わらせないので null を返す。
|
|
53
|
-
const DIFF_USAGE = "usage: live-spec diff <file.spec.ts
|
|
57
|
+
const DIFF_USAGE = "usage: live-spec diff <file.spec.ts | dir>... [--base <リビジョン>](既定は HEAD) [--head <リビジョン>](省くと作業ツリー) [--html | --json] [--out <file>]";
|
|
54
58
|
const runDiff = async (argv, io) => {
|
|
59
|
+
if (wantsHelp(argv)) {
|
|
60
|
+
io.stdout(`${DIFF_USAGE}\n`);
|
|
61
|
+
return 0;
|
|
62
|
+
}
|
|
55
63
|
let parsed;
|
|
56
64
|
try {
|
|
57
|
-
parsed = parseArgs({
|
|
65
|
+
parsed = parseArgs({
|
|
66
|
+
args: [...argv],
|
|
67
|
+
allowPositionals: true,
|
|
68
|
+
options: { base: { type: "string" }, head: { type: "string" }, html: { type: "boolean" }, json: { type: "boolean" }, out: { type: "string" } },
|
|
69
|
+
});
|
|
58
70
|
}
|
|
59
71
|
catch (e) {
|
|
60
72
|
io.stderr(`${messageOf(e)}\n${DIFF_USAGE}\n`);
|
|
61
73
|
return 2;
|
|
62
74
|
}
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
75
|
+
if (parsed.positionals.length === 0) {
|
|
76
|
+
io.stderr(`比べる spec ファイル(またはディレクトリ)を指定してください\n${DIFF_USAGE}\n`);
|
|
77
|
+
return 2;
|
|
78
|
+
}
|
|
79
|
+
if (parsed.values.html && parsed.values.json) {
|
|
80
|
+
io.stderr(`--html と --json は同時に指定できません\n${DIFF_USAGE}\n`);
|
|
66
81
|
return 2;
|
|
67
82
|
}
|
|
83
|
+
let result;
|
|
68
84
|
try {
|
|
69
|
-
|
|
70
|
-
|
|
85
|
+
result = await diffProject({
|
|
86
|
+
paths: parsed.positionals,
|
|
87
|
+
base: parsed.values.base ?? "HEAD",
|
|
88
|
+
...(parsed.values.head === undefined ? {} : { head: parsed.values.head }),
|
|
89
|
+
});
|
|
71
90
|
}
|
|
72
91
|
catch (e) {
|
|
73
92
|
io.stderr(`差分を取れませんでした: ${messageOf(e)}\n`);
|
|
74
93
|
return 1;
|
|
75
94
|
}
|
|
95
|
+
const output = parsed.values.html
|
|
96
|
+
? await renderDiffHtml(result)
|
|
97
|
+
: parsed.values.json
|
|
98
|
+
? JSON.stringify({ base: result.base, head: result.head, objects: result.objects.map((o) => ({ name: o.name, status: o.status, diff: o.diff })) }, null, 2)
|
|
99
|
+
: renderDiffMarkdown(result);
|
|
100
|
+
if (parsed.values.out === undefined)
|
|
101
|
+
io.stdout(output.endsWith("\n") ? output : output + "\n");
|
|
102
|
+
else
|
|
103
|
+
await writeOutput(parsed.values.out, output);
|
|
104
|
+
return 0;
|
|
76
105
|
};
|
|
77
106
|
const SKILLS_USAGE = "usage: live-spec skills install [--dir <path>](既定は .claude/skills) [--force]";
|
|
78
107
|
const runSkills = (argv, io) => {
|
|
108
|
+
if (wantsHelp(argv)) {
|
|
109
|
+
io.stdout(`${SKILLS_USAGE}\n`);
|
|
110
|
+
return 0;
|
|
111
|
+
}
|
|
79
112
|
let parsed;
|
|
80
113
|
try {
|
|
81
114
|
parsed = parseArgs({
|
|
@@ -104,6 +137,10 @@ export const runCli = async (argv, io) => {
|
|
|
104
137
|
return runDiff(argv.slice(1), io);
|
|
105
138
|
if (argv[0] === "skills")
|
|
106
139
|
return runSkills(argv.slice(1), io);
|
|
140
|
+
if (wantsHelp(argv)) {
|
|
141
|
+
io.stdout(`${[USAGE, DIFF_USAGE, SKILLS_USAGE].join("\n")}\n`);
|
|
142
|
+
return 0;
|
|
143
|
+
}
|
|
107
144
|
let options;
|
|
108
145
|
try {
|
|
109
146
|
options = parseCliArgs(argv);
|
|
@@ -171,6 +208,8 @@ export const isMain = (moduleUrl, argv1) => {
|
|
|
171
208
|
}
|
|
172
209
|
};
|
|
173
210
|
if (isMain(import.meta.url, process.argv[1])) {
|
|
211
|
+
// CLI として起動したときだけ登録する。ライブラリとして読み込んだ側(テストランナーなど)の読み込み方には手を出さない。
|
|
212
|
+
registerSpecLoader();
|
|
174
213
|
const code = await runCli(process.argv.slice(2), {
|
|
175
214
|
stdout: (text) => process.stdout.write(text),
|
|
176
215
|
stderr: (text) => process.stderr.write(text),
|
package/dist/diff.d.ts
CHANGED
|
@@ -1,3 +1,55 @@
|
|
|
1
|
+
interface Model {
|
|
2
|
+
readonly actors?: ReadonlyArray<{
|
|
3
|
+
readonly name: string;
|
|
4
|
+
}>;
|
|
5
|
+
readonly inputs?: Readonly<Record<string, ReadonlyArray<string>>>;
|
|
6
|
+
readonly actions?: ReadonlyArray<string | {
|
|
7
|
+
readonly name: string;
|
|
8
|
+
}>;
|
|
9
|
+
readonly states: ReadonlyArray<{
|
|
10
|
+
readonly id: string;
|
|
11
|
+
readonly label: string;
|
|
12
|
+
readonly values?: unknown;
|
|
13
|
+
}>;
|
|
14
|
+
readonly transitions: ReadonlyArray<{
|
|
15
|
+
readonly from: string;
|
|
16
|
+
readonly to: string;
|
|
17
|
+
readonly action: string;
|
|
18
|
+
}>;
|
|
19
|
+
readonly availability: ReadonlyArray<FlatAvailability | CompactAvailability>;
|
|
20
|
+
readonly findings: ReadonlyArray<Readonly<Record<string, unknown>>>;
|
|
21
|
+
readonly surfaces?: ReadonlyArray<{
|
|
22
|
+
readonly state: string;
|
|
23
|
+
readonly actor: string;
|
|
24
|
+
readonly surface: unknown;
|
|
25
|
+
}>;
|
|
26
|
+
readonly scenarios?: ReadonlyArray<{
|
|
27
|
+
readonly name: string;
|
|
28
|
+
readonly passed: boolean;
|
|
29
|
+
}>;
|
|
30
|
+
readonly questions?: ReadonlyArray<{
|
|
31
|
+
readonly ask: string;
|
|
32
|
+
}>;
|
|
33
|
+
}
|
|
34
|
+
interface FlatAvailability {
|
|
35
|
+
readonly state: string;
|
|
36
|
+
readonly action: string;
|
|
37
|
+
readonly actor: string;
|
|
38
|
+
readonly input: string | null;
|
|
39
|
+
readonly enabled: boolean;
|
|
40
|
+
readonly to: string | null;
|
|
41
|
+
readonly reason: string | null;
|
|
42
|
+
}
|
|
43
|
+
interface CompactAvailability {
|
|
44
|
+
readonly state: string;
|
|
45
|
+
readonly action: string;
|
|
46
|
+
readonly outcomes: ReadonlyArray<{
|
|
47
|
+
readonly enabled: boolean;
|
|
48
|
+
readonly to: string | null;
|
|
49
|
+
readonly reason: string | null;
|
|
50
|
+
readonly cases: "*" | Readonly<Record<string, "*" | ReadonlyArray<string>>>;
|
|
51
|
+
}>;
|
|
52
|
+
}
|
|
1
53
|
export interface ModelDiff {
|
|
2
54
|
readonly actions: {
|
|
3
55
|
readonly added: ReadonlyArray<string>;
|
|
@@ -27,7 +79,45 @@ export interface ModelDiff {
|
|
|
27
79
|
readonly state: string;
|
|
28
80
|
readonly actor: string;
|
|
29
81
|
}>;
|
|
82
|
+
readonly scenarios: {
|
|
83
|
+
readonly added: ReadonlyArray<string>;
|
|
84
|
+
readonly removed: ReadonlyArray<string>;
|
|
85
|
+
readonly changed: ReadonlyArray<{
|
|
86
|
+
readonly name: string;
|
|
87
|
+
readonly before: boolean;
|
|
88
|
+
readonly after: boolean;
|
|
89
|
+
}>;
|
|
90
|
+
};
|
|
91
|
+
readonly questions: {
|
|
92
|
+
readonly added: ReadonlyArray<string>;
|
|
93
|
+
readonly removed: ReadonlyArray<string>;
|
|
94
|
+
};
|
|
30
95
|
}
|
|
96
|
+
export declare const EMPTY_MODEL: Model;
|
|
31
97
|
export declare const diffModels: (beforeRaw: unknown, afterRaw: unknown) => ModelDiff;
|
|
32
|
-
export declare const
|
|
33
|
-
|
|
98
|
+
export declare const describeFinding: (f: unknown) => string;
|
|
99
|
+
type AvailabilityChange = ModelDiff["availability"][number];
|
|
100
|
+
export interface ReasonOnlyGroup {
|
|
101
|
+
readonly action: string;
|
|
102
|
+
readonly before: string;
|
|
103
|
+
readonly after: string;
|
|
104
|
+
readonly count: number;
|
|
105
|
+
readonly cases: ReadonlyArray<AvailabilityChange>;
|
|
106
|
+
}
|
|
107
|
+
export declare const groupAvailability: (changes: ReadonlyArray<AvailabilityChange>) => {
|
|
108
|
+
flipped: {
|
|
109
|
+
readonly state: string;
|
|
110
|
+
readonly action: string;
|
|
111
|
+
readonly actor: string;
|
|
112
|
+
readonly input: string | null;
|
|
113
|
+
readonly before: string;
|
|
114
|
+
readonly after: string;
|
|
115
|
+
}[];
|
|
116
|
+
reasonOnly: ReasonOnlyGroup[];
|
|
117
|
+
};
|
|
118
|
+
export declare const whoOf: (a: AvailabilityChange) => string;
|
|
119
|
+
export declare const isEmptyDiff: (d: ModelDiff) => boolean;
|
|
120
|
+
export declare const renderDiff: (d: ModelDiff, options?: {
|
|
121
|
+
readonly heading?: boolean;
|
|
122
|
+
}) => string;
|
|
123
|
+
export {};
|
package/dist/diff.js
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
import { basename, dirname, join, relative, resolve } from "node:path";
|
|
4
|
-
import { generateProject } from "./generate.js";
|
|
5
|
-
const EMPTY = { states: [], transitions: [], availability: [], findings: [], surfaces: [] };
|
|
6
|
-
// 状態 ID(s0, s1…)ではなくラベルで比べる理由: ID は幅優先探索で見つけた順に振られるので、
|
|
1
|
+
export const EMPTY_MODEL = { states: [], transitions: [], availability: [], findings: [], surfaces: [] };
|
|
2
|
+
// 状態 ID(s0, s1…)ではなく状態の値で比べる理由: ID は幅優先探索で見つけた順に振られるので、
|
|
7
3
|
// 状態が 1 つ増えるだけで後ろの ID がずれ、何も変わっていない状態まで「変わった」ことになるため。
|
|
4
|
+
// 名前(label)で比べない理由: 名前は initial に書いた項目の順に並ぶので、書き順を変えただけで変わってしまう。
|
|
5
|
+
// 値を持たない古い形式の JSON だけは名前で比べる。
|
|
6
|
+
const keyOfState = (s) => (s.values === undefined ? s.label : JSON.stringify(s.values));
|
|
8
7
|
const labeler = (model) => {
|
|
9
|
-
const
|
|
10
|
-
return (id) => (id === null ? "" : (
|
|
8
|
+
const keys = new Map(model.states.map((s) => [s.id, keyOfState(s)]));
|
|
9
|
+
return (id) => (id === null ? "" : (keys.get(id) ?? id));
|
|
11
10
|
};
|
|
11
|
+
const ENABLED_PREFIX = "✅ → ";
|
|
12
12
|
const setDiff = (before, after) => ({
|
|
13
13
|
added: after.filter((x) => !before.includes(x)),
|
|
14
14
|
removed: before.filter((x) => !after.includes(x)),
|
|
@@ -36,8 +36,16 @@ export const diffModels = (beforeRaw, afterRaw) => {
|
|
|
36
36
|
const after = afterRaw;
|
|
37
37
|
const lb = labeler(before);
|
|
38
38
|
const la = labeler(after);
|
|
39
|
-
|
|
40
|
-
const
|
|
39
|
+
// 変更後の名前を優先する理由: レビューする人が読むのは変更後の spec なので。
|
|
40
|
+
const names = new Map([...before.states, ...after.states].map((s) => [keyOfState(s), s.label]));
|
|
41
|
+
const show = (key) => names.get(key) ?? key;
|
|
42
|
+
const showCell = (cell) => (cell.startsWith(ENABLED_PREFIX) ? ENABLED_PREFIX + show(cell.slice(ENABLED_PREFIX.length)) : cell);
|
|
43
|
+
const cellOf = (a, l) => (a.enabled ? `${ENABLED_PREFIX}${l(a.to)}` : `❌ ${a.reason ?? "エラー"}`);
|
|
44
|
+
const transitionKeys = (m, l) => m.transitions.map((t) => JSON.stringify([l(t.from), t.action, l(t.to)]));
|
|
45
|
+
const showTransition = (key) => {
|
|
46
|
+
const [from, action, to] = JSON.parse(key);
|
|
47
|
+
return `${show(from)} -${action}-> ${show(to)}`;
|
|
48
|
+
};
|
|
41
49
|
const availabilityOf = (m, l) => new Map(expandAvailability(m).map((a) => [JSON.stringify([l(a.state), a.action, a.actor, a.input]), cellOf(a, l)]));
|
|
42
50
|
const ab = availabilityOf(before, lb);
|
|
43
51
|
const aa = availabilityOf(after, la);
|
|
@@ -47,43 +55,95 @@ export const diffModels = (beforeRaw, afterRaw) => {
|
|
|
47
55
|
if (old === undefined || old === cell)
|
|
48
56
|
continue;
|
|
49
57
|
const [state, action, actor, input] = JSON.parse(key);
|
|
50
|
-
availability.push({ state, action, actor, input, before: old, after: cell });
|
|
58
|
+
availability.push({ state: show(state), action, actor, input, before: showCell(old), after: showCell(cell) });
|
|
51
59
|
}
|
|
52
|
-
|
|
53
|
-
|
|
60
|
+
// 最短の手順(path)を除いて突き合わせる理由: 違反そのものが同じでも、権限を変えると探索の順が変わり、
|
|
61
|
+
// 手順に出てくる人が入れ替わることがある。それを「違反が消えて別の違反が出た」と見せると誤解を招く。
|
|
62
|
+
const findingKeyOf = (f, l) => {
|
|
63
|
+
const { path: _path, ...rest } = f;
|
|
64
|
+
return JSON.stringify(typeof rest.state === "string" ? { ...rest, state: l(rest.state) } : rest);
|
|
65
|
+
};
|
|
66
|
+
const findingsOf = (m, l) => new Map(m.findings.map((f) => [findingKeyOf(f, l), typeof f.state === "string" ? { ...f, state: l(f.state) } : f]));
|
|
67
|
+
const fb = findingsOf(before, lb);
|
|
68
|
+
const fa = findingsOf(after, la);
|
|
69
|
+
const fd = setDiff([...fb.keys()], [...fa.keys()]);
|
|
70
|
+
const showFinding = (f) => f !== undefined && typeof f.state === "string" ? { ...f, state: show(f.state) } : f;
|
|
54
71
|
const surfacesOf = (m, l) => new Map((m.surfaces ?? []).map((s) => [JSON.stringify([l(s.state), s.actor]), JSON.stringify(s.surface)]));
|
|
55
72
|
const sb = surfacesOf(before, lb);
|
|
56
73
|
const sa = surfacesOf(after, la);
|
|
57
74
|
const surfaces = [];
|
|
75
|
+
const afterKeys = new Set(after.states.map(keyOfState));
|
|
58
76
|
for (const key of new Set([...sb.keys(), ...sa.keys()])) {
|
|
59
77
|
if (sb.get(key) === sa.get(key))
|
|
60
78
|
continue;
|
|
61
79
|
const [state, actor] = JSON.parse(key);
|
|
62
|
-
|
|
80
|
+
// 消えた状態の画面は出さない(状態ごと消えたことは「状態」の差分と図でわかる)。増えた状態の画面は、レビューで見たいので出す。
|
|
81
|
+
if (!afterKeys.has(state))
|
|
82
|
+
continue;
|
|
83
|
+
surfaces.push({ state: show(state), actor });
|
|
63
84
|
}
|
|
64
85
|
const actionNames = (m) => (m.actions ?? []).map((a) => (typeof a === "string" ? a : a.name));
|
|
86
|
+
const scenarioNames = (m) => (m.scenarios ?? []).map((s) => s.name);
|
|
87
|
+
const passedOf = (m, name) => m.scenarios?.find((s) => s.name === name)?.passed;
|
|
88
|
+
const sd = setDiff(scenarioNames(before), scenarioNames(after));
|
|
65
89
|
return {
|
|
90
|
+
scenarios: {
|
|
91
|
+
...sd,
|
|
92
|
+
changed: scenarioNames(after)
|
|
93
|
+
.filter((n) => scenarioNames(before).includes(n) && passedOf(before, n) !== passedOf(after, n))
|
|
94
|
+
.map((name) => ({ name, before: passedOf(before, name) ?? false, after: passedOf(after, name) ?? false })),
|
|
95
|
+
},
|
|
96
|
+
questions: setDiff((before.questions ?? []).map((q) => q.ask), (after.questions ?? []).map((q) => q.ask)),
|
|
66
97
|
actions: setDiff(actionNames(before), actionNames(after)),
|
|
67
|
-
states:
|
|
68
|
-
transitions: setDiff(transitionKeys(before, lb), transitionKeys(after, la)),
|
|
98
|
+
states: (({ added, removed }) => ({ added: added.map(show), removed: removed.map(show) }))(setDiff(before.states.map(keyOfState), after.states.map(keyOfState))),
|
|
99
|
+
transitions: (({ added, removed }) => ({ added: added.map(showTransition), removed: removed.map(showTransition) }))(setDiff(transitionKeys(before, lb), transitionKeys(after, la))),
|
|
69
100
|
availability,
|
|
70
|
-
findings: { added: fd.added.map((
|
|
101
|
+
findings: { added: fd.added.map((k) => showFinding(fa.get(k))), removed: fd.removed.map((k) => showFinding(fb.get(k))) },
|
|
71
102
|
surfaces,
|
|
72
103
|
};
|
|
73
104
|
};
|
|
74
|
-
const
|
|
105
|
+
const isStep = (v) => typeof v === "object" && v !== null && "action" in v && "actor" in v;
|
|
106
|
+
export const describeFinding = (f) => {
|
|
75
107
|
const { kind, ...rest } = f;
|
|
76
108
|
return `${String(kind)}: ${Object.entries(rest)
|
|
77
|
-
.map(([k, v]) =>
|
|
109
|
+
.map(([k, v]) => Array.isArray(v) && v.every(isStep)
|
|
110
|
+
? `${k}=${v.map((s) => `${s.action}(${s.actor})`).join(" → ")}`
|
|
111
|
+
: `${k}=${typeof v === "string" ? v : JSON.stringify(v)}`)
|
|
78
112
|
.join(", ")}`;
|
|
79
113
|
};
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
114
|
+
const enabledOf = (cell) => cell.startsWith("✅");
|
|
115
|
+
// できない理由の文言だけが変わったものを、操作 × 変更前 × 変更後でまとめる。権限の条件を 1 つ変えると、
|
|
116
|
+
// 「できない」のままの組み合わせの理由がまとめて書き換わり、本当に変わった行(できる/できないの入れ替わり)が埋もれるため。
|
|
117
|
+
export const groupAvailability = (changes) => {
|
|
118
|
+
const flipped = changes.filter((a) => enabledOf(a.before) || enabledOf(a.after));
|
|
119
|
+
const groups = new Map();
|
|
120
|
+
for (const a of changes.filter((x) => !enabledOf(x.before) && !enabledOf(x.after))) {
|
|
121
|
+
const key = JSON.stringify([a.action, a.before, a.after]);
|
|
122
|
+
groups.set(key, [...(groups.get(key) ?? []), a]);
|
|
123
|
+
}
|
|
124
|
+
const reasonOnly = [...groups.values()].map((cases) => ({
|
|
125
|
+
action: cases[0].action,
|
|
126
|
+
before: cases[0].before,
|
|
127
|
+
after: cases[0].after,
|
|
128
|
+
count: cases.length,
|
|
129
|
+
cases,
|
|
130
|
+
}));
|
|
131
|
+
return { flipped, reasonOnly };
|
|
132
|
+
};
|
|
133
|
+
export const whoOf = (a) => (a.input === null ? a.actor : `${a.actor}(${a.input})`);
|
|
134
|
+
export const isEmptyDiff = (d) => d.actions.added.length + d.actions.removed.length + d.states.added.length + d.states.removed.length +
|
|
135
|
+
d.transitions.added.length + d.transitions.removed.length + d.availability.length +
|
|
136
|
+
d.findings.added.length + d.findings.removed.length + d.surfaces.length +
|
|
137
|
+
d.scenarios.added.length + d.scenarios.removed.length + d.scenarios.changed.length +
|
|
138
|
+
d.questions.added.length + d.questions.removed.length ===
|
|
139
|
+
0;
|
|
140
|
+
const passed = (p) => (p ? "通る" : "止まる");
|
|
141
|
+
// heading を外から切り替えられるようにしている理由: 1 つの spec の差分では「## 振る舞いの変化」を見出しにし、
|
|
142
|
+
// プロジェクト全体の差分ではドメインオブジェクトの見出しの下に節だけを並べたいため。
|
|
143
|
+
export const renderDiff = (d, options = {}) => {
|
|
144
|
+
if (isEmptyDiff(d))
|
|
85
145
|
return "振る舞いの変化はありません。\n";
|
|
86
|
-
const lines = ["## 振る舞いの変化", ""];
|
|
146
|
+
const lines = options.heading === false ? [] : ["## 振る舞いの変化", ""];
|
|
87
147
|
const addRemove = (title, diff, show) => {
|
|
88
148
|
if (diff.added.length + diff.removed.length === 0)
|
|
89
149
|
return;
|
|
@@ -92,50 +152,27 @@ export const renderDiff = (d) => {
|
|
|
92
152
|
addRemove("操作", d.actions, String);
|
|
93
153
|
addRemove("状態", d.states, String);
|
|
94
154
|
addRemove("遷移", d.transitions, String);
|
|
95
|
-
|
|
155
|
+
const g = groupAvailability(d.availability);
|
|
156
|
+
if (g.flipped.length > 0) {
|
|
96
157
|
lines.push("### できる/できないの変化", "", "| 状態 | 操作 | 登場人物 | 変更前 | 変更後 |", "|---|---|---|---|---|");
|
|
97
|
-
for (const a of
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
158
|
+
for (const a of g.flipped)
|
|
159
|
+
lines.push(`| ${a.state} | ${a.action} | ${whoOf(a)} | ${a.before} | ${a.after} |`);
|
|
160
|
+
lines.push("");
|
|
161
|
+
}
|
|
162
|
+
if (g.reasonOnly.length > 0) {
|
|
163
|
+
lines.push("### できない理由の言い方だけが変わったもの", "", "| 操作 | 変更前 | 変更後 | 件数 |", "|---|---|---|---|");
|
|
164
|
+
for (const r of g.reasonOnly)
|
|
165
|
+
lines.push(`| ${r.action} | ${r.before} | ${r.after} | ${r.count} |`);
|
|
101
166
|
lines.push("");
|
|
102
167
|
}
|
|
168
|
+
if (d.scenarios.added.length + d.scenarios.removed.length + d.scenarios.changed.length > 0) {
|
|
169
|
+
lines.push("### シナリオ", "", ...d.scenarios.added.map((n) => `- + ${n}`), ...d.scenarios.removed.map((n) => `- - ${n}`), ...d.scenarios.changed.map((c) => `- ${c.name}: ${passed(c.before)} → ${passed(c.after)}`), "");
|
|
170
|
+
}
|
|
171
|
+
if (d.questions.added.length + d.questions.removed.length > 0) {
|
|
172
|
+
lines.push("### 未決事項", "", ...d.questions.added.map((q) => `- + ${q}`), ...d.questions.removed.map((q) => `- - ${q}(解決したか、取り下げた)`), "");
|
|
173
|
+
}
|
|
103
174
|
addRemove("検査結果", d.findings, describeFinding);
|
|
104
175
|
if (d.surfaces.length > 0)
|
|
105
176
|
lines.push("### 画面が変わったところ", "", ...d.surfaces.map((s) => `- ${s.state}(${s.actor})`), "");
|
|
106
177
|
return lines.join("\n");
|
|
107
178
|
};
|
|
108
|
-
const modelOf = async (path) => {
|
|
109
|
-
const out = await generateProject([path], "json");
|
|
110
|
-
if (out.kind !== "text")
|
|
111
|
-
throw new Error("1 つの spec からは 1 つの JSON が生成されるはず");
|
|
112
|
-
return JSON.parse(out.content);
|
|
113
|
-
};
|
|
114
|
-
export const diffSpec = async (specPath, base = "HEAD") => {
|
|
115
|
-
const spec = resolve(specPath);
|
|
116
|
-
const root = execFileSync("git", ["-C", dirname(spec), "rev-parse", "--show-toplevel"], { encoding: "utf8" }).trim();
|
|
117
|
-
let baseSource;
|
|
118
|
-
try {
|
|
119
|
-
baseSource = execFileSync("git", ["-C", root, "show", `${base}:${relative(root, spec)}`], {
|
|
120
|
-
encoding: "utf8",
|
|
121
|
-
stdio: ["ignore", "pipe", "ignore"],
|
|
122
|
-
});
|
|
123
|
-
}
|
|
124
|
-
catch {
|
|
125
|
-
baseSource = undefined; // base には spec がない(新しく作った spec)
|
|
126
|
-
}
|
|
127
|
-
let before = EMPTY;
|
|
128
|
-
if (baseSource !== undefined) {
|
|
129
|
-
// 元の spec と同じディレクトリに置く理由: spec が相対パスで import しているファイルを、
|
|
130
|
-
// 変更前の spec からも同じように解決できるようにするため。
|
|
131
|
-
const tmp = join(dirname(spec), `.${basename(spec).replace(/\.ts$/, "")}.base.ts`);
|
|
132
|
-
writeFileSync(tmp, baseSource);
|
|
133
|
-
try {
|
|
134
|
-
before = await modelOf(tmp);
|
|
135
|
-
}
|
|
136
|
-
finally {
|
|
137
|
-
rmSync(tmp, { force: true });
|
|
138
|
-
}
|
|
139
|
-
}
|
|
140
|
-
return renderDiff(diffModels(before, await modelOf(spec)));
|
|
141
|
-
};
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import type { Spec } from "./load.js";
|
|
2
|
+
import type { Graph } from "./model.js";
|
|
3
|
+
export interface EnsureScope {
|
|
4
|
+
/** 何によって通る・止まるが決まるか */
|
|
5
|
+
readonly dimension: "state" | "actor" | "input" | "relation" | "mixed" | "none" | "always";
|
|
6
|
+
/** 通るのがどんなときかを、人が理由の文と見比べられる言葉で表したもの */
|
|
7
|
+
readonly passes: string;
|
|
8
|
+
}
|
|
9
|
+
export declare const ensureScope: (spec: Spec, graph: Graph, action: string, reason: string) => EnsureScope;
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
import { withOnlyEnsure } from "./ensure.js";
|
|
2
|
+
import { runAction } from "./explore.js";
|
|
3
|
+
const MAX_STATES = 200;
|
|
4
|
+
const MAX_LISTED = 4;
|
|
5
|
+
const isPlainObject = (u) => typeof u === "object" && u !== null && !Array.isArray(u);
|
|
6
|
+
const same = (a, b) => JSON.stringify(a) === JSON.stringify(b);
|
|
7
|
+
const uniformBy = (cells, keyOf) => {
|
|
8
|
+
const result = new Map();
|
|
9
|
+
for (const c of cells) {
|
|
10
|
+
const known = result.get(keyOf(c));
|
|
11
|
+
if (known !== undefined && known !== c.blocked)
|
|
12
|
+
return null;
|
|
13
|
+
result.set(keyOf(c), c.blocked);
|
|
14
|
+
}
|
|
15
|
+
return result;
|
|
16
|
+
};
|
|
17
|
+
const ordered = (values, preferred) => {
|
|
18
|
+
const list = [...values];
|
|
19
|
+
return preferred === undefined ? list : list.sort((a, b) => preferred.indexOf(a) - preferred.indexOf(b));
|
|
20
|
+
};
|
|
21
|
+
export const ensureScope = (spec, graph, action, reason) => {
|
|
22
|
+
const fn = spec.actions.find(([name]) => name === action)?.[1];
|
|
23
|
+
if (fn === undefined)
|
|
24
|
+
throw new Error(`操作 ${action} は spec にない`);
|
|
25
|
+
const inputs = spec.inputs[action] ?? [[null, []]];
|
|
26
|
+
const cells = [];
|
|
27
|
+
for (const state of graph.states.slice(0, MAX_STATES)) {
|
|
28
|
+
for (const [actor, actorValue] of spec.actors) {
|
|
29
|
+
for (const [input, args] of inputs) {
|
|
30
|
+
const outcome = withOnlyEnsure(reason, () => runAction(fn, state.values, actorValue, args, null));
|
|
31
|
+
cells.push({ state, actor, actorValue, input, blocked: outcome.kind === "not-allowed" && outcome.reason === reason });
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
if (cells.every((c) => !c.blocked))
|
|
36
|
+
return { dimension: "none", passes: "到達できるどの場合も通る(この条件で止まることがない)" };
|
|
37
|
+
if (cells.every((c) => c.blocked))
|
|
38
|
+
return { dimension: "always", passes: "到達できるどの場合も止まる" };
|
|
39
|
+
// 関係を先に調べる理由: 所有者の条件は、到達できる状態で持ち主が 1 人しかいないと「その人のとき」とも読めてしまう。
|
|
40
|
+
// 「持ち主と同じとき」のほうが、理由の文(「作った人だけ」)と見比べやすい。
|
|
41
|
+
// initial に書いた順に試す理由: 到達できる状態で連動している項目(status と dispatch など)は、どちらでも
|
|
42
|
+
// 言い表せる。書き手が先に書いた、読み手に見せたい項目で言い表すほうが、理由の文と見比べやすい。
|
|
43
|
+
const declared = Object.keys(spec.initial);
|
|
44
|
+
const fields = [...new Set([...declared, ...cells.flatMap((c) => Object.keys(c.state.values))])].filter((f) => cells.some((c) => f in c.state.values));
|
|
45
|
+
const actorKeys = [...new Set(cells.flatMap((c) => (isPlainObject(c.actorValue) ? Object.keys(c.actorValue) : [])))];
|
|
46
|
+
for (const field of fields) {
|
|
47
|
+
for (const key of actorKeys) {
|
|
48
|
+
const equal = (c) => isPlainObject(c.actorValue) && same(c.state.values[field], c.actorValue[key]);
|
|
49
|
+
if (!cells.some(equal) || cells.every(equal))
|
|
50
|
+
continue;
|
|
51
|
+
if (cells.every((c) => c.blocked === !equal(c)))
|
|
52
|
+
return { dimension: "relation", passes: `${field} が登場人物の ${key} と同じとき` };
|
|
53
|
+
if (cells.every((c) => c.blocked === equal(c)))
|
|
54
|
+
return { dimension: "relation", passes: `${field} が登場人物の ${key} と違うとき` };
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
const byState = uniformBy(cells, (c) => c.state.id);
|
|
58
|
+
if (byState !== null) {
|
|
59
|
+
const pass = graph.states.filter((s) => byState.get(s.id) === false);
|
|
60
|
+
const block = graph.states.filter((s) => byState.get(s.id) === true);
|
|
61
|
+
for (const field of fields) {
|
|
62
|
+
const passValues = new Set(pass.map((s) => JSON.stringify(s.values[field])));
|
|
63
|
+
if (block.some((s) => passValues.has(JSON.stringify(s.values[field]))))
|
|
64
|
+
continue;
|
|
65
|
+
const values = ordered([...passValues].map((v) => JSON.parse(v)), spec.fields[field]);
|
|
66
|
+
return { dimension: "state", passes: `${field} が ${values.map(String).join(", ")} のとき` };
|
|
67
|
+
}
|
|
68
|
+
return { dimension: "state", passes: `${pass.map((s) => s.label).join(", ")} のとき` };
|
|
69
|
+
}
|
|
70
|
+
const byActor = uniformBy(cells, (c) => c.actor);
|
|
71
|
+
if (byActor !== null) {
|
|
72
|
+
return { dimension: "actor", passes: `${spec.actors.map(([name]) => name).filter((n) => byActor.get(n) === false).join(", ")} のとき` };
|
|
73
|
+
}
|
|
74
|
+
const byInput = uniformBy(cells, (c) => c.input ?? "");
|
|
75
|
+
if (byInput !== null) {
|
|
76
|
+
return { dimension: "input", passes: `入力が ${inputs.map(([label]) => label ?? "").filter((l) => byInput.get(l) === false).join(", ")} のとき` };
|
|
77
|
+
}
|
|
78
|
+
const passing = cells.filter((c) => !c.blocked);
|
|
79
|
+
const listed = passing.slice(0, MAX_LISTED).map((c) => [c.state.label, c.actor, ...(c.input === null ? [] : [c.input])].join("・"));
|
|
80
|
+
return { dimension: "mixed", passes: `${listed.join("、")}${passing.length > MAX_LISTED ? ` ほか ${passing.length - MAX_LISTED} 通り` : ""} のとき` };
|
|
81
|
+
};
|
package/dist/ensure.d.ts
CHANGED
|
@@ -2,4 +2,8 @@ export declare class NotAllowed extends Error {
|
|
|
2
2
|
readonly _tag = "NotAllowed";
|
|
3
3
|
}
|
|
4
4
|
export declare function ensure(condition: unknown, reason: string): asserts condition;
|
|
5
|
+
/** fn を実行している間だけ、理由が reason の ensure を素通りさせる(ミューテーションで使う) */
|
|
6
|
+
export declare const withEnsureBypassed: <A>(reason: string, fn: () => A) => A;
|
|
7
|
+
/** fn を実行している間だけ、理由が reason の ensure 以外を素通りさせる(その ensure が実際に止める範囲を求めるのに使う) */
|
|
8
|
+
export declare const withOnlyEnsure: <A>(reason: string, fn: () => A) => A;
|
|
5
9
|
export declare const isNotAllowed: (u: unknown) => u is NotAllowed;
|
package/dist/ensure.js
CHANGED
|
@@ -1,10 +1,35 @@
|
|
|
1
1
|
export class NotAllowed extends Error {
|
|
2
2
|
_tag = "NotAllowed";
|
|
3
3
|
}
|
|
4
|
+
// 外す ensure を globalThis に Symbol.for で置く理由: spec ファイルと live-spec 本体が別々に読み込まれて
|
|
5
|
+
// このモジュールが 2 つ存在する場合でも、同じ設定を見られるようにするため(isNotAllowed を _tag で判定するのと同じ事情)。
|
|
6
|
+
const BYPASS = Symbol.for("live-spec.ensure.bypass");
|
|
4
7
|
export function ensure(condition, reason) {
|
|
5
|
-
if (
|
|
6
|
-
|
|
8
|
+
if (condition)
|
|
9
|
+
return;
|
|
10
|
+
const bypass = globalThis[BYPASS];
|
|
11
|
+
if (bypass !== undefined && (bypass.mode === "skip" ? bypass.reason === reason : bypass.reason !== reason))
|
|
12
|
+
return;
|
|
13
|
+
throw new NotAllowed(reason);
|
|
7
14
|
}
|
|
15
|
+
const withBypass = (mode, fn) => {
|
|
16
|
+
const holder = globalThis;
|
|
17
|
+
const previous = holder[BYPASS];
|
|
18
|
+
holder[BYPASS] = mode;
|
|
19
|
+
try {
|
|
20
|
+
return fn();
|
|
21
|
+
}
|
|
22
|
+
finally {
|
|
23
|
+
if (previous === undefined)
|
|
24
|
+
delete holder[BYPASS];
|
|
25
|
+
else
|
|
26
|
+
holder[BYPASS] = previous;
|
|
27
|
+
}
|
|
28
|
+
};
|
|
29
|
+
/** fn を実行している間だけ、理由が reason の ensure を素通りさせる(ミューテーションで使う) */
|
|
30
|
+
export const withEnsureBypassed = (reason, fn) => withBypass({ mode: "skip", reason }, fn);
|
|
31
|
+
/** fn を実行している間だけ、理由が reason の ensure 以外を素通りさせる(その ensure が実際に止める範囲を求めるのに使う) */
|
|
32
|
+
export const withOnlyEnsure = (reason, fn) => withBypass({ mode: "only", reason }, fn);
|
|
8
33
|
// instanceof ではなく _tag で判定する理由: spec ファイルと live-spec 本体が別々に
|
|
9
34
|
// 読み込まれて NotAllowed クラスが 2 つ存在する場合でも、正しく判定できるようにするため。
|
|
10
35
|
export const isNotAllowed = (u) => u instanceof Error && u._tag === "NotAllowed";
|