@fluxenginejp/sftest 1.5.0 → 2.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 (40) hide show
  1. package/dist/pages/common/ListViewPage.js +7 -4
  2. package/dist/pages/common/RecordDetailPage.d.ts +4 -3
  3. package/dist/pages/common/RecordDetailPage.js +11 -6
  4. package/dist/pages/common/RecordFields.d.ts +53 -12
  5. package/dist/pages/common/RecordFields.js +123 -33
  6. package/dist/pages/common/RecordFormDialog.js +1 -1
  7. package/dist/pages/common/fieldSections.d.ts +1 -1
  8. package/dist/pages/common/fieldSections.js +3 -3
  9. package/dist/steps/common/common.steps.js +6 -6
  10. package/dist/steps/common/instance.steps.js +67 -13
  11. package/dist/steps/common/variable.steps.js +4 -4
  12. package/dist/support/apexBatch.js +5 -4
  13. package/dist/support/dataRestore.d.ts +17 -3
  14. package/dist/support/dataRestore.js +20 -8
  15. package/dist/support/dateValues.d.ts +54 -0
  16. package/dist/support/dateValues.js +159 -0
  17. package/dist/support/fixtures.js +20 -4
  18. package/dist/support/period.d.ts +30 -0
  19. package/dist/support/period.js +191 -0
  20. package/dist/support/scenarioFunctions.d.ts +74 -10
  21. package/dist/support/scenarioFunctions.js +155 -16
  22. package/dist/support/settings.d.ts +9 -2
  23. package/dist/support/settings.js +23 -14
  24. package/dist/support/sobjects.d.ts +18 -0
  25. package/dist/support/sobjects.js +38 -0
  26. package/dist/support/soqlLabels.d.ts +60 -0
  27. package/dist/support/soqlLabels.js +304 -0
  28. package/dist/support/soqlQuery.d.ts +16 -6
  29. package/dist/support/soqlQuery.js +25 -8
  30. package/dist/support/standardFunctions.d.ts +32 -6
  31. package/dist/support/standardFunctions.js +102 -28
  32. package/dist/support/variables.d.ts +127 -17
  33. package/dist/support/variables.js +540 -31
  34. package/docs/CHANGELOG.md +52 -18
  35. package/docs/EXTENSIONS.html +30 -29
  36. package/docs/SPEC.html +1319 -1043
  37. package/package.json +2 -1
  38. package/readme.md +2 -2
  39. package/sftest.config.jsonc +10 -5
  40. package/templates/functions/customFunctions.ts +5 -5
@@ -13,8 +13,13 @@ export declare const DEFAULT_TIMEOUTS: {
13
13
  };
14
14
  /** 制限時間の項目名。 */
15
15
  export type TimeoutName = keyof typeof DEFAULT_TIMEOUTS;
16
- /** 項目名の「セクション名」と「項目名」を区切る文字(列)の初期値(20261016-012)。 */
17
- export declare const DEFAULT_SECTION_SEPARATOR = "/";
16
+ /**
17
+ * 項目名の「セクション名」と「項目名」を区切る文字(列)の初期値(20261016-012)。
18
+ * 20261022-001 で "/" から "//" に変えた("/" は項目のラベルによく含まれるため。参照項目の区切りとそろえる)。
19
+ */
20
+ export declare const DEFAULT_SECTION_SEPARATOR = "//";
21
+ /** 参照項目の値の「名前」と「補助情報(検索結果の候補の名前の下の表示)」を区切る文字(列)の初期値(20261022-001)。 */
22
+ export declare const DEFAULT_LOOKUP_SEPARATOR = "//";
18
23
  /** SOQL に LIMIT がないときに付ける件数の初期値(20261021-001)。 */
19
24
  export declare const DEFAULT_SOQL_LIMIT = 10;
20
25
  /** SOQL のクラスの1つのインスタンスで持てる行の上限(20261021-001)。soqlDefaultLimit もこれを超えられない。 */
@@ -46,6 +51,8 @@ export declare function loadDataRestoreExcludeFields(config: Record<string, unkn
46
51
  export declare const timeouts: Record<"apexBatch" | "batchFile" | "customFunction" | "dataImportJob" | "expect" | "expectSlow" | "pageLoad" | "scenario", number>;
47
52
  /** 設定ファイルを反映した、項目名の「セクション名」と「項目名」の区切り(20261016-012)。 */
48
53
  export declare const sectionSeparator: string;
54
+ /** 設定ファイルを反映した、参照項目の値の「名前」と「補助情報」の区切り(20261022-001)。 */
55
+ export declare const lookupSeparator: string;
49
56
  /** 設定ファイルを反映した、SOQL に LIMIT がないときに付ける件数(20261021-001)。 */
50
57
  export declare const soqlDefaultLimit: number;
51
58
  /** 設定ファイルを反映した、データを記憶して戻すステップの、1つのステップで扱うレコードの上限(20261021-003)。 */
@@ -1,10 +1,10 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.timeoutMs = exports.dataRestoreExcludeFields = exports.dataRestoreMaxRecords = exports.soqlDefaultLimit = exports.sectionSeparator = exports.timeouts = exports.DEFAULT_DATA_RESTORE_MAX_RECORDS = exports.MAX_SOQL_ROWS = exports.DEFAULT_SOQL_LIMIT = exports.DEFAULT_SECTION_SEPARATOR = exports.DEFAULT_TIMEOUTS = exports.SETTINGS_FILE = void 0;
3
+ exports.timeoutMs = exports.dataRestoreExcludeFields = exports.dataRestoreMaxRecords = exports.soqlDefaultLimit = exports.lookupSeparator = exports.sectionSeparator = exports.timeouts = exports.DEFAULT_DATA_RESTORE_MAX_RECORDS = exports.MAX_SOQL_ROWS = exports.DEFAULT_SOQL_LIMIT = exports.DEFAULT_LOOKUP_SEPARATOR = exports.DEFAULT_SECTION_SEPARATOR = exports.DEFAULT_TIMEOUTS = exports.SETTINGS_FILE = void 0;
4
4
  exports.loadDataRestoreMaxRecords = loadDataRestoreMaxRecords;
5
5
  exports.loadDataRestoreExcludeFields = loadDataRestoreExcludeFields;
6
6
  /**
7
- * ツールの設定(作業フォルダ直下の sftest.config.jsonc)を読み込み、制限時間と、項目名のセクションの区切りを提供する。
7
+ * ツールの設定(作業フォルダ直下の sftest.config.jsonc)を読み込み、制限時間と、項目名のセクションの区切り・参照項目の値の区切りなどを提供する。
8
8
  *
9
9
  * 設定ファイルはコメント付きの JSON で書く。ファイルがない場合や、項目を書かなかった場合は初期値を使う。
10
10
  * 知らない項目名や、正しくない値はエラーにする(設定誤りを見落として初期値で実行することを防ぐため)。
@@ -26,10 +26,15 @@ exports.DEFAULT_TIMEOUTS = {
26
26
  batchFile: 180, // バッチファイル
27
27
  apexBatch: 180, // Apex バッチの完了待ち
28
28
  dataImportJob: 180, // データインポートのジョブ完了待ち
29
- customFunction: 30, // 利用者の関数(「変数 … に関数 … の結果を設定する」。20261020-001)
29
+ customFunction: 30, // 利用者の関数・メソッド・コンストラクタ({関数名(…)}・「変数 … に関数 … の結果を設定する」など。20261020-001)
30
30
  };
31
- /** 項目名の「セクション名」と「項目名」を区切る文字(列)の初期値(20261016-012)。 */
32
- exports.DEFAULT_SECTION_SEPARATOR = '/';
31
+ /**
32
+ * 項目名の「セクション名」と「項目名」を区切る文字(列)の初期値(20261016-012)。
33
+ * 20261022-001 で "/" から "//" に変えた("/" は項目のラベルによく含まれるため。参照項目の区切りとそろえる)。
34
+ */
35
+ exports.DEFAULT_SECTION_SEPARATOR = '//';
36
+ /** 参照項目の値の「名前」と「補助情報(検索結果の候補の名前の下の表示)」を区切る文字(列)の初期値(20261022-001)。 */
37
+ exports.DEFAULT_LOOKUP_SEPARATOR = '//';
33
38
  /** SOQL に LIMIT がないときに付ける件数の初期値(20261021-001)。 */
34
39
  exports.DEFAULT_SOQL_LIMIT = 10;
35
40
  /** SOQL のクラスの1つのインスタンスで持てる行の上限(20261021-001)。soqlDefaultLimit もこれを超えられない。 */
@@ -37,7 +42,7 @@ exports.MAX_SOQL_ROWS = 2000;
37
42
  /** データを記憶して戻すステップの、1つのステップで扱うレコードの初期値(20261021-003)。 */
38
43
  exports.DEFAULT_DATA_RESTORE_MAX_RECORDS = 200;
39
44
  /** 設定ファイルに書ける項目名。 */
40
- const SETTING_KEYS = ['timeouts', 'sectionSeparator', 'soqlDefaultLimit', 'dataRestoreMaxRecords', 'dataRestoreExcludeFields'];
45
+ const SETTING_KEYS = ['timeouts', 'sectionSeparator', 'lookupSeparator', 'soqlDefaultLimit', 'dataRestoreMaxRecords', 'dataRestoreExcludeFields'];
41
46
  /**
42
47
  * JSONC のコメント(// と /* *\/)と、閉じ括弧の前の余分なカンマを取り除き、JSON として読める文字列にする。
43
48
  *
@@ -133,21 +138,23 @@ function loadTimeouts(config) {
133
138
  return timeouts;
134
139
  }
135
140
  /**
136
- * 設定から、項目名の「セクション名」と「項目名」を区切る文字(列)を読み込む(20261016-012)。
141
+ * 設定から、区切りの文字(列)を読み込む(セクション名の区切りは 20261016-012、参照項目の区切りは 20261022-001)。
137
142
  *
138
- * sectionSeparator を書かない場合は初期値(/)を使う。書く場合は、空でなく、空白だけでもない文字列とする
143
+ * 項目を書かない場合は初期値を使う。書く場合は、空でなく、空白だけでもない文字列とする
139
144
  * (空白の文字列では区切りの位置が分からず、指定の誤りを見落とすため)。null・数値などはエラーにする。
140
145
  *
141
146
  * @param config - 設定ファイルに書かれた項目(readSettings の結果)。
147
+ * @param key - 設定の項目名(sectionSeparator・lookupSeparator)。
148
+ * @param initial - 項目を書かない場合の初期値。
142
149
  * @returns 区切りの文字(列)。
143
- * @throws sectionSeparator が文字列でない場合、空・空白だけの場合。
150
+ * @throws 項目の値が文字列でない場合、空・空白だけの場合。
144
151
  */
145
- function loadSectionSeparator(config) {
146
- const value = config.sectionSeparator;
152
+ function loadSeparator(config, key, initial) {
153
+ const value = config[key];
147
154
  if (value === undefined)
148
- return exports.DEFAULT_SECTION_SEPARATOR;
155
+ return initial;
149
156
  if (typeof value !== 'string' || value.trim() === '') {
150
- throw new executionControl_1.ExecutionFailure('scenario-invalid', `${exports.SETTINGS_FILE} の sectionSeparator は、空でない文字列で書いてください(指定値: ${JSON.stringify(value)})`);
157
+ throw new executionControl_1.ExecutionFailure('scenario-invalid', `${exports.SETTINGS_FILE} の ${key} は、空でない文字列で書いてください(指定値: ${JSON.stringify(value)})`);
151
158
  }
152
159
  return value;
153
160
  }
@@ -219,7 +226,9 @@ const settings = (0, executionControl_1.runRequired)('configuration', readSettin
219
226
  /** 設定ファイルを反映した制限時間(秒)。 */
220
227
  exports.timeouts = (0, executionControl_1.runRequired)('configuration', () => loadTimeouts(settings));
221
228
  /** 設定ファイルを反映した、項目名の「セクション名」と「項目名」の区切り(20261016-012)。 */
222
- exports.sectionSeparator = (0, executionControl_1.runRequired)('configuration', () => loadSectionSeparator(settings));
229
+ exports.sectionSeparator = (0, executionControl_1.runRequired)('configuration', () => loadSeparator(settings, 'sectionSeparator', exports.DEFAULT_SECTION_SEPARATOR));
230
+ /** 設定ファイルを反映した、参照項目の値の「名前」と「補助情報」の区切り(20261022-001)。 */
231
+ exports.lookupSeparator = (0, executionControl_1.runRequired)('configuration', () => loadSeparator(settings, 'lookupSeparator', exports.DEFAULT_LOOKUP_SEPARATOR));
223
232
  /** 設定ファイルを反映した、SOQL に LIMIT がないときに付ける件数(20261021-001)。 */
224
233
  exports.soqlDefaultLimit = (0, executionControl_1.runRequired)('configuration', () => loadSoqlDefaultLimit(settings));
225
234
  /** 設定ファイルを反映した、データを記憶して戻すステップの、1つのステップで扱うレコードの上限(20261021-003)。 */
@@ -20,6 +20,24 @@ export interface SObjectInfo {
20
20
  * @throws オブジェクトが見つからない場合、1つに特定できない場合、レコードの画面を持たない(キープレフィックスがない)場合。
21
21
  */
22
22
  export declare function sobjectByLabel(labelOrApiName: string): SObjectInfo;
23
+ /**
24
+ * SOQL の FROM に書いた表示ラベル(または API 名)から、オブジェクトの API 名を返す(20261023-012)。
25
+ *
26
+ * 照合は sobjectByLabel と同じ(表示ラベル、複数あれば画面を持つものを優先、見つからなければ API 名)。SOQL では画面を持たない
27
+ * オブジェクトも検索できるため、キープレフィックスがないことは誤りにしない。
28
+ *
29
+ * @param labelOrApiName - オブジェクトの表示ラベル、または API 名。
30
+ * @returns オブジェクトの API 名。
31
+ * @throws オブジェクトが見つからない場合、1つに特定できない場合(シナリオ不正)。
32
+ */
33
+ export declare function sobjectApiNameForSoql(labelOrApiName: string): string;
34
+ /**
35
+ * レコード ID の先頭3桁(キープレフィックス)から、オブジェクトの API 名を返す(20261022-002)。
36
+ *
37
+ * @param keyPrefix - レコード ID の先頭3桁。
38
+ * @returns オブジェクトの API 名。見つからない場合は undefined。
39
+ */
40
+ export declare function sobjectApiNameByKeyPrefix(keyPrefix: string): string | undefined;
23
41
  /**
24
42
  * オブジェクトの情報(REST API の describe)を返す。テストの実行全体でキャッシュする(support/metadataCache.ts。20261021-005)。
25
43
  *
@@ -2,6 +2,8 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.RECENT_LIST_LABELS = void 0;
4
4
  exports.sobjectByLabel = sobjectByLabel;
5
+ exports.sobjectApiNameForSoql = sobjectApiNameForSoql;
6
+ exports.sobjectApiNameByKeyPrefix = sobjectApiNameByKeyPrefix;
5
7
  exports.describeSObject = describeSObject;
6
8
  exports.listViewApiName = listViewApiName;
7
9
  /**
@@ -47,6 +49,42 @@ function sobjectByLabel(labelOrApiName) {
47
49
  throw new executionControl_1.ExecutionFailure('scenario-invalid', `オブジェクト "${labelOrApiName}" はレコードの画面を持たないため使えません`);
48
50
  return { apiName: s.name, label: s.label, labelPlural: s.labelPlural, keyPrefix: s.keyPrefix };
49
51
  }
52
+ /**
53
+ * SOQL の FROM に書いた表示ラベル(または API 名)から、オブジェクトの API 名を返す(20261023-012)。
54
+ *
55
+ * 照合は sobjectByLabel と同じ(表示ラベル、複数あれば画面を持つものを優先、見つからなければ API 名)。SOQL では画面を持たない
56
+ * オブジェクトも検索できるため、キープレフィックスがないことは誤りにしない。
57
+ *
58
+ * @param labelOrApiName - オブジェクトの表示ラベル、または API 名。
59
+ * @returns オブジェクトの API 名。
60
+ * @throws オブジェクトが見つからない場合、1つに特定できない場合(シナリオ不正)。
61
+ */
62
+ function sobjectApiNameForSoql(labelOrApiName) {
63
+ const allObjects = (0, metadataCache_1.cachedMetadata)('sobjects', () => (0, sf_1.sfRest)(`${API}/sobjects`).sobjects);
64
+ let candidates = allObjects.filter((s) => s.label === labelOrApiName);
65
+ // 画面を持つものがなければ、すべての候補を示す(見つからないと取り違えないため)。
66
+ if (candidates.length > 1 && candidates.some((s) => s.layoutable))
67
+ candidates = candidates.filter((s) => s.layoutable);
68
+ if (!candidates.length)
69
+ candidates = allObjects.filter((s) => s.name.toLowerCase() === labelOrApiName.toLowerCase());
70
+ if (!candidates.length) {
71
+ throw new executionControl_1.ExecutionFailure('scenario-invalid', `SOQL の FROM のオブジェクト "${labelOrApiName}" が見つかりません(オブジェクトの表示ラベルか API 名で書いてください)`);
72
+ }
73
+ if (candidates.length > 1) {
74
+ throw new executionControl_1.ExecutionFailure('scenario-invalid', `表示ラベルが "${labelOrApiName}" のオブジェクトが複数あります(${candidates.map((s) => s.name).join(', ')})。SOQL の FROM には API 名で書いてください`);
75
+ }
76
+ return candidates[0].name;
77
+ }
78
+ /**
79
+ * レコード ID の先頭3桁(キープレフィックス)から、オブジェクトの API 名を返す(20261022-002)。
80
+ *
81
+ * @param keyPrefix - レコード ID の先頭3桁。
82
+ * @returns オブジェクトの API 名。見つからない場合は undefined。
83
+ */
84
+ function sobjectApiNameByKeyPrefix(keyPrefix) {
85
+ const allObjects = (0, metadataCache_1.cachedMetadata)('sobjects', () => (0, sf_1.sfRest)(`${API}/sobjects`).sobjects);
86
+ return allObjects.find((s) => s.keyPrefix === keyPrefix)?.name;
87
+ }
50
88
  /**
51
89
  * オブジェクトの情報(REST API の describe)を返す。テストの実行全体でキャッシュする(support/metadataCache.ts。20261021-005)。
52
90
  *
@@ -0,0 +1,60 @@
1
+ /** 項目ラベルを API 名に変えるために使う、describe の項目の情報。 */
2
+ export interface LabeledField {
3
+ /** 項目の API 名。 */
4
+ name: string;
5
+ /** 項目ラベル(Salesforce CLI でログインしたユーザの言語)。 */
6
+ label?: string;
7
+ /** 項目の型(参照項目は reference)。 */
8
+ type: string;
9
+ /** 参照項目の、参照先のレコードを指す名前(例: Account__r)。 */
10
+ relationshipName?: string | null;
11
+ /** 参照項目の参照先のオブジェクト。 */
12
+ referenceTo?: string[];
13
+ }
14
+ /** オブジェクトの API 名から、項目の一覧を返す処理。 */
15
+ export type LabeledFieldDescriber = (objectApiName: string) => LabeledField[];
16
+ /**
17
+ * 文字列の値('…')の中身を空白に、角括弧で囲んだ項目ラベル([…])の中身を LABEL_FILLER に置き換える(位置は元の SOQL と同じ)。
18
+ *
19
+ * 文字列の値と角括弧を、左から1回だけ読んで区別する。文字列の値の中の [ ] は項目ラベルとみなさず、角括弧の中の ' ( ) や FROM・SELECT に
20
+ * 見える文字は、文字列・括弧・キーワードとみなさない(20261022-003 のレビューの指摘)。角括弧の中の ]] は ] の文字として、そのまま残す。
21
+ *
22
+ * @param soql - SOQL。
23
+ * @returns 文字列の値と角括弧の中身を置き換えた SOQL。角括弧が閉じていない場合は、残りをすべて LABEL_FILLER にする。
24
+ */
25
+ export declare function maskSoql(soql: string): string;
26
+ /**
27
+ * SOQL の主のオブジェクト(括弧の外の最初の FROM の後の名前)を返す。
28
+ *
29
+ * @param masked - 文字列の値と角括弧の中身を置き換えた SOQL(maskSoql の結果)。
30
+ * @returns オブジェクトの API 名。FROM が見つからない、API 名の形でない場合は undefined。
31
+ */
32
+ export declare function mainObjectOfSoql(masked: string): string | undefined;
33
+ /** オブジェクトの表示ラベル(または API 名)から、API 名を返す処理(20261023-012)。 */
34
+ export type ObjectResolver = (labelOrApiName: string) => string;
35
+ /**
36
+ * SOQL の FROM の後に表示ラベルで書いたオブジェクトを、API 名に変える(20261023-012)。
37
+ *
38
+ * 主の FROM と、サブクエリ((SELECT … FROM …))の FROM の両方を対象とする。オブジェクトは、項目ラベルと同じく、そのまま書くか角括弧で囲む。
39
+ * 囲まない語は、英数字と半角の記号以外の文字(日本語など)を含む場合だけ表示ラベルとみなし、それ以外は API 名としてそのまま残す。
40
+ *
41
+ * @param soql - SOQL。
42
+ * @param resolveObject - 表示ラベルから API 名を返す処理。
43
+ * @returns 変えた後の SOQL。表示ラベルで書いたオブジェクトがなければ同じ文字列。
44
+ * @throws FROM の後の角括弧が閉じていない・空の場合(シナリオ不正)。オブジェクトが見つからない・決まらない場合は、resolveObject の例外のまま。
45
+ */
46
+ export declare function resolveSoqlObjects(soql: string, resolveObject: ObjectResolver): string;
47
+ /**
48
+ * SOQL の中の項目ラベルを API 名に変える。
49
+ *
50
+ * FROM のオブジェクトを表示ラベルで書いた場合は、resolveObject で先に API 名に変えてから、項目ラベルを変える(20261023-012)。
51
+ *
52
+ * @param source - SOQL(インスタンスの SOQL の全体、またはデータの記憶の WHERE 句)。
53
+ * @param objectApiName - 主のオブジェクトの API 名。undefined の場合は、SOQL の FROM から決める。
54
+ * @param describe - オブジェクトの項目の一覧を返す処理(describe)。
55
+ * @param resolveObject - FROM のオブジェクトの表示ラベルから API 名を返す処理。省略した場合、FROM の表示ラベルは変えない。
56
+ * @returns 変えた後の SOQL。項目ラベルがなければ同じ文字列。
57
+ * @throws 角括弧が閉じていない、項目ラベルの項目がない・2つ以上ある、参照先が決まらない、主のオブジェクトが分からない、
58
+ * サブクエリの中に項目ラベルを書いた場合(シナリオ不正)。describe を取得できない場合は、その例外のまま。
59
+ */
60
+ export declare function resolveSoqlLabels(source: string, objectApiName: string | undefined, describe: LabeledFieldDescriber, resolveObject?: ObjectResolver): string;
@@ -0,0 +1,304 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.maskSoql = maskSoql;
4
+ exports.mainObjectOfSoql = mainObjectOfSoql;
5
+ exports.resolveSoqlObjects = resolveSoqlObjects;
6
+ exports.resolveSoqlLabels = resolveSoqlLabels;
7
+ /**
8
+ * SOQL の中に書いた項目ラベルを、項目の API 名に変える(20261022-003)。
9
+ *
10
+ * 項目ラベルは、角括弧で囲む書き方 [項目ラベル] と、囲まない書き方の両方を受け付ける(2026-10-23、PO の決定。案C)。
11
+ * 項目ラベルにはどの文字も使える前提とし、SOQL の文法で意味を持つ文字(LABEL_DELIMITERS)を含むラベルだけを角括弧で囲む。
12
+ * 囲まない語は、英数字と半角の記号以外の文字(日本語など)を含むものだけを項目ラベルとみなし、それ以外は API 名・キーワードとしてそのまま残す。
13
+ * 参照先は . でつなぎ、段ごとに囲める([取引先 ID].[会社名(英)])。文字列の値('…')の中は変えない。
14
+ * 主のオブジェクト(インスタンスの SOQL は FROM の後、データの記憶はステップのオブジェクト)の項目と、その参照先の項目を対象とする。
15
+ * WHERE の中のサブクエリ((SELECT …))の中の項目ラベルは、主のオブジェクトの項目と取り違えないよう、シナリオ不正とする。
16
+ * FROM の後のオブジェクトも、表示ラベルで書ける(主の FROM とサブクエリの FROM。20261023-012)。
17
+ *
18
+ * @author Fluxengine.co.jp
19
+ */
20
+ const executionControl_1 = require("./executionControl");
21
+ /**
22
+ * 囲まない書き方の項目ラベルの終わりになる文字(SOQL の文法で意味を持つ文字)。これらを含むラベルは角括弧で囲む。
23
+ * 半角の空白・タブ・改行(語の区切り)、, (項目の区切り)、( )(関数・括弧)、= < > !(比べ方)、'(文字列)、.(参照先の区切り)、[ ](角括弧そのもの)。
24
+ */
25
+ const LABEL_DELIMITERS = new Set([' ', '\t', '\r', '\n', ',', '(', ')', '=', '<', '>', '!', "'", '.', '[', ']']);
26
+ /** 英数字と半角の記号だけの語(API 名・キーワード・数値・日付など)。これ以外の文字を含む語を、囲まない項目ラベルとみなす。 */
27
+ const ASCII_ONLY = /^[\x20-\x7E]*$/;
28
+ /** 角括弧で囲んだ項目ラベルの中身を置き換える文字(SOQL の文法で意味を持たず、項目ラベルの区切りでもない文字)。 */
29
+ const LABEL_FILLER = '#';
30
+ /**
31
+ * 文字列の値('…')の中身を空白に、角括弧で囲んだ項目ラベル([…])の中身を LABEL_FILLER に置き換える(位置は元の SOQL と同じ)。
32
+ *
33
+ * 文字列の値と角括弧を、左から1回だけ読んで区別する。文字列の値の中の [ ] は項目ラベルとみなさず、角括弧の中の ' ( ) や FROM・SELECT に
34
+ * 見える文字は、文字列・括弧・キーワードとみなさない(20261022-003 のレビューの指摘)。角括弧の中の ]] は ] の文字として、そのまま残す。
35
+ *
36
+ * @param soql - SOQL。
37
+ * @returns 文字列の値と角括弧の中身を置き換えた SOQL。角括弧が閉じていない場合は、残りをすべて LABEL_FILLER にする。
38
+ */
39
+ function maskSoql(soql) {
40
+ let out = '';
41
+ let state = 'code';
42
+ for (let i = 0; i < soql.length; i++) {
43
+ const c = soql[i];
44
+ if (state === 'string') {
45
+ if (c === '\\' && i + 1 < soql.length) {
46
+ out += ' ';
47
+ i++;
48
+ continue;
49
+ }
50
+ if (c === "'") {
51
+ state = 'code';
52
+ out += c;
53
+ continue;
54
+ }
55
+ out += ' ';
56
+ continue;
57
+ }
58
+ if (state === 'label') {
59
+ if (c === ']' && soql[i + 1] === ']') {
60
+ out += ']]';
61
+ i++;
62
+ continue;
63
+ }
64
+ if (c === ']') {
65
+ state = 'code';
66
+ out += c;
67
+ continue;
68
+ }
69
+ out += LABEL_FILLER;
70
+ continue;
71
+ }
72
+ if (c === "'")
73
+ state = 'string';
74
+ else if (c === '[')
75
+ state = 'label';
76
+ out += c;
77
+ }
78
+ return out;
79
+ }
80
+ /**
81
+ * SOQL の主のオブジェクト(括弧の外の最初の FROM の後の名前)を返す。
82
+ *
83
+ * @param masked - 文字列の値と角括弧の中身を置き換えた SOQL(maskSoql の結果)。
84
+ * @returns オブジェクトの API 名。FROM が見つからない、API 名の形でない場合は undefined。
85
+ */
86
+ function mainObjectOfSoql(masked) {
87
+ let depth = 0;
88
+ for (let i = 0; i < masked.length; i++) {
89
+ const c = masked[i];
90
+ if (c === '(')
91
+ depth++;
92
+ else if (c === ')')
93
+ depth--;
94
+ else if (depth === 0 && /^FROM\b/i.test(masked.slice(i)) && /[\s,)]/.test(masked[i - 1] ?? ' ')) {
95
+ return /^FROM\s+([A-Za-z][A-Za-z0-9_]*)(?![^\s,)])/i.exec(masked.slice(i))?.[1];
96
+ }
97
+ }
98
+ return undefined;
99
+ }
100
+ /**
101
+ * SOQL の FROM の後に表示ラベルで書いたオブジェクトを、API 名に変える(20261023-012)。
102
+ *
103
+ * 主の FROM と、サブクエリ((SELECT … FROM …))の FROM の両方を対象とする。オブジェクトは、項目ラベルと同じく、そのまま書くか角括弧で囲む。
104
+ * 囲まない語は、英数字と半角の記号以外の文字(日本語など)を含む場合だけ表示ラベルとみなし、それ以外は API 名としてそのまま残す。
105
+ *
106
+ * @param soql - SOQL。
107
+ * @param resolveObject - 表示ラベルから API 名を返す処理。
108
+ * @returns 変えた後の SOQL。表示ラベルで書いたオブジェクトがなければ同じ文字列。
109
+ * @throws FROM の後の角括弧が閉じていない・空の場合(シナリオ不正)。オブジェクトが見つからない・決まらない場合は、resolveObject の例外のまま。
110
+ */
111
+ function resolveSoqlObjects(soql, resolveObject) {
112
+ const masked = maskSoql(soql);
113
+ let out = '';
114
+ let last = 0;
115
+ for (let i = 0; i < masked.length; i++) {
116
+ if (!/^FROM\b/i.test(masked.slice(i)) || !/[\s,()]/.test(masked[i - 1] ?? ' '))
117
+ continue;
118
+ let start = i + 'FROM'.length;
119
+ while (start < masked.length && /\s/.test(masked[start]))
120
+ start++;
121
+ if (start === i + 'FROM'.length)
122
+ continue;
123
+ let end;
124
+ let text;
125
+ if (masked[start] === '[') {
126
+ text = '';
127
+ let j = start + 1;
128
+ for (; j < masked.length; j++) {
129
+ if (masked[j] !== ']') {
130
+ text += soql[j];
131
+ continue;
132
+ }
133
+ if (masked[j + 1] === ']') {
134
+ text += ']';
135
+ j++;
136
+ continue;
137
+ }
138
+ break;
139
+ }
140
+ if (j >= masked.length)
141
+ throw new executionControl_1.ExecutionFailure('scenario-invalid', `SOQL の FROM の角括弧 [ が閉じていません。表示ラベルの ] は ]] と書きます。SOQL: ${soql}`);
142
+ if (!text.trim())
143
+ throw new executionControl_1.ExecutionFailure('scenario-invalid', `SOQL の FROM の角括弧の中が空です。SOQL: ${soql}`);
144
+ end = j + 1;
145
+ }
146
+ else {
147
+ end = start;
148
+ while (end < masked.length && !LABEL_DELIMITERS.has(masked[end]))
149
+ end++;
150
+ text = soql.slice(start, end);
151
+ if (!text || ASCII_ONLY.test(text))
152
+ continue;
153
+ }
154
+ out += soql.slice(last, start) + resolveObject(text);
155
+ last = end;
156
+ i = end - 1;
157
+ }
158
+ return out + soql.slice(last);
159
+ }
160
+ /**
161
+ * SOQL の中の項目ラベルを API 名に変える。
162
+ *
163
+ * FROM のオブジェクトを表示ラベルで書いた場合は、resolveObject で先に API 名に変えてから、項目ラベルを変える(20261023-012)。
164
+ *
165
+ * @param source - SOQL(インスタンスの SOQL の全体、またはデータの記憶の WHERE 句)。
166
+ * @param objectApiName - 主のオブジェクトの API 名。undefined の場合は、SOQL の FROM から決める。
167
+ * @param describe - オブジェクトの項目の一覧を返す処理(describe)。
168
+ * @param resolveObject - FROM のオブジェクトの表示ラベルから API 名を返す処理。省略した場合、FROM の表示ラベルは変えない。
169
+ * @returns 変えた後の SOQL。項目ラベルがなければ同じ文字列。
170
+ * @throws 角括弧が閉じていない、項目ラベルの項目がない・2つ以上ある、参照先が決まらない、主のオブジェクトが分からない、
171
+ * サブクエリの中に項目ラベルを書いた場合(シナリオ不正)。describe を取得できない場合は、その例外のまま。
172
+ */
173
+ function resolveSoqlLabels(source, objectApiName, describe, resolveObject) {
174
+ const soql = resolveObject ? resolveSoqlObjects(source, resolveObject) : source;
175
+ const masked = maskSoql(soql);
176
+ // 角括弧も、英数字と半角の記号以外の文字もなければ、変えるものはない(よくある API 名だけの SOQL を速く通す)。
177
+ if (!masked.includes('[') && ASCII_ONLY.test(masked))
178
+ return soql;
179
+ const invalid = (reason) => new executionControl_1.ExecutionFailure('scenario-invalid', `SOQL の項目ラベルを API 名にできません(${reason})。SOQL: ${soql}`);
180
+ let out = '';
181
+ let last = 0;
182
+ // 括弧ごとに、サブクエリ(( の直後が SELECT)かを積む。
183
+ const parens = [];
184
+ for (let i = 0; i < masked.length;) {
185
+ const c = masked[i];
186
+ if (c === '(') {
187
+ parens.push(/^\(\s*SELECT\b/i.test(masked.slice(i)));
188
+ i++;
189
+ continue;
190
+ }
191
+ if (c === ')') {
192
+ parens.pop();
193
+ i++;
194
+ continue;
195
+ }
196
+ if (c !== '[' && LABEL_DELIMITERS.has(c)) {
197
+ i++;
198
+ continue;
199
+ }
200
+ // 項目(. でつないだ段の並び)を読む。
201
+ const start = i;
202
+ const segments = [];
203
+ for (;;) {
204
+ if (masked[i] === '[') {
205
+ let text = '';
206
+ let j = i + 1;
207
+ for (; j < masked.length; j++) {
208
+ if (masked[j] !== ']') {
209
+ text += soql[j];
210
+ continue;
211
+ }
212
+ if (masked[j + 1] === ']') {
213
+ text += ']';
214
+ j++;
215
+ continue;
216
+ }
217
+ break;
218
+ }
219
+ if (j >= masked.length)
220
+ throw invalid(`角括弧 [ が閉じていません。項目ラベルの ] は ]] と書きます`);
221
+ if (!text.trim())
222
+ throw invalid('角括弧の中が空です');
223
+ segments.push({ text, label: true });
224
+ i = j + 1;
225
+ }
226
+ else {
227
+ let j = i;
228
+ while (j < masked.length && !LABEL_DELIMITERS.has(masked[j]))
229
+ j++;
230
+ const text = soql.slice(i, j);
231
+ segments.push({ text, label: !ASCII_ONLY.test(text) });
232
+ i = j;
233
+ }
234
+ // . の後に段が続く場合だけ、同じ項目とする(数値の小数点などは、段が英数字のため API 名として残る)。
235
+ if (masked[i] === '.' && i + 1 < masked.length && (masked[i + 1] === '[' || !LABEL_DELIMITERS.has(masked[i + 1]))) {
236
+ i++;
237
+ continue;
238
+ }
239
+ break;
240
+ }
241
+ if (!segments.some((segment) => segment.label))
242
+ continue;
243
+ const written = soql.slice(start, i);
244
+ if (parens.some(Boolean))
245
+ throw invalid(`サブクエリ((SELECT …))の中では項目ラベルを使えません。項目 "${written}" は API 名で書いてください`);
246
+ const object = objectApiName ?? mainObjectOfSoql(masked);
247
+ if (!object)
248
+ throw invalid(`主のオブジェクトが分かりません。FROM の後にオブジェクトの表示ラベルか API 名を書いてください`);
249
+ out += soql.slice(last, start) + resolvePath(object, segments, written, describe, invalid);
250
+ last = i;
251
+ }
252
+ return out + soql.slice(last);
253
+ }
254
+ /**
255
+ * 項目(段の並び)を API 名の並びにする。
256
+ *
257
+ * 途中の段は参照項目(項目ラベル、または末尾の「 ID」を除いたラベル)を探して参照先の名前(relationshipName)にし、
258
+ * 最後の段は項目ラベルの項目の API 名にする。API 名で書いた段はそのまま使い、参照先を describe で調べる。
259
+ *
260
+ * @param object - 主のオブジェクトの API 名。
261
+ * @param segments - 段の並び。
262
+ * @param written - 書いた項目の全体(メッセージに使う)。
263
+ * @param describe - 項目の一覧を返す処理。
264
+ * @param invalid - シナリオ不正の例外を作る関数。
265
+ * @returns API 名の並び(. でつないだもの)。
266
+ */
267
+ function resolvePath(object, segments, written, describe, invalid) {
268
+ const names = [];
269
+ let current = object;
270
+ for (let k = 0; k < segments.length; k++) {
271
+ const { text, label } = segments[k];
272
+ const final = k === segments.length - 1;
273
+ if (!label) {
274
+ names.push(text);
275
+ if (final)
276
+ break;
277
+ // API 名で書いた途中の段は、参照先の名前として参照先のオブジェクトを調べる(後ろの段を項目ラベルで書いた場合に使う)。
278
+ const reference = current ? describe(current).find((f) => f.relationshipName?.toLowerCase() === text.toLowerCase()) : undefined;
279
+ current = reference?.referenceTo?.length === 1 ? reference.referenceTo[0] : undefined;
280
+ continue;
281
+ }
282
+ if (!current)
283
+ throw invalid(`項目 "${written}" の "${text}" の前の参照先のオブジェクトが1つに決まりません。参照先までを API 名で書いてください`);
284
+ const fields = describe(current);
285
+ const matches = final
286
+ ? fields.filter((f) => f.label === text)
287
+ : fields.filter((f) => f.type === 'reference' && !!f.relationshipName && (f.label === text || f.label?.replace(/\s+ID$/, '') === text));
288
+ if (matches.length > 1) {
289
+ const candidates = matches.map((f) => (final ? f.name : f.relationshipName)).join('、');
290
+ throw invalid(`"${current}" に${final ? '' : '参照項目の'}項目ラベルが "${text}" の項目が2つ以上あり、どれか決められません(${candidates})。項目 "${written}" は API 名で書いてください`);
291
+ }
292
+ const found = matches[0];
293
+ if (!found) {
294
+ throw invalid(`"${current}" に${final ? '' : '参照項目の'}項目ラベルが "${text}" の項目がありません。項目ラベル(Salesforce CLI でログインしたユーザの言語の表示ラベル)か API 名で書いてください。空白や , ( ) = < > ! ' . [ ] を含むラベルは [ ] で囲みます`);
295
+ }
296
+ if (final) {
297
+ names.push(found.name);
298
+ break;
299
+ }
300
+ names.push(found.relationshipName);
301
+ current = found.referenceTo?.length === 1 ? found.referenceTo[0] : undefined;
302
+ }
303
+ return names.join('.');
304
+ }
@@ -1,3 +1,4 @@
1
+ import { ObjectResolver } from './soqlLabels';
1
2
  /** SOQL を実行してレコードの配列を返す処理。 */
2
3
  export type SoqlRunner = (soql: string) => unknown[];
3
4
  /**
@@ -27,6 +28,12 @@ export type FieldDescriber = (objectApiName: string) => FieldDescribe[];
27
28
  * @param replacement - 差し替える処理。undefined を渡すと、通常の処理(describe)に戻す。
28
29
  */
29
30
  export declare function setFieldDescriberForTest(replacement?: FieldDescriber): void;
31
+ /**
32
+ * FROM のオブジェクトの表示ラベルから API 名を返す処理を差し替える(組織に接続しない単体の確認で使う。利用者向けではない)。
33
+ *
34
+ * @param replacement - 差し替える処理。undefined を渡すと、通常の処理(sobjectApiNameForSoql)に戻す。
35
+ */
36
+ export declare function setObjectResolverForTest(replacement?: ObjectResolver): void;
30
37
  /** 結果の表で、行を指定する列の見出し。 */
31
38
  export declare const ROW_HEADER = "\u884C";
32
39
  /**
@@ -84,32 +91,35 @@ export declare class SoqlResult {
84
91
  /**
85
92
  * SOQL を実行し、結果を持つ。現在の行は1行目(0件なら現在の行はない)。
86
93
  *
87
- * @param soql - 実行する SOQL(SELECT)。{{名前}} は、ステップを呼ぶ前に置き換え済みである。
94
+ * 引用符の外の YYYY/MM/DD の日付(TODAY・ADDDAYS の戻り値など)は、SOQL の日付の書式 YYYY-MM-DD にして実行する(20261022-002)。
95
+ * SOQL の中の項目ラベル([項目ラベル]、または日本語などを含む語)は、FROM のオブジェクトの項目の API 名にして実行する(20261022-003)。
96
+ *
97
+ * @param source - 実行する SOQL(SELECT)。{名前} は、ステップを呼ぶ前に置き換え済みである。
88
98
  * @throws SOQL が空・SELECT でない・上限を超える LIMIT・Salesforce が返した SOQL の誤り・子のリレーションを含む・2,000 行を超える場合(シナリオ不正)。
89
99
  * Salesforce に接続できない場合(実行時エラー)。
90
100
  */
91
- constructor(soql: string);
101
+ constructor(source: string);
92
102
  /**
93
- * 結果の行数を返す(メソッド「件数」)。
103
+ * 結果の行数を返す(メソッド「count」)。
94
104
  *
95
105
  * @returns 行数。
96
106
  */
97
107
  count(): number;
98
108
  /**
99
- * 現在の行の番号を返す(メソッド「行番号」)。
109
+ * 現在の行の番号を返す(メソッド「rowNumber」)。
100
110
  *
101
111
  * @returns 1 から始まる番号。0件の場合は 0。
102
112
  */
103
113
  rowNumber(): number;
104
114
  /**
105
- * 現在の行を次の行へ移す(メソッド「次」)。
115
+ * 現在の行を次の行へ移す(メソッド「next」)。
106
116
  *
107
117
  * @returns 移った後の行番号。
108
118
  * @throws 現在の行が最後の行(または0件)の場合(シナリオ不正)。
109
119
  */
110
120
  next(): number;
111
121
  /**
112
- * 現在の行の項目の値を返す(既定のメソッド「値」。{{インスタンス名::項目}}。20261021-002)。
122
+ * 現在の行の項目の値を返す(既定のメソッド「value」。{インスタンス名::項目}。20261021-002)。
113
123
  *
114
124
  * @param field - 項目の API 名(参照先は Account.Name のように . でつなぐ。大文字・小文字は区別しない)。
115
125
  * @returns 値(保存されている値を文字列にしたもの)。