@fluxenginejp/sftest 1.4.1 → 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.
- package/dist/cli/sftest.js +1 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.js +4 -1
- package/dist/pages/common/DataImportWizard.js +4 -2
- package/dist/pages/common/ElementStates.d.ts +1 -1
- package/dist/pages/common/ElementStates.js +1 -1
- package/dist/pages/common/ListViewPage.js +7 -4
- package/dist/pages/common/RecordData.js +1 -1
- package/dist/pages/common/RecordDetailPage.d.ts +4 -3
- package/dist/pages/common/RecordDetailPage.js +11 -6
- package/dist/pages/common/RecordFields.d.ts +54 -13
- package/dist/pages/common/RecordFields.js +124 -34
- package/dist/pages/common/RecordFormDialog.js +1 -1
- package/dist/pages/common/ScreenDialog.d.ts +27 -0
- package/dist/pages/common/ScreenDialog.js +71 -2
- package/dist/pages/common/fieldSections.d.ts +1 -1
- package/dist/pages/common/fieldSections.js +3 -3
- package/dist/steps/common/apex.steps.js +12 -8
- package/dist/steps/common/common.steps.js +7 -7
- package/dist/steps/common/data.steps.d.ts +1 -0
- package/dist/steps/common/data.steps.js +49 -0
- package/dist/steps/common/dialog.steps.js +8 -0
- package/dist/steps/common/file.steps.js +5 -0
- package/dist/steps/common/instance.steps.d.ts +1 -0
- package/dist/steps/common/instance.steps.js +115 -0
- package/dist/steps/common/variable.steps.d.ts +1 -0
- package/dist/steps/common/variable.steps.js +76 -0
- package/dist/support/FileDownloads.d.ts +9 -0
- package/dist/support/FileDownloads.js +12 -0
- package/dist/support/apexBatch.d.ts +11 -0
- package/dist/support/apexBatch.js +6 -4
- package/dist/support/csvInsert.d.ts +77 -0
- package/dist/support/csvInsert.js +446 -0
- package/dist/support/dataRestore.d.ts +201 -0
- package/dist/support/dataRestore.js +449 -0
- package/dist/support/dateValues.d.ts +54 -0
- package/dist/support/dateValues.js +159 -0
- package/dist/support/fixtures.d.ts +83 -0
- package/dist/support/fixtures.js +84 -0
- package/dist/support/globalSetup.js +3 -0
- package/dist/support/metadataCache.d.ts +21 -0
- package/dist/support/metadataCache.js +87 -0
- package/dist/support/outputPaths.d.ts +8 -0
- package/dist/support/outputPaths.js +12 -1
- package/dist/support/period.d.ts +30 -0
- package/dist/support/period.js +191 -0
- package/dist/support/playwrightConfig.d.ts +12 -1
- package/dist/support/playwrightConfig.js +7 -1
- package/dist/support/scenarioFunctions.d.ts +308 -0
- package/dist/support/scenarioFunctions.js +727 -0
- package/dist/support/session.js +3 -0
- package/dist/support/settings.d.ts +44 -3
- package/dist/support/settings.js +100 -13
- package/dist/support/sf.d.ts +56 -1
- package/dist/support/sf.js +169 -11
- package/dist/support/sobjects.d.ts +29 -0
- package/dist/support/sobjects.js +53 -3
- package/dist/support/soqlLabels.d.ts +60 -0
- package/dist/support/soqlLabels.js +304 -0
- package/dist/support/soqlQuery.d.ts +205 -0
- package/dist/support/soqlQuery.js +577 -0
- package/dist/support/standardFunctions.d.ts +76 -0
- package/dist/support/standardFunctions.js +269 -0
- package/dist/support/stepArguments.d.ts +1 -1
- package/dist/support/valueComparison.d.ts +22 -0
- package/dist/support/valueComparison.js +119 -0
- package/dist/support/variables.d.ts +225 -0
- package/dist/support/variables.js +720 -0
- package/docs/CHANGELOG.md +72 -4
- package/docs/EXTENSIONS.html +186 -6
- package/docs/SPEC.html +2368 -1281
- package/package.json +2 -1
- package/readme.md +9 -2
- package/scripts/backup-account.bat +1 -1
- package/sftest.config.jsonc +24 -3
- package/templates/functions/customFunctions.ts +22 -0
- package/templates/playwright.config.ts +3 -0
|
@@ -0,0 +1,449 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.DataRestores = exports.DataRestore = exports.defaultDataApi = exports.DATA_RESTORE_CLASS = void 0;
|
|
4
|
+
exports.decodeLogText = decodeLogText;
|
|
5
|
+
exports.undeleteResults = undeleteResults;
|
|
6
|
+
exports.normalizeDecimal = normalizeDecimal;
|
|
7
|
+
/**
|
|
8
|
+
* データの状態を記憶し、シナリオの終わりに元に戻す(標準のクラス「データの初期化」。20261021-003)。
|
|
9
|
+
*
|
|
10
|
+
* 「"<オブジェクト>" の "<WHERE 句>" の状態を記憶し、シナリオの終わりに戻す」のステップごとに、1つのオブジェクトのインスタンスを作る。
|
|
11
|
+
* 作った時点で、条件に合うレコードの更新できる項目の値を記憶する。シナリオの終わりに(成功・失敗・中断のどれでも)、条件で SOQL を
|
|
12
|
+
* もう一度実行し、次のとおり戻す(2026-10-21、PO の決定)。
|
|
13
|
+
* - 記憶したレコードがごみ箱にある: ごみ箱から復元する(同じ Id で戻る)。
|
|
14
|
+
* - 記憶したレコード(復元したものを含む): 変わった項目だけを、記憶した値に更新する。変わっていないレコードは更新しない。
|
|
15
|
+
* - 記憶していないレコードが結果にあり、記憶した時点より後に作成された: 削除する。
|
|
16
|
+
* - 記憶していないレコードが結果にあり、記憶した時点より前からあった: 元の状態が分からないため触らず、失敗として示す。
|
|
17
|
+
* 複数のインスタンスは、作成されたレコードの削除を後に作ったものから、復元と値の戻しを作った順に行う(DataRestores)。
|
|
18
|
+
*
|
|
19
|
+
* 削除・更新・復元は Salesforce CLI のユーザ(.env の SF_ORG_ALIAS)で行う。記憶した値はメモリだけに持ち、レポートには示さない。
|
|
20
|
+
*
|
|
21
|
+
* @author Fluxengine.co.jp
|
|
22
|
+
*/
|
|
23
|
+
const apexBatch_1 = require("./apexBatch");
|
|
24
|
+
const executionControl_1 = require("./executionControl");
|
|
25
|
+
const settings_1 = require("./settings");
|
|
26
|
+
const sf_1 = require("./sf");
|
|
27
|
+
const sobjects_1 = require("./sobjects");
|
|
28
|
+
const soqlQuery_1 = require("./soqlQuery");
|
|
29
|
+
const dateValues_1 = require("./dateValues");
|
|
30
|
+
const soqlLabels_1 = require("./soqlLabels");
|
|
31
|
+
/** REST API の基準のパス。 */
|
|
32
|
+
const API = '/services/data/v62.0';
|
|
33
|
+
/** 1回の呼び出しで扱うレコードの数(sObject Collections の上限)。 */
|
|
34
|
+
const CHUNK = 200;
|
|
35
|
+
/** Id を並べて SOQL で読む1回の件数(URL が長くなりすぎないため)。 */
|
|
36
|
+
const QUERY_CHUNK = 100;
|
|
37
|
+
/** 記憶しない型(住所・地理位置情報の複合項目そのものと、ファイルの本体)。 */
|
|
38
|
+
const SKIPPED_TYPES = new Set(['address', 'location', 'base64']);
|
|
39
|
+
/** 標準のクラスの名前(レポートとメッセージに使う)。 */
|
|
40
|
+
exports.DATA_RESTORE_CLASS = 'データの初期化';
|
|
41
|
+
/**
|
|
42
|
+
* 配列を、決まった数ずつに分ける。
|
|
43
|
+
*
|
|
44
|
+
* @param items - 分ける配列。
|
|
45
|
+
* @param size - 1つの数。
|
|
46
|
+
* @returns 分けた配列の配列。
|
|
47
|
+
*/
|
|
48
|
+
function chunks(items, size) {
|
|
49
|
+
const result = [];
|
|
50
|
+
for (let i = 0; i < items.length; i += size)
|
|
51
|
+
result.push(items.slice(i, i + size));
|
|
52
|
+
return result;
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* Id の書き方を確かめる(SOQL・URL・Apex に埋め込むため)。
|
|
56
|
+
*
|
|
57
|
+
* @param id - レコードの Id。
|
|
58
|
+
* @returns 同じ Id。
|
|
59
|
+
* @throws Id の形でない場合。
|
|
60
|
+
*/
|
|
61
|
+
function requireId(id) {
|
|
62
|
+
if (!/^[a-zA-Z0-9]{15,18}$/.test(id))
|
|
63
|
+
throw new Error(`レコードの Id の形が正しくありません: ${id}`);
|
|
64
|
+
return id;
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
* sObject Collections の結果を、1件ずつの結果にする。
|
|
68
|
+
*
|
|
69
|
+
* @param ids - 要求した Id(結果と同じ順)。
|
|
70
|
+
* @param response - API の結果([{ id, success, errors: [{ statusCode, message }] }])。
|
|
71
|
+
* @returns 1件ずつの結果。
|
|
72
|
+
*/
|
|
73
|
+
function collectionResults(ids, response) {
|
|
74
|
+
const items = Array.isArray(response) ? response : [];
|
|
75
|
+
return ids.map((id, i) => {
|
|
76
|
+
const item = items[i];
|
|
77
|
+
if (item?.success)
|
|
78
|
+
return { id, success: true };
|
|
79
|
+
const error = (item?.errors ?? []).map((e) => `${e.statusCode ?? 'エラー'}: ${e.message ?? ''}`).join('、') || '結果を受け取れませんでした';
|
|
80
|
+
return { id, success: false, error };
|
|
81
|
+
});
|
|
82
|
+
}
|
|
83
|
+
/**
|
|
84
|
+
* デバッグログの文字参照(|・& など)を、元の文字に戻す。
|
|
85
|
+
*
|
|
86
|
+
* @param text - デバッグログの文字列。
|
|
87
|
+
* @returns 元の文字列。
|
|
88
|
+
*/
|
|
89
|
+
function decodeLogText(text) {
|
|
90
|
+
return text
|
|
91
|
+
.replace(/&#(\d+);/g, (_, code) => String.fromCharCode(Number(code)))
|
|
92
|
+
.replace(/"/g, '"').replace(/</g, '<').replace(/>/g, '>').replace(/&/g, '&');
|
|
93
|
+
}
|
|
94
|
+
/**
|
|
95
|
+
* ごみ箱からの復元の匿名 Apex のデバッグログから、1件ずつの結果を取り出す。
|
|
96
|
+
*
|
|
97
|
+
* @param ids - 復元を求めた Id。
|
|
98
|
+
* @param logs - デバッグログ(USER_DEBUG の行に「SFTEST_UNDELETE:<Id>:<true|false>:<理由>」がある)。
|
|
99
|
+
* @returns 1件ずつの結果。ログに結果がない Id は失敗とする。
|
|
100
|
+
*/
|
|
101
|
+
function undeleteResults(ids, logs) {
|
|
102
|
+
return ids.map((id) => {
|
|
103
|
+
const line = new RegExp(`SFTEST_UNDELETE:${requireId(id)}:(true|false):(.*)`).exec(logs);
|
|
104
|
+
if (!line)
|
|
105
|
+
return { id, success: false, error: '復元の結果を受け取れませんでした' };
|
|
106
|
+
return line[1] === 'true' ? { id, success: true } : { id, success: false, error: decodeLogText(line[2]).trim() };
|
|
107
|
+
});
|
|
108
|
+
}
|
|
109
|
+
/** 組織を操作する既定の処理(Salesforce CLI のユーザで行う)。 */
|
|
110
|
+
exports.defaultDataApi = {
|
|
111
|
+
objectApiName: (labelOrApiName) => (0, sobjects_1.sobjectByLabel)(labelOrApiName).apiName,
|
|
112
|
+
soqlObjectApiName: (labelOrApiName) => (0, sobjects_1.sobjectApiNameForSoql)(labelOrApiName),
|
|
113
|
+
// オブジェクトの情報は、テストの実行全体でキャッシュする(20261021-005)。
|
|
114
|
+
describeFields: (apiName) => (0, sobjects_1.describeSObject)(apiName).fields,
|
|
115
|
+
serverTime: sf_1.sfServerTime,
|
|
116
|
+
// 数値の項目は18桁まであるため、書かれた桁のまま読み、記憶・比較・書き戻しで丸めない(独立レビューの指摘)。
|
|
117
|
+
query: (soql) => (0, sf_1.sfQuery)(soql, true),
|
|
118
|
+
queryAll: (soql) => (0, sf_1.sfQueryAll)(soql),
|
|
119
|
+
deleteRecords: (ids) => chunks(ids, CHUNK).flatMap((part) => collectionResults(part, (0, sf_1.sfRestWrite)('DELETE', `${API}/composite/sobjects?ids=${part.map(requireId).join(',')}&allOrNone=false`))),
|
|
120
|
+
updateRecords: (apiName, records) => chunks(records, CHUNK).flatMap((part) => collectionResults(part.map((r) => String(r.Id)), (0, sf_1.sfRestWrite)('PATCH', `${API}/composite/sobjects`, {
|
|
121
|
+
allOrNone: false,
|
|
122
|
+
records: part.map((r) => ({ attributes: { type: apiName }, ...r })),
|
|
123
|
+
}))),
|
|
124
|
+
undeleteRecords: (ids) => chunks(ids, CHUNK).flatMap((part) => {
|
|
125
|
+
// REST API には復元がないため、Apex の Database.undelete を匿名 Apex で実行し、1件ずつの結果をデバッグログで受け取る。
|
|
126
|
+
// デバッグログでは | が | などの文字参照に置き換わるため、区切りには : を使う(Id と true・false は : を含まない)。
|
|
127
|
+
const code = [
|
|
128
|
+
`List<Id> ids = new List<Id>{${part.map((id) => `'${requireId(id)}'`).join(',')}};`,
|
|
129
|
+
'Database.UndeleteResult[] rs = Database.undelete(ids, false);',
|
|
130
|
+
'for (Integer i = 0; i < rs.size(); i++) {',
|
|
131
|
+
" System.debug('SFTEST_UNDELETE:' + ids[i] + ':' + rs[i].isSuccess() + ':' + (rs[i].isSuccess() ? '' : rs[i].getErrors()[0].getStatusCode() + ': ' + rs[i].getErrors()[0].getMessage().replace('\\n', ' ')));",
|
|
132
|
+
'}',
|
|
133
|
+
].join('\n');
|
|
134
|
+
return undeleteResults(part, String((0, apexBatch_1.runAnonymousApex)(code).logs ?? ''));
|
|
135
|
+
}),
|
|
136
|
+
};
|
|
137
|
+
/** 値を比べるための文字列にする(REST API の値どうしを比べる)。 */
|
|
138
|
+
const comparable = (value) => {
|
|
139
|
+
// 数値は、書かれた桁(RawJsonNumber)を小数の値として正規化して比べる(12.50 と 12.5、1.0E+2 と 100 は同じ値)。
|
|
140
|
+
if ((0, sf_1.isRawJsonNumber)(value))
|
|
141
|
+
return `数値:${normalizeDecimal(value.rawJSON)}`;
|
|
142
|
+
if (typeof value === 'number')
|
|
143
|
+
return `数値:${normalizeDecimal(String(value))}`;
|
|
144
|
+
return JSON.stringify(value ?? null);
|
|
145
|
+
};
|
|
146
|
+
/**
|
|
147
|
+
* JSON の数値の文字列を、値が同じなら同じ文字列になるように正規化する(桁を丸めない)。
|
|
148
|
+
*
|
|
149
|
+
* 指数(1.0E+17)を通常の小数にし、先頭・末尾の余分な 0 と、-0 の符号を除く。
|
|
150
|
+
*
|
|
151
|
+
* @param text - JSON の数値の文字列。
|
|
152
|
+
* @returns 正規化した文字列(例: 12.50 → 12.5、1.0E+2 → 100、-0.0 → 0)。数値の形でない場合は、そのまま返す。
|
|
153
|
+
*/
|
|
154
|
+
function normalizeDecimal(text) {
|
|
155
|
+
const match = /^(-?)(\d+)(?:\.(\d+))?(?:[eE]([+-]?\d+))?$/.exec(text.trim());
|
|
156
|
+
if (!match)
|
|
157
|
+
return text;
|
|
158
|
+
const [, sign, integer, fraction = '', exponent = '0'] = match;
|
|
159
|
+
const digits = integer + fraction;
|
|
160
|
+
const point = integer.length + Number(exponent);
|
|
161
|
+
const padded = point <= 0 ? '0'.repeat(1 - point) + digits : point > digits.length ? digits + '0'.repeat(point - digits.length) : digits;
|
|
162
|
+
const at = Math.max(point, 1);
|
|
163
|
+
const whole = padded.slice(0, at).replace(/^0+(?=\d)/, '');
|
|
164
|
+
const decimals = padded.slice(at).replace(/0+$/, '');
|
|
165
|
+
const result = decimals ? `${whole}.${decimals}` : whole;
|
|
166
|
+
return /^0(\.0*)?$/.test(result) ? '0' : `${sign}${result}`;
|
|
167
|
+
}
|
|
168
|
+
/**
|
|
169
|
+
* 1つのオブジェクトの状態を記憶し、シナリオの終わりに戻す(標準のクラス「データの初期化」のインスタンス)。
|
|
170
|
+
*/
|
|
171
|
+
class DataRestore {
|
|
172
|
+
objectName;
|
|
173
|
+
api;
|
|
174
|
+
options;
|
|
175
|
+
/** オブジェクトの API 名。 */
|
|
176
|
+
apiName;
|
|
177
|
+
/** 記憶した時点(Salesforce の時刻)。 */
|
|
178
|
+
rememberedAt;
|
|
179
|
+
/** 記憶した項目の API 名。 */
|
|
180
|
+
fields;
|
|
181
|
+
/** 記憶したレコード(Id → 項目の値)。 */
|
|
182
|
+
remembered = new Map();
|
|
183
|
+
/** シナリオの終わりの SOQL の結果(削除の段階で取り出す)。 */
|
|
184
|
+
ending;
|
|
185
|
+
/** WHERE 句(引用符の外の YYYY/MM/DD の日付を YYYY-MM-DD にし、項目ラベルを API 名にしたもの)。 */
|
|
186
|
+
where;
|
|
187
|
+
/**
|
|
188
|
+
* 条件に合うレコードを記憶する。
|
|
189
|
+
*
|
|
190
|
+
* @param objectName - オブジェクト(表示ラベルまたは API 名。シナリオに書いたもの)。
|
|
191
|
+
* @param written - WHERE 句(WHERE は書かない)。{名前} は、ステップを呼ぶ前に置き換え済みである。引用符の外の YYYY/MM/DD の日付は、
|
|
192
|
+
* SOQL の日付の書式 YYYY-MM-DD にして使う(20261022-002)。項目ラベル([項目ラベル]、または日本語などを含む語)は、
|
|
193
|
+
* オブジェクトの項目の API 名にして使う(20261022-003)。
|
|
194
|
+
* @param api - 組織の操作(単体の確認で差し替える)。
|
|
195
|
+
* @param options - 記憶の設定(省略時は sftest.config.jsonc の設定)。
|
|
196
|
+
* @throws WHERE 句が空・LIMIT などを含む、オブジェクトがない、SOQL の誤り、件数が上限を超えた場合(シナリオ不正)。
|
|
197
|
+
* Salesforce に接続できない場合(実行時エラー)。
|
|
198
|
+
*/
|
|
199
|
+
constructor(objectName, written, api = exports.defaultDataApi, options = { maxRecords: settings_1.dataRestoreMaxRecords, excludeFields: settings_1.dataRestoreExcludeFields }) {
|
|
200
|
+
this.objectName = objectName;
|
|
201
|
+
this.api = api;
|
|
202
|
+
this.options = options;
|
|
203
|
+
const { maxRecords, excludeFields } = options;
|
|
204
|
+
const dated = (0, dateValues_1.soqlDisplayDatesToIso)(written);
|
|
205
|
+
if (!dated.trim())
|
|
206
|
+
throw new executionControl_1.ExecutionFailure('scenario-invalid', `"${objectName}" のデータを記憶する条件(WHERE 句)が空です。条件を書いてください(オブジェクトのすべてのレコードは対象にできません)`);
|
|
207
|
+
this.apiName = this.wrap(() => api.objectApiName(objectName), 'scenario-invalid');
|
|
208
|
+
// 項目ラベルを API 名にする(誤りはシナリオ不正のまま。describe を取得できない場合は実行時エラー)。WHERE・LIMIT などの確認は、
|
|
209
|
+
// 角括弧の中の文字を誤って拾わないよう、API 名にした後の条件で行う(20261022-003 のレビューの指摘)。
|
|
210
|
+
// 主のオブジェクトはステップのオブジェクト名(画面を持つオブジェクト)、条件のサブクエリの FROM は標準クラス SOQL と同じ照合で決める(20261023-012 のレビューの指摘)。
|
|
211
|
+
const soqlObjectApiName = (o) => (api.soqlObjectApiName ?? sobjects_1.sobjectApiNameForSoql)(o);
|
|
212
|
+
const where = this.wrap(() => (0, soqlLabels_1.resolveSoqlLabels)(dated, this.apiName, (o) => api.describeFields(o), soqlObjectApiName), 'runtime-error');
|
|
213
|
+
if (/^\s*WHERE\b/i.test(where))
|
|
214
|
+
throw new executionControl_1.ExecutionFailure('scenario-invalid', `条件には WHERE を書かず、その後の部分だけを書いてください(指定値: "${dated}")`);
|
|
215
|
+
if (/\b(LIMIT|OFFSET|ORDER\s+BY|GROUP\s+BY)\b/i.test((0, soqlQuery_1.maskStrings)(where))) {
|
|
216
|
+
throw new executionControl_1.ExecutionFailure('scenario-invalid', `条件(WHERE 句)には、LIMIT・OFFSET・ORDER BY・GROUP BY を書けません(指定値: "${dated}")`);
|
|
217
|
+
}
|
|
218
|
+
this.where = where;
|
|
219
|
+
const excluded = new Set([...(excludeFields.get('*') ?? []), ...(excludeFields.get(this.apiName.toLowerCase()) ?? [])]);
|
|
220
|
+
this.fields = this.wrap(() => api.describeFields(this.apiName), 'runtime-error')
|
|
221
|
+
.filter((field) => field.updateable && !SKIPPED_TYPES.has(field.type) && !excluded.has(field.name.toLowerCase()))
|
|
222
|
+
.map((field) => field.name);
|
|
223
|
+
this.rememberedAt = this.wrap(() => api.serverTime(), 'runtime-error');
|
|
224
|
+
const ids = this.queryIds(`SELECT Id FROM ${this.apiName} WHERE ${where} LIMIT ${maxRecords + 1}`).map((r) => String(r.Id));
|
|
225
|
+
if (ids.length > maxRecords) {
|
|
226
|
+
throw new executionControl_1.ExecutionFailure('scenario-invalid', `"${objectName}" の条件に合うレコードが ${maxRecords} 件を超えました。条件を絞ってください(上限は sftest.config.jsonc の dataRestoreMaxRecords。条件: ${where})`);
|
|
227
|
+
}
|
|
228
|
+
for (const [id, record] of this.readValues(ids))
|
|
229
|
+
this.remembered.set(id, record);
|
|
230
|
+
}
|
|
231
|
+
/** 記憶したレコードの数。 */
|
|
232
|
+
get count() {
|
|
233
|
+
return this.remembered.size;
|
|
234
|
+
}
|
|
235
|
+
/** レポートとメッセージに示す、このインスタンスの名前(オブジェクトと条件)。 */
|
|
236
|
+
get label() {
|
|
237
|
+
return `"${this.objectName}"(${this.apiName})の "${this.where}"`;
|
|
238
|
+
}
|
|
239
|
+
/**
|
|
240
|
+
* シナリオの終わりの1段階目: 条件で SOQL をもう一度実行し、記憶した後に作成されたレコードを削除する。
|
|
241
|
+
*
|
|
242
|
+
* @returns 結果。前からあって条件に入ったレコードと、件数の上限を超えた場合は失敗に含める。
|
|
243
|
+
*/
|
|
244
|
+
deleteCreated() {
|
|
245
|
+
const outcome = { lines: [], failures: [] };
|
|
246
|
+
const current = this.queryIds(`SELECT Id, CreatedDate FROM ${this.apiName} WHERE ${this.where} LIMIT ${settings_1.MAX_SOQL_ROWS + 1}`);
|
|
247
|
+
if (current.length > settings_1.MAX_SOQL_ROWS) {
|
|
248
|
+
outcome.failures.push(`${this.label}: シナリオの終わりの条件に合うレコードが ${settings_1.MAX_SOQL_ROWS} 件を超えたため、作成されたレコードを削除していません`);
|
|
249
|
+
}
|
|
250
|
+
const found = new Set(current.map((r) => String(r.Id)));
|
|
251
|
+
const notRemembered = current.filter((r) => !this.remembered.has(String(r.Id)));
|
|
252
|
+
const created = notRemembered.filter((r) => new Date(String(r.CreatedDate)).getTime() >= this.rememberedAt.getTime()).map((r) => String(r.Id));
|
|
253
|
+
const preexisting = notRemembered.filter((r) => !created.includes(String(r.Id))).map((r) => String(r.Id));
|
|
254
|
+
this.ending = { created, preexisting, missing: [...this.remembered.keys()].filter((id) => !found.has(id)) };
|
|
255
|
+
if (preexisting.length) {
|
|
256
|
+
outcome.failures.push(`${this.label}: 記憶した時点より前からあったレコードが、シナリオの中で条件に入りました。元の状態が分からないため、戻していません(${preexisting.join(', ')})。記憶する条件を広げてください`);
|
|
257
|
+
}
|
|
258
|
+
if (current.length > settings_1.MAX_SOQL_ROWS)
|
|
259
|
+
return outcome;
|
|
260
|
+
if (created.length > this.options.maxRecords) {
|
|
261
|
+
outcome.failures.push(`${this.label}: 記憶した後に作成されたレコードが ${this.options.maxRecords} 件を超えたため、削除していません(上限は sftest.config.jsonc の dataRestoreMaxRecords)`);
|
|
262
|
+
return outcome;
|
|
263
|
+
}
|
|
264
|
+
if (created.length)
|
|
265
|
+
this.collect(outcome, '削除(記憶した後に作成)', this.api.deleteRecords(created));
|
|
266
|
+
return outcome;
|
|
267
|
+
}
|
|
268
|
+
/**
|
|
269
|
+
* シナリオの終わりの2段階目: 記憶したレコードのうち、ごみ箱にあるものを復元する。
|
|
270
|
+
*
|
|
271
|
+
* @returns 結果。ごみ箱にない(完全に削除された)レコードと、復元に失敗したレコードは失敗に含める。
|
|
272
|
+
*/
|
|
273
|
+
undeleteDeleted() {
|
|
274
|
+
const outcome = { lines: [], failures: [] };
|
|
275
|
+
const missing = this.ending?.missing ?? [];
|
|
276
|
+
if (!missing.length)
|
|
277
|
+
return outcome;
|
|
278
|
+
const states = new Map();
|
|
279
|
+
for (const part of chunks(missing, QUERY_CHUNK)) {
|
|
280
|
+
for (const r of this.api.queryAll(`SELECT Id, IsDeleted FROM ${this.apiName} WHERE Id IN (${part.map((id) => `'${requireId(id)}'`).join(',')})`)) {
|
|
281
|
+
states.set(String(r.Id), r.IsDeleted === true);
|
|
282
|
+
}
|
|
283
|
+
}
|
|
284
|
+
const deleted = missing.filter((id) => states.get(id) === true);
|
|
285
|
+
const gone = missing.filter((id) => !states.has(id));
|
|
286
|
+
if (gone.length)
|
|
287
|
+
outcome.failures.push(`${this.label}: ごみ箱にないため、復元できません(完全に削除された、または15日を過ぎた): ${gone.join(', ')}`);
|
|
288
|
+
if (deleted.length)
|
|
289
|
+
this.collect(outcome, '復元(ごみ箱から)', this.api.undeleteRecords(deleted));
|
|
290
|
+
return outcome;
|
|
291
|
+
}
|
|
292
|
+
/**
|
|
293
|
+
* シナリオの終わりの3段階目: 記憶したレコードの、変わった項目だけを記憶した値に更新する。
|
|
294
|
+
*
|
|
295
|
+
* @returns 結果。変わっていないレコードは更新せず、件数だけを示す。
|
|
296
|
+
*/
|
|
297
|
+
restoreValues() {
|
|
298
|
+
const outcome = { lines: [], failures: [] };
|
|
299
|
+
if (!this.remembered.size || !this.fields.length)
|
|
300
|
+
return outcome;
|
|
301
|
+
const current = this.readValues([...this.remembered.keys()]);
|
|
302
|
+
const updates = [];
|
|
303
|
+
const changedFields = new Map();
|
|
304
|
+
for (const [id, saved] of this.remembered) {
|
|
305
|
+
const now = current.get(id);
|
|
306
|
+
if (!now)
|
|
307
|
+
continue; // 削除されたまま(復元できなかった)レコードは、2段階目で失敗として示した。
|
|
308
|
+
const changed = this.fields.filter((field) => comparable(now[field]) !== comparable(saved[field]));
|
|
309
|
+
if (!changed.length)
|
|
310
|
+
continue;
|
|
311
|
+
updates.push({ Id: id, ...Object.fromEntries(changed.map((field) => [field, saved[field] ?? null])) });
|
|
312
|
+
changedFields.set(id, changed);
|
|
313
|
+
}
|
|
314
|
+
const unchanged = current.size - updates.length;
|
|
315
|
+
if (unchanged)
|
|
316
|
+
outcome.lines.push(`変更なし: ${unchanged} 件`);
|
|
317
|
+
if (updates.length) {
|
|
318
|
+
const results = this.api.updateRecords(this.apiName, updates);
|
|
319
|
+
this.collect(outcome, '更新(記憶した値に戻す)', results, (id) => `(${(changedFields.get(id) ?? []).join('、')})`);
|
|
320
|
+
}
|
|
321
|
+
return outcome;
|
|
322
|
+
}
|
|
323
|
+
/**
|
|
324
|
+
* Id の一覧のレコードの、記憶する項目の値を読む。
|
|
325
|
+
*
|
|
326
|
+
* @param ids - レコードの Id。
|
|
327
|
+
* @returns Id → 項目の値(今あるレコードだけ)。
|
|
328
|
+
*/
|
|
329
|
+
readValues(ids) {
|
|
330
|
+
const result = new Map();
|
|
331
|
+
const select = ['Id', ...this.fields.filter((field) => field !== 'Id')].join(', ');
|
|
332
|
+
for (const part of chunks(ids, QUERY_CHUNK)) {
|
|
333
|
+
const records = this.wrap(() => this.api.query(`SELECT ${select} FROM ${this.apiName} WHERE Id IN (${part.map((id) => `'${requireId(id)}'`).join(',')})`), 'runtime-error');
|
|
334
|
+
for (const record of records) {
|
|
335
|
+
const { attributes: _attributes, ...values } = record;
|
|
336
|
+
result.set(String(record.Id), values);
|
|
337
|
+
}
|
|
338
|
+
}
|
|
339
|
+
return result;
|
|
340
|
+
}
|
|
341
|
+
/**
|
|
342
|
+
* 条件の SOQL を実行する。SOQL の誤りは、位置を示すシナリオ不正にする。
|
|
343
|
+
*
|
|
344
|
+
* @param soql - 実行する SOQL。
|
|
345
|
+
* @returns レコード。
|
|
346
|
+
* @throws SOQL の誤り(シナリオ不正)、そのほかの失敗(実行時エラー)。
|
|
347
|
+
*/
|
|
348
|
+
queryIds(soql) {
|
|
349
|
+
try {
|
|
350
|
+
return this.api.query(soql);
|
|
351
|
+
}
|
|
352
|
+
catch (error) {
|
|
353
|
+
if ((0, executionControl_1.isExecutionFailure)(error))
|
|
354
|
+
throw error;
|
|
355
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
356
|
+
if (/HTTP 400|MALFORMED_QUERY|INVALID_FIELD|INVALID_TYPE|INVALID_QUERY|INVALID_OPERATION/.test(message)) {
|
|
357
|
+
throw new executionControl_1.ExecutionFailure('scenario-invalid', `${this.label} の条件に誤りがあります${(0, soqlQuery_1.describeQueryError)(message)}\n実行した SOQL: ${soql}`, { cause: error });
|
|
358
|
+
}
|
|
359
|
+
throw new executionControl_1.ExecutionFailure('runtime-error', `${this.label} の SOQL を実行できません: ${message}(SOQL: ${soql})`, { cause: error });
|
|
360
|
+
}
|
|
361
|
+
}
|
|
362
|
+
/**
|
|
363
|
+
* 処理を実行し、ExecutionFailure 以外の例外を、指定した分類の ExecutionFailure にする。
|
|
364
|
+
*
|
|
365
|
+
* @param run - 処理。
|
|
366
|
+
* @param category - 例外の分類。
|
|
367
|
+
* @returns 処理の戻り値。
|
|
368
|
+
*/
|
|
369
|
+
wrap(run, category) {
|
|
370
|
+
try {
|
|
371
|
+
return run();
|
|
372
|
+
}
|
|
373
|
+
catch (error) {
|
|
374
|
+
if ((0, executionControl_1.isExecutionFailure)(error))
|
|
375
|
+
throw error;
|
|
376
|
+
throw new executionControl_1.ExecutionFailure(category, `${exports.DATA_RESTORE_CLASS}("${this.objectName}"): ${error instanceof Error ? error.message : String(error)}`, { cause: error });
|
|
377
|
+
}
|
|
378
|
+
}
|
|
379
|
+
/**
|
|
380
|
+
* 1件ずつの結果を、レポートの行と失敗に分ける。
|
|
381
|
+
*
|
|
382
|
+
* @param outcome - 追加する先。
|
|
383
|
+
* @param action - 処理の名前。
|
|
384
|
+
* @param results - 1件ずつの結果。
|
|
385
|
+
* @param detail - 成功した行に付ける説明(更新した項目の名前など)。
|
|
386
|
+
*/
|
|
387
|
+
collect(outcome, action, results, detail = () => '') {
|
|
388
|
+
const succeeded = results.filter((r) => r.success);
|
|
389
|
+
if (succeeded.length)
|
|
390
|
+
outcome.lines.push(`${action}: ${succeeded.length} 件`, ...succeeded.map((r) => ` ${r.id}${detail(r.id)}`));
|
|
391
|
+
for (const r of results.filter((result) => !result.success))
|
|
392
|
+
outcome.failures.push(`${this.label}: ${action}に失敗しました(${r.id}): ${r.error}`);
|
|
393
|
+
}
|
|
394
|
+
}
|
|
395
|
+
exports.DataRestore = DataRestore;
|
|
396
|
+
/**
|
|
397
|
+
* 1つのシナリオの「データの初期化」のインスタンスを持ち、シナリオの終わりに戻す(support/fixtures.ts の dataRestores)。
|
|
398
|
+
*/
|
|
399
|
+
class DataRestores {
|
|
400
|
+
/** 作った順のインスタンス。 */
|
|
401
|
+
items = [];
|
|
402
|
+
/**
|
|
403
|
+
* インスタンスを加える。
|
|
404
|
+
*
|
|
405
|
+
* @param item - 記憶したインスタンス。
|
|
406
|
+
*/
|
|
407
|
+
add(item) {
|
|
408
|
+
this.items.push(item);
|
|
409
|
+
}
|
|
410
|
+
/** インスタンスがあるか。 */
|
|
411
|
+
get isEmpty() {
|
|
412
|
+
return !this.items.length;
|
|
413
|
+
}
|
|
414
|
+
/**
|
|
415
|
+
* シナリオの終わりに、すべてのインスタンスを戻す。
|
|
416
|
+
*
|
|
417
|
+
* 作成されたレコードの削除を後に作ったものから(子から)、ごみ箱からの復元と値の戻しを作った順に(親から)行う。
|
|
418
|
+
* 1つのインスタンス・段階が失敗しても、残りを続ける。
|
|
419
|
+
*
|
|
420
|
+
* @returns レポートに添付する行と、失敗。
|
|
421
|
+
*/
|
|
422
|
+
restoreAll() {
|
|
423
|
+
const lines = [];
|
|
424
|
+
const failures = [];
|
|
425
|
+
const sections = new Map(this.items.map((item) => [item, []]));
|
|
426
|
+
const run = (item, phase) => {
|
|
427
|
+
try {
|
|
428
|
+
const outcome = phase();
|
|
429
|
+
sections.get(item)?.push(...outcome.lines);
|
|
430
|
+
failures.push(...outcome.failures);
|
|
431
|
+
}
|
|
432
|
+
catch (error) {
|
|
433
|
+
failures.push(`${item.label}: ${error instanceof Error ? error.message : String(error)}`);
|
|
434
|
+
}
|
|
435
|
+
};
|
|
436
|
+
for (const item of [...this.items].reverse())
|
|
437
|
+
run(item, () => item.deleteCreated());
|
|
438
|
+
for (const item of this.items)
|
|
439
|
+
run(item, () => item.undeleteDeleted());
|
|
440
|
+
for (const item of this.items)
|
|
441
|
+
run(item, () => item.restoreValues());
|
|
442
|
+
for (const [item, itemLines] of sections)
|
|
443
|
+
lines.push(`${item.label}(記憶 ${item.count} 件)`, ...(itemLines.length ? itemLines.map((l) => ` ${l}`) : [' 戻したものなし']));
|
|
444
|
+
if (failures.length)
|
|
445
|
+
lines.push('失敗:', ...failures.map((f) => ` ${f}`));
|
|
446
|
+
return { lines, failures };
|
|
447
|
+
}
|
|
448
|
+
}
|
|
449
|
+
exports.DataRestores = DataRestores;
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 値の全体が存在する日付の YYYY-MM-DD(SOQL の結果の書式)なら、画面の書式 YYYY/MM/DD にする。
|
|
3
|
+
*
|
|
4
|
+
* @param value - 値。
|
|
5
|
+
* @returns YYYY/MM/DD。YYYY-MM-DD の日付でなければ undefined。
|
|
6
|
+
*/
|
|
7
|
+
export declare function isoToDisplayDate(value: string): string | undefined;
|
|
8
|
+
/**
|
|
9
|
+
* 値の全体が存在する日付の YYYY/MM/DD(画面の書式・標準の関数の戻り値)なら、YYYY-MM-DD にする。
|
|
10
|
+
*
|
|
11
|
+
* @param value - 値。
|
|
12
|
+
* @returns YYYY-MM-DD。YYYY/MM/DD の日付でなければ undefined。
|
|
13
|
+
*/
|
|
14
|
+
export declare function displayToIsoDate(value: string): string | undefined;
|
|
15
|
+
/**
|
|
16
|
+
* SOQL の中の、引用符の外にある YYYY/MM/DD の日付を、SOQL の日付の書式 YYYY-MM-DD にする。
|
|
17
|
+
*
|
|
18
|
+
* 引用符の外の YYYY/MM/DD は SOQL の文法の誤りになるため、変えても動いている SOQL の意味は変わらない。
|
|
19
|
+
* 引用符 '…' の中(文字列の値。\' は引用符の文字)は変えない。前後が数字・英字・/ に続く並び(日付の一部でないもの)は変えない。
|
|
20
|
+
*
|
|
21
|
+
* @param soql - SOQL。
|
|
22
|
+
* @returns 変えた後の SOQL。変える日付がなければ同じ文字列。
|
|
23
|
+
*/
|
|
24
|
+
export declare function soqlDisplayDatesToIso(soql: string): string;
|
|
25
|
+
/**
|
|
26
|
+
* 画面の URL(/lightning/r/<API 名>/<ID>/view、/lightning/o/<API 名>/list など)から、オブジェクトの API 名を返す。
|
|
27
|
+
*
|
|
28
|
+
* 一覧の行から開いた詳細の URL は /lightning/r/<ID>/view のことがあるため、その場合は ID の先頭3桁からオブジェクトを調べる。
|
|
29
|
+
*
|
|
30
|
+
* @param url - 画面の URL。
|
|
31
|
+
* @param byKeyPrefix - キープレフィックスから API 名を返す処理(単体の確認で差し替える)。
|
|
32
|
+
* @returns オブジェクトの API 名。URL から分からない場合は undefined。
|
|
33
|
+
*/
|
|
34
|
+
export declare function objectApiNameOfUrl(url: string, byKeyPrefix?: (keyPrefix: string) => string | undefined): string | undefined;
|
|
35
|
+
/**
|
|
36
|
+
* オブジェクトの、指定した項目ラベルの項目が日付の項目(describe の type が date)かを返す。
|
|
37
|
+
*
|
|
38
|
+
* 同じラベルの項目が複数ある場合は、すべてが日付の項目のときだけ true とする。項目が見つからない場合、
|
|
39
|
+
* describe を取得できない場合は false とする(変換せず、これまでどおり文字として比べる)。describe は実行全体でキャッシュする。
|
|
40
|
+
*
|
|
41
|
+
* @param objectApiName - オブジェクトの API 名。
|
|
42
|
+
* @param label - 項目ラベル(画面の表示)。
|
|
43
|
+
* @returns 日付の項目なら true。
|
|
44
|
+
*/
|
|
45
|
+
export declare function isDateField(objectApiName: string | undefined, label: string): boolean;
|
|
46
|
+
/**
|
|
47
|
+
* 画面の項目と比べる期待値を、画面の表示の書式にそろえる(期待値が YYYY-MM-DD の日付で、項目が日付の項目のときだけ YYYY/MM/DD にする)。
|
|
48
|
+
*
|
|
49
|
+
* @param url - 画面の URL(オブジェクトの API 名を調べる)。
|
|
50
|
+
* @param label - 項目ラベル。
|
|
51
|
+
* @param expected - 期待値。
|
|
52
|
+
* @returns 比べる値。変えない場合は期待値のまま。
|
|
53
|
+
*/
|
|
54
|
+
export declare function expectedDisplayValue(url: string, label: string, expected: string): string;
|
|
@@ -0,0 +1,159 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.isoToDisplayDate = isoToDisplayDate;
|
|
4
|
+
exports.displayToIsoDate = displayToIsoDate;
|
|
5
|
+
exports.soqlDisplayDatesToIso = soqlDisplayDatesToIso;
|
|
6
|
+
exports.objectApiNameOfUrl = objectApiNameOfUrl;
|
|
7
|
+
exports.isDateField = isDateField;
|
|
8
|
+
exports.expectedDisplayValue = expectedDisplayValue;
|
|
9
|
+
/**
|
|
10
|
+
* 日付の値を、利用先が求める書式に変える(20261022-002)。
|
|
11
|
+
*
|
|
12
|
+
* SOQL の結果の日付は YYYY-MM-DD、画面の入力・表示は YYYY/MM/DD(日本語の利用者)、TODAY・ADDDAYS などの戻り値は YYYY/MM/DD である。
|
|
13
|
+
* 値の文字列を一律に置き換えると通常の文字列を誤って変えるため、sfTest が日付の欄・日付の項目・SOQL の日付の位置だと判定できる
|
|
14
|
+
* 利用先でだけ変える。値の全体が、存在する日付の YYYY-MM-DD・YYYY/MM/DD である場合に限る(日時・空値・ない日付・後ろに文字がある値は変えない)。
|
|
15
|
+
*
|
|
16
|
+
* @author Fluxengine.co.jp
|
|
17
|
+
*/
|
|
18
|
+
const sobjects_1 = require("./sobjects");
|
|
19
|
+
/** 日付の書式(区切りは - か / のどちらかにそろえる)。 */
|
|
20
|
+
const DATE_PATTERN = /^(\d{4})([-/])(\d{2})\2(\d{2})$/;
|
|
21
|
+
/**
|
|
22
|
+
* 値の全体が、存在する日付(区切りは指定のもの)なら、年・月・日を返す。
|
|
23
|
+
*
|
|
24
|
+
* @param value - 値。
|
|
25
|
+
* @param separator - 区切り(- か /)。
|
|
26
|
+
* @returns 年・月・日の文字列。日付でない場合は undefined。
|
|
27
|
+
*/
|
|
28
|
+
function parseDate(value, separator) {
|
|
29
|
+
const match = DATE_PATTERN.exec(value);
|
|
30
|
+
if (!match || match[2] !== separator)
|
|
31
|
+
return undefined;
|
|
32
|
+
const [, year, , month, day] = match;
|
|
33
|
+
const date = new Date(0);
|
|
34
|
+
date.setUTCFullYear(Number(year), Number(month) - 1, Number(day));
|
|
35
|
+
// 繰り越しが起きた場合(2026-02-30 など)は、存在しない日付とする。
|
|
36
|
+
if (date.getUTCFullYear() !== Number(year) || date.getUTCMonth() !== Number(month) - 1 || date.getUTCDate() !== Number(day) || year === '0000')
|
|
37
|
+
return undefined;
|
|
38
|
+
return [year, month, day];
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* 値の全体が存在する日付の YYYY-MM-DD(SOQL の結果の書式)なら、画面の書式 YYYY/MM/DD にする。
|
|
42
|
+
*
|
|
43
|
+
* @param value - 値。
|
|
44
|
+
* @returns YYYY/MM/DD。YYYY-MM-DD の日付でなければ undefined。
|
|
45
|
+
*/
|
|
46
|
+
function isoToDisplayDate(value) {
|
|
47
|
+
return parseDate(value, '-')?.join('/');
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* 値の全体が存在する日付の YYYY/MM/DD(画面の書式・標準の関数の戻り値)なら、YYYY-MM-DD にする。
|
|
51
|
+
*
|
|
52
|
+
* @param value - 値。
|
|
53
|
+
* @returns YYYY-MM-DD。YYYY/MM/DD の日付でなければ undefined。
|
|
54
|
+
*/
|
|
55
|
+
function displayToIsoDate(value) {
|
|
56
|
+
return parseDate(value, '/')?.join('-');
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* SOQL の中の、引用符の外にある YYYY/MM/DD の日付を、SOQL の日付の書式 YYYY-MM-DD にする。
|
|
60
|
+
*
|
|
61
|
+
* 引用符の外の YYYY/MM/DD は SOQL の文法の誤りになるため、変えても動いている SOQL の意味は変わらない。
|
|
62
|
+
* 引用符 '…' の中(文字列の値。\' は引用符の文字)は変えない。前後が数字・英字・/ に続く並び(日付の一部でないもの)は変えない。
|
|
63
|
+
*
|
|
64
|
+
* @param soql - SOQL。
|
|
65
|
+
* @returns 変えた後の SOQL。変える日付がなければ同じ文字列。
|
|
66
|
+
*/
|
|
67
|
+
function soqlDisplayDatesToIso(soql) {
|
|
68
|
+
let out = '';
|
|
69
|
+
let quoted = false;
|
|
70
|
+
for (let i = 0; i < soql.length;) {
|
|
71
|
+
const c = soql[i];
|
|
72
|
+
if (quoted) {
|
|
73
|
+
out += c;
|
|
74
|
+
if (c === '\\') {
|
|
75
|
+
out += soql[i + 1] ?? '';
|
|
76
|
+
i += 2;
|
|
77
|
+
continue;
|
|
78
|
+
}
|
|
79
|
+
if (c === "'")
|
|
80
|
+
quoted = false;
|
|
81
|
+
i++;
|
|
82
|
+
continue;
|
|
83
|
+
}
|
|
84
|
+
if (c === "'") {
|
|
85
|
+
quoted = true;
|
|
86
|
+
out += c;
|
|
87
|
+
i++;
|
|
88
|
+
continue;
|
|
89
|
+
}
|
|
90
|
+
const candidate = soql.slice(i, i + 10);
|
|
91
|
+
const iso = /[\w/]/.test(soql[i - 1] ?? '') || /[\w/]/.test(soql[i + 10] ?? '') ? undefined : displayToIsoDate(candidate);
|
|
92
|
+
if (iso) {
|
|
93
|
+
out += iso;
|
|
94
|
+
i += 10;
|
|
95
|
+
continue;
|
|
96
|
+
}
|
|
97
|
+
out += c;
|
|
98
|
+
i++;
|
|
99
|
+
}
|
|
100
|
+
return out;
|
|
101
|
+
}
|
|
102
|
+
/**
|
|
103
|
+
* 画面の URL(/lightning/r/<API 名>/<ID>/view、/lightning/o/<API 名>/list など)から、オブジェクトの API 名を返す。
|
|
104
|
+
*
|
|
105
|
+
* 一覧の行から開いた詳細の URL は /lightning/r/<ID>/view のことがあるため、その場合は ID の先頭3桁からオブジェクトを調べる。
|
|
106
|
+
*
|
|
107
|
+
* @param url - 画面の URL。
|
|
108
|
+
* @param byKeyPrefix - キープレフィックスから API 名を返す処理(単体の確認で差し替える)。
|
|
109
|
+
* @returns オブジェクトの API 名。URL から分からない場合は undefined。
|
|
110
|
+
*/
|
|
111
|
+
function objectApiNameOfUrl(url, byKeyPrefix = sobjects_1.sobjectApiNameByKeyPrefix) {
|
|
112
|
+
const parts = new URL(url, 'https://localhost').pathname.split('/').filter(Boolean);
|
|
113
|
+
if (parts[0] !== 'lightning' || !['r', 'o'].includes(parts[1] ?? ''))
|
|
114
|
+
return undefined;
|
|
115
|
+
const first = parts[2] ?? '';
|
|
116
|
+
// /lightning/r/<ID>/view(API 名の段がない)。ID は15桁か18桁の英数字。
|
|
117
|
+
if (parts[1] === 'r' && parts[3] === 'view' && /^[a-zA-Z0-9]{15}(?:[a-zA-Z0-9]{3})?$/.test(first)) {
|
|
118
|
+
try {
|
|
119
|
+
return byKeyPrefix(first.slice(0, 3));
|
|
120
|
+
}
|
|
121
|
+
catch {
|
|
122
|
+
return undefined;
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
return /^[A-Za-z]\w*$/.test(first) ? first : undefined;
|
|
126
|
+
}
|
|
127
|
+
/**
|
|
128
|
+
* オブジェクトの、指定した項目ラベルの項目が日付の項目(describe の type が date)かを返す。
|
|
129
|
+
*
|
|
130
|
+
* 同じラベルの項目が複数ある場合は、すべてが日付の項目のときだけ true とする。項目が見つからない場合、
|
|
131
|
+
* describe を取得できない場合は false とする(変換せず、これまでどおり文字として比べる)。describe は実行全体でキャッシュする。
|
|
132
|
+
*
|
|
133
|
+
* @param objectApiName - オブジェクトの API 名。
|
|
134
|
+
* @param label - 項目ラベル(画面の表示)。
|
|
135
|
+
* @returns 日付の項目なら true。
|
|
136
|
+
*/
|
|
137
|
+
function isDateField(objectApiName, label) {
|
|
138
|
+
if (!objectApiName)
|
|
139
|
+
return false;
|
|
140
|
+
try {
|
|
141
|
+
const fields = (0, sobjects_1.describeSObject)(objectApiName).fields.filter((field) => field.label === label);
|
|
142
|
+
return fields.length > 0 && fields.every((field) => field.type === 'date');
|
|
143
|
+
}
|
|
144
|
+
catch {
|
|
145
|
+
return false;
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
/**
|
|
149
|
+
* 画面の項目と比べる期待値を、画面の表示の書式にそろえる(期待値が YYYY-MM-DD の日付で、項目が日付の項目のときだけ YYYY/MM/DD にする)。
|
|
150
|
+
*
|
|
151
|
+
* @param url - 画面の URL(オブジェクトの API 名を調べる)。
|
|
152
|
+
* @param label - 項目ラベル。
|
|
153
|
+
* @param expected - 期待値。
|
|
154
|
+
* @returns 比べる値。変えない場合は期待値のまま。
|
|
155
|
+
*/
|
|
156
|
+
function expectedDisplayValue(url, label, expected) {
|
|
157
|
+
const display = isoToDisplayDate(expected);
|
|
158
|
+
return display && isDateField(objectApiNameOfUrl(url), label) ? display : expected;
|
|
159
|
+
}
|