@fluxenginejp/sftest 2.2.0 → 3.0.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.
Files changed (42) hide show
  1. package/{readme.md → README.md} +13 -12
  2. package/dist/cli/sftest.js +6 -6
  3. package/dist/pages/common/RecordFields.d.ts +35 -0
  4. package/dist/pages/common/RecordFields.js +101 -2
  5. package/dist/steps/common/conditional.steps.d.ts +1 -0
  6. package/dist/steps/common/conditional.steps.js +41 -0
  7. package/dist/steps/common/data.steps.js +51 -13
  8. package/dist/steps/common/instance.steps.js +22 -2
  9. package/dist/support/conditionalBlocks.d.ts +110 -0
  10. package/dist/support/conditionalBlocks.js +153 -0
  11. package/dist/support/csvInsert.d.ts +92 -8
  12. package/dist/support/csvInsert.js +359 -247
  13. package/dist/support/dateValues.d.ts +1 -24
  14. package/dist/support/dateValues.js +10 -87
  15. package/dist/support/dmlWrite.d.ts +99 -0
  16. package/dist/support/dmlWrite.js +194 -0
  17. package/dist/support/fixtures.d.ts +27 -0
  18. package/dist/support/fixtures.js +40 -0
  19. package/dist/support/outputPaths.d.ts +0 -8
  20. package/dist/support/outputPaths.js +1 -12
  21. package/dist/support/playwrightConfig.d.ts +0 -5
  22. package/dist/support/playwrightConfig.js +0 -2
  23. package/dist/support/recordCondition.d.ts +13 -2
  24. package/dist/support/recordCondition.js +13 -10
  25. package/dist/support/settings.d.ts +21 -0
  26. package/dist/support/settings.js +38 -2
  27. package/dist/support/sf.d.ts +18 -32
  28. package/dist/support/sf.js +44 -40
  29. package/dist/support/sobjects.d.ts +5 -3
  30. package/dist/support/sobjects.js +7 -16
  31. package/dist/support/soqlQuery.d.ts +45 -32
  32. package/dist/support/soqlQuery.js +88 -116
  33. package/dist/support/standardFunctions.js +7 -0
  34. package/docs/CHANGELOG.md +80 -40
  35. package/docs/EXTENSIONS.md +481 -0
  36. package/docs/SPEC.md +4000 -0
  37. package/package.json +4 -4
  38. package/sftest.config.jsonc +6 -0
  39. package/dist/support/soqlLabels.d.ts +0 -60
  40. package/dist/support/soqlLabels.js +0 -304
  41. package/docs/EXTENSIONS.html +0 -843
  42. package/docs/SPEC.html +0 -9729
@@ -1,12 +1,12 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.SoqlResult = exports.ROW_HEADER = void 0;
3
+ exports.SoqlResult = exports.ROW_HEADER = exports.withDefaultLimit = exports.maskStrings = exports.describeQueryError = void 0;
4
4
  exports.setSoqlRunnerForTest = setSoqlRunnerForTest;
5
5
  exports.setFieldDescriberForTest = setFieldDescriberForTest;
6
6
  exports.setObjectResolverForTest = setObjectResolverForTest;
7
- exports.maskStrings = maskStrings;
8
- exports.withDefaultLimit = withDefaultLimit;
9
- exports.describeQueryError = describeQueryError;
7
+ exports.scenarioSoqlMetadata = scenarioSoqlMetadata;
8
+ exports.scenarioInvalidOnSoqlInput = scenarioInvalidOnSoqlInput;
9
+ exports.prepareScenarioSoql = prepareScenarioSoql;
10
10
  /**
11
11
  * 標準のクラス「SOQL」(シナリオで「インスタンス "…" をクラス "SOQL" から作る」で作る)を提供する(20261021-001)。
12
12
  *
@@ -18,14 +18,21 @@ exports.describeQueryError = describeQueryError;
18
18
  * SOQL は Salesforce CLI でログインしたユーザ(.env の SF_ORG_ALIAS)の接続で実行する(代理ログインしたユーザではない)。
19
19
  * インスタンスはシナリオごとに作られ(support/scenarioFunctions.ts)、別のシナリオに残らない。
20
20
  *
21
+ * SOQL の文字列の処理(コメントの除去、日付の書式、項目ラベルの解決、SELECT * の展開、LIMIT の付与など)は、SOQL Editor と共通の
22
+ * ライブラリ @fluxenginejp/soql-core を使う(20261028-001。SOQL Editor で書いた SOQL を、シナリオに貼り付けても動くようにするため)。
23
+ *
21
24
  * @author Fluxengine.co.jp
22
25
  */
26
+ const soql_core_1 = require("@fluxenginejp/soql-core");
23
27
  const executionControl_1 = require("./executionControl");
24
28
  const sf_1 = require("./sf");
25
29
  const settings_1 = require("./settings");
26
30
  const sobjects_1 = require("./sobjects");
27
- const dateValues_1 = require("./dateValues");
28
- const soqlLabels_1 = require("./soqlLabels");
31
+ // 20261028-001 で共通のライブラリへ移した関数・型。ほかのファイル(support/csvInsert.ts など)は、今までどおりここから読み込める。
32
+ var soql_core_2 = require("@fluxenginejp/soql-core");
33
+ Object.defineProperty(exports, "describeQueryError", { enumerable: true, get: function () { return soql_core_2.describeQueryError; } });
34
+ Object.defineProperty(exports, "maskStrings", { enumerable: true, get: function () { return soql_core_2.maskStrings; } });
35
+ Object.defineProperty(exports, "withDefaultLimit", { enumerable: true, get: function () { return soql_core_2.withDefaultLimit; } });
29
36
  /** SOQL を実行する処理。通常は Salesforce CLI でログインしたユーザの接続で実行する(sfQuery)。 */
30
37
  let runner = sf_1.sfQuery;
31
38
  /**
@@ -58,45 +65,73 @@ let objectResolver = sobjects_1.sobjectApiNameForSoql;
58
65
  function setObjectResolverForTest(replacement) {
59
66
  objectResolver = replacement ?? sobjects_1.sobjectApiNameForSoql;
60
67
  }
68
+ /**
69
+ * オブジェクトの子のリレーションの一覧を返す(describe の childRelationships。SELECT の中の子のリレーションのサブクエリの FROM を解決するために使う)。
70
+ *
71
+ * リレーションの名前がないもの(SOQL のサブクエリに書けないもの)は除く。
72
+ *
73
+ * @param objectApiName - 親のオブジェクトの API 名。
74
+ * @returns 子のリレーションの一覧。
75
+ */
76
+ function childRelationshipsOf(objectApiName) {
77
+ const relationships = (0, sobjects_1.describeSObject)(objectApiName).childRelationships;
78
+ if (!Array.isArray(relationships))
79
+ return [];
80
+ return relationships.filter((r) => typeof r.relationshipName === 'string' && typeof r.childSObject === 'string' && typeof r.field === 'string');
81
+ }
82
+ /**
83
+ * シナリオの SOQL の変換に使うオブジェクト・項目の情報を返す(20261028-001)。
84
+ *
85
+ * 項目の一覧とオブジェクトの照合は、単体の確認で差し替えた処理を使う(setFieldDescriberForTest・setObjectResolverForTest)。
86
+ * describe とオブジェクトの一覧は、テストの実行全体でキャッシュする(support/sobjects.ts)。
87
+ *
88
+ * @returns @fluxenginejp/soql-core の prepareSoql・planDmlWrite に渡す情報。
89
+ */
90
+ function scenarioSoqlMetadata() {
91
+ return {
92
+ fields: (objectApiName) => describer(objectApiName),
93
+ objectApiName: (name) => objectResolver(name),
94
+ childRelationships: (objectApiName) => childRelationshipsOf(objectApiName),
95
+ };
96
+ }
97
+ /**
98
+ * 共通のライブラリが投げた利用者の書き方の誤り(SoqlInputError)を、シナリオ不正にする(20261028-001)。
99
+ *
100
+ * メッセージは、ライブラリのメッセージのまま(先頭に「[シナリオ不正] 」が付く)。元の例外は cause に残す。
101
+ *
102
+ * @param run - ライブラリの処理を呼ぶ処理。
103
+ * @returns run の戻り値。
104
+ * @throws SoqlInputError の場合(シナリオ不正)。そのほかの例外は、そのまま。
105
+ */
106
+ function scenarioInvalidOnSoqlInput(run) {
107
+ try {
108
+ return run();
109
+ }
110
+ catch (error) {
111
+ if (error instanceof soql_core_1.SoqlInputError)
112
+ throw new executionControl_1.ExecutionFailure('scenario-invalid', error.message, { cause: error });
113
+ throw error;
114
+ }
115
+ }
116
+ /**
117
+ * シナリオに書いた SOQL を、組織に送る SOQL にする(標準クラス SOQL のコンストラクタが使う。20261028-001)。
118
+ *
119
+ * コメントと末尾の ; を取り除き、引用符の外の YYYY/MM/DD の日付を YYYY-MM-DD にし、表示ラベル・項目ラベルを API 名にし、
120
+ * SELECT * を主のオブジェクトの全項目にする(@fluxenginejp/soql-core の prepareSoql。SOQL Editor と同じ変換)。
121
+ *
122
+ * @param source - シナリオに書いた SOQL。{名前} は、置き換え済みである。
123
+ * @returns 組織に送る SOQL(LIMIT は付けていない)。
124
+ * @throws コメントと空白だけ、ラベルが見つからない・1つに決まらない場合など(シナリオ不正)。describe を取得できない場合は、その例外のまま。
125
+ */
126
+ function prepareScenarioSoql(source) {
127
+ return scenarioInvalidOnSoqlInput(() => (0, soql_core_1.prepareSoql)(source, scenarioSoqlMetadata()));
128
+ }
61
129
  /** API 名の形(英字で始まり、英数字と _ だけ)。参照先が null の行で、この形の段は describe を問い合わせずに空とする。 */
62
130
  const API_NAME = /^[A-Za-z][A-Za-z0-9_]*$/;
63
131
  /** 集計の結果のオブジェクトの名前(項目の describe がない。別名で書く)。 */
64
132
  const AGGREGATE_RESULT = 'AggregateResult';
65
133
  /** 結果の表で、行を指定する列の見出し。 */
66
134
  exports.ROW_HEADER = '行';
67
- /** 集計関数(集計関数だけの SOQL には LIMIT を付けない)。 */
68
- const AGGREGATE_FUNCTION = /\b(COUNT|COUNT_DISTINCT|SUM|AVG|MIN|MAX)\s*\(/i;
69
- /**
70
- * SOQL の文字列の値('…')を、同じ長さの空白に置き換える(キーワードを探すときに、値の中の語を拾わないため)。
71
- *
72
- * @param soql - SOQL。
73
- * @returns 文字列の値の中身を空白にした SOQL(位置は元の SOQL と同じ)。
74
- */
75
- function maskStrings(soql) {
76
- let out = '';
77
- let inString = false;
78
- for (let i = 0; i < soql.length; i++) {
79
- const c = soql[i];
80
- if (inString) {
81
- if (c === '\\' && i + 1 < soql.length) {
82
- out += ' ';
83
- i++;
84
- continue;
85
- }
86
- if (c === "'") {
87
- inString = false;
88
- out += c;
89
- continue;
90
- }
91
- out += ' ';
92
- continue;
93
- }
94
- if (c === "'")
95
- inString = true;
96
- out += c;
97
- }
98
- return out;
99
- }
100
135
  /**
101
136
  * SOQL の SELECT の項目の並び(SELECT と、括弧の外の最初の FROM の間)を返す。
102
137
  *
@@ -121,28 +156,6 @@ function selectList(masked) {
121
156
  }
122
157
  return undefined;
123
158
  }
124
- /**
125
- * SOQL に LIMIT がない場合に、LIMIT を付ける。
126
- *
127
- * LIMIT は文字列の値の外で探す。OFFSET や FOR(FOR VIEW・FOR REFERENCE・FOR UPDATE)がある場合は、SOQL の文法の順番に合わせてその前に付ける。
128
- * 集計関数だけで GROUP BY のない SOQL(SELECT COUNT() FROM … など)には付けない(数える件数が変わるため)。
129
- *
130
- * @param soql - 利用者が書いた SOQL。
131
- * @param limit - 付ける件数。
132
- * @returns 実行する SOQL と、付けた件数。
133
- */
134
- function withDefaultLimit(soql, limit) {
135
- const masked = maskStrings(soql);
136
- if (/\bLIMIT\b/i.test(masked))
137
- return { soql };
138
- const fields = selectList(masked);
139
- if (fields !== undefined && AGGREGATE_FUNCTION.test(fields) && !/\bGROUP\s+BY\b/i.test(masked))
140
- return { soql };
141
- const tail = /\s+\b(OFFSET|FOR\s+(VIEW|REFERENCE|UPDATE)|UPDATE\s+(TRACKING|VIEWSTAT))\b/i.exec(masked);
142
- if (tail)
143
- return { soql: `${soql.slice(0, tail.index)} LIMIT ${limit}${soql.slice(tail.index)}`, addedLimit: limit };
144
- return { soql: `${soql.replace(/\s+$/, '')} LIMIT ${limit}`, addedLimit: limit };
145
- }
146
159
  /**
147
160
  * SOQL の LIMIT に書かれた件数を返す(上限を超える LIMIT を、実行する前に止めるため)。
148
161
  *
@@ -150,55 +163,9 @@ function withDefaultLimit(soql, limit) {
150
163
  * @returns LIMIT の件数。LIMIT がない、または数値でない(バインド変数など)場合は undefined。
151
164
  */
152
165
  function writtenLimit(soql) {
153
- const match = /\bLIMIT\s+(\d+)\b/i.exec(maskStrings(soql));
166
+ const match = /\bLIMIT\s+(\d+)\b/i.exec((0, soql_core_1.maskStrings)(soql));
154
167
  return match ? Number(match[1]) : undefined;
155
168
  }
156
- /**
157
- * Salesforce が返した SOQL の誤りを、利用者が誤りの箇所を読み取れる形にする。
158
- *
159
- * REST API の誤り([{"message": "…", "errorCode": "…"}] の JSON)からエラーコードとメッセージを取り出す。メッセージには、誤りのある SOQL の行、
160
- * 誤りの位置を指す ^ の行、「ERROR at Row:1:Column:8」(行と列)が改行で入っているため、改行のまま示す。JSON を読めない場合(sf コマンドの
161
- * 誤りなど)は、元のメッセージをそのまま示す。
162
- *
163
- * @param message - API または sf コマンドの誤りのメッセージ。
164
- * @returns 表示する説明(先頭に「(エラーコード):」と改行を含む)。
165
- */
166
- function describeQueryError(message) {
167
- const start = message.indexOf('[{');
168
- if (start >= 0) {
169
- try {
170
- const errors = JSON.parse(message.slice(start));
171
- const lines = errors
172
- .filter((e) => typeof e?.message === 'string')
173
- // エラーコードは見出し(「(INVALID_FIELD)」)に示し、本文の行の先頭には付けない(^ の行が、SOQL の行の誤りの位置を指すようにするため)。
174
- .map((e) => String(e.message).replace(/^\n+/, ''));
175
- const codes = errors.map((e) => e?.errorCode).filter((c) => typeof c === 'string');
176
- if (lines.length) {
177
- const position = /ERROR at Row:(\d+):Column:(\d+)/.exec(lines.join('\n'));
178
- const where = position ? `(${position[1]}行目 ${position[2]}文字目。^ の位置)` : '';
179
- return `(${codes.join('・') || 'エラー'})${where}:\n${lines.join('\n')}`;
180
- }
181
- }
182
- catch {
183
- // API のメッセージは 500 文字で切られる(support/sf.ts)ため、JSON として読めない場合は、message と errorCode の値だけを取り出す。
184
- const text = /"message"\s*:\s*"((?:[^"\\]|\\.)*)/.exec(message.slice(start));
185
- const code = /"errorCode"\s*:\s*"([^"]*)"/.exec(message.slice(start));
186
- if (text) {
187
- let body = text[1];
188
- try {
189
- body = JSON.parse(`"${body}"`);
190
- }
191
- catch {
192
- body = body.replace(/\\n/g, '\n').replace(/\\"/g, '"');
193
- }
194
- const position = /ERROR at Row:(\d+):Column:(\d+)/.exec(body);
195
- const where = position ? `(${position[1]}行目 ${position[2]}文字目。^ の位置)` : '';
196
- return `(${code?.[1] ?? 'エラー'})${where}:\n${body.replace(/^\n+/, '')}`;
197
- }
198
- }
199
- }
200
- return `: ${message}`;
201
- }
202
169
  /**
203
170
  * 保存されている値を、シナリオで比べる文字列にする。
204
171
  *
@@ -233,15 +200,20 @@ class SoqlResult {
233
200
  /**
234
201
  * SOQL を実行し、結果を持つ。現在の行は1行目(0件なら現在の行はない)。
235
202
  *
236
- * 引用符の外の YYYY/MM/DD の日付(TODAY・ADDDAYS の戻り値など)は、SOQL の日付の書式 YYYY-MM-DD にして実行する(20261022-002)。
237
- * SOQL の中の項目ラベル([項目ラベル]、または日本語などを含む語)は、FROM のオブジェクトの項目の API 名にして実行する(20261022-003)。
203
+ * SOQL は prepareScenarioSoql で組織に送る形にする。コメント(// …、/* … *\/)と末尾の ; を取り除き(20261028-001)、
204
+ * 引用符の外の YYYY/MM/DD の日付(TODAY・ADDDAYS の戻り値など)は YYYY-MM-DD にし(20261022-002)、
205
+ * 項目ラベル([項目ラベル]、または日本語などを含む語)は API 名にし(20261022-003)、SELECT * は主のオブジェクトの全項目にする(20261028-001)。
238
206
  *
239
207
  * @param source - 実行する SOQL(SELECT)。{名前} は、ステップを呼ぶ前に置き換え済みである。
240
- * @throws SOQL が空・SELECT でない・上限を超える LIMIT・Salesforce が返した SOQL の誤り・子のリレーションを含む・2,000 行を超える場合(シナリオ不正)。
241
- * Salesforce に接続できない場合(実行時エラー)。
208
+ * @throws SOQL が空・SELECT でない・上限を超える LIMIT・Salesforce が返した SOQL の誤り・子のリレーションを含む・2,000 行を超える場合、
209
+ * 項目ラベルの誤りの場合(シナリオ不正)。Salesforce に接続できない場合(実行時エラー)。
242
210
  */
243
211
  constructor(source) {
244
- const soql = (0, soqlLabels_1.resolveSoqlLabels)((0, dateValues_1.soqlDisplayDatesToIso)(source), undefined, (objectApiName) => describer(objectApiName), (name) => objectResolver(name));
212
+ // データを変える文は、標準のクラス DML で実行する(20261113-004。2026-11-14、PO の決定)。ラベルを解決する前に案内する。
213
+ if ((0, soql_core_1.dmlOperationOf)((0, soql_core_1.toExecutableSoql)(source).trim())) {
214
+ throw new executionControl_1.ExecutionFailure('scenario-invalid', 'SOQL のクラスでは SELECT だけを実行できます。データを変える文(DELETE・UPDATE・UNDELETE・TOUCH)は、標準のクラス DML で実行してください(「インスタンス "名前" を以下の DML で作る」)');
215
+ }
216
+ const soql = prepareScenarioSoql(source);
245
217
  if (!/^\s*SELECT\b/i.test(soql))
246
218
  throw new executionControl_1.ExecutionFailure('scenario-invalid', `SOQL は SELECT で始めてください(指定値: "${soql}")`);
247
219
  // SELECT COUNT() の件数は、結果の行ではなく totalSize に返るため、行として扱えない。別名を付けた COUNT(Id) を使ってもらう。
@@ -249,14 +221,14 @@ class SoqlResult {
249
221
  throw new executionControl_1.ExecutionFailure('scenario-invalid', `SELECT COUNT() には対応していません。件数は「SELECT COUNT(Id) cnt FROM …」のように別名を付けて取り出すか、メソッド「count」を使ってください(指定値: "${soql}")`);
250
222
  }
251
223
  // 子のリレーション(SELECT の中のサブクエリ)は、結果が0件でも分かるよう、実行する前に SOQL で確かめる(WHERE の中のサブクエリは対象外)。
252
- if (/\(\s*SELECT\b/i.test(selectList(maskStrings(soql)) ?? '')) {
224
+ if (/\(\s*SELECT\b/i.test(selectList((0, soql_core_1.maskStrings)(soql)) ?? '')) {
253
225
  throw new executionControl_1.ExecutionFailure('scenario-invalid', `SOQL の子のリレーション(SELECT の中のサブクエリ)には対応していません(指定値: "${soql}")`);
254
226
  }
255
227
  const written = writtenLimit(soql);
256
228
  if (written !== undefined && written > settings_1.MAX_SOQL_ROWS) {
257
229
  throw new executionControl_1.ExecutionFailure('scenario-invalid', `SOQL の LIMIT は ${settings_1.MAX_SOQL_ROWS} 以下にしてください(1つのインスタンスで持てる行は ${settings_1.MAX_SOQL_ROWS} 行まで。指定値: LIMIT ${written})`);
258
230
  }
259
- const limited = withDefaultLimit(soql, settings_1.soqlDefaultLimit);
231
+ const limited = (0, soql_core_1.withDefaultLimit)(soql, settings_1.soqlDefaultLimit);
260
232
  this.executedSoql = limited.soql;
261
233
  this.addedLimit = limited.addedLimit;
262
234
  let records;
@@ -272,7 +244,7 @@ class SoqlResult {
272
244
  if (isQueryError) {
273
245
  // 付けた LIMIT の位置で誤りになることがある(WHERE の途中で終わる SOQL など)。利用者が書いていない LIMIT であることを示す。
274
246
  const note = limited.addedLimit === undefined ? '' : `\n(末尾の LIMIT ${limited.addedLimit} は、SOQL に LIMIT がないため sfTest が付けたものです。誤りの位置が LIMIT の場合は、その前の部分を確かめてください)`;
275
- throw new executionControl_1.ExecutionFailure('scenario-invalid', `SOQL に誤りがあります${describeQueryError(message)}\n実行した SOQL: ${limited.soql}${note}`, { cause: error });
247
+ throw new executionControl_1.ExecutionFailure('scenario-invalid', `SOQL に誤りがあります${(0, soql_core_1.describeQueryError)(message)}\n実行した SOQL: ${limited.soql}${note}`, { cause: error });
276
248
  }
277
249
  throw new executionControl_1.ExecutionFailure('runtime-error', `SOQL を実行できません: ${message}(SOQL: ${limited.soql})`, { cause: error });
278
250
  }
@@ -22,6 +22,7 @@ const executionControl_1 = require("./executionControl");
22
22
  const soqlQuery_1 = require("./soqlQuery");
23
23
  const period_1 = require("./period");
24
24
  const dataSnapshot_1 = require("./dataSnapshot");
25
+ const dmlWrite_1 = require("./dmlWrite");
25
26
  const valueComparison_1 = require("./valueComparison");
26
27
  const runner_1 = require("./runner");
27
28
  /** 日付の書式(YYYY/MM/DD)。 */
@@ -275,6 +276,12 @@ exports.STANDARD_CLASSES = {
275
276
  methods: { count: { method: 'count' }, rowNumber: { method: 'rowNumber' }, next: { method: 'next' }, value: { method: 'field', args: ['項目'] } },
276
277
  default: 'value',
277
278
  },
279
+ // データを変える文(DELETE・UPDATE・UNDELETE・TOUCH)を実行し、件数を持つ(20261113-004)。作った時点で実行する。
280
+ DML: {
281
+ class: dmlWrite_1.DmlResult,
282
+ args: ['DML'],
283
+ methods: { count: { method: 'count' }, succeeded: { method: 'succeeded' }, failed: { method: 'failed' }, ids: { method: 'ids' } },
284
+ },
278
285
  // 締め日で期間を扱う(20261022-004)。長さ・締め日・基準日を渡して名前を付けて作り、基準日を含む期間の開始日・終了日を返す。作った後は変わらない。
279
286
  Period: {
280
287
  class: period_1.Period,