1
0

test-bootstrap-caching.mjs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. import fs from 'fs';
  2. import { pathToFileURL } from 'url';
  3. const [, , pluginPath, scenario] = process.argv;
  4. if (!pluginPath || !['present', 'missing'].includes(scenario)) {
  5. console.error('Usage: node test-bootstrap-caching.mjs PLUGIN_PATH present|missing');
  6. process.exit(2);
  7. }
  8. let existsCount = 0;
  9. let readCount = 0;
  10. const originalExistsSync = fs.existsSync;
  11. const originalReadFileSync = fs.readFileSync;
  12. fs.existsSync = function (...args) {
  13. if (isBootstrapSkillPath(args[0])) {
  14. existsCount += 1;
  15. }
  16. return originalExistsSync.apply(this, args);
  17. };
  18. fs.readFileSync = function (...args) {
  19. if (isBootstrapSkillPath(args[0])) {
  20. readCount += 1;
  21. }
  22. return originalReadFileSync.apply(this, args);
  23. };
  24. const mod = await import(pathToFileURL(pluginPath).href);
  25. const plugin = await mod.SuperpowersPlugin({ client: {}, directory: '.' });
  26. const transform = plugin['experimental.chat.messages.transform'];
  27. const firstOutput = makeOutput(`${scenario} bootstrap first step`);
  28. await transform({}, firstOutput);
  29. const afterFirst = { existsCount, readCount };
  30. const secondOutput = makeOutput(`${scenario} bootstrap second step`);
  31. await transform({}, secondOutput);
  32. const afterSecond = { existsCount, readCount };
  33. const result = {
  34. scenario,
  35. firstBootstrapParts: countBootstrapParts(firstOutput),
  36. secondBootstrapParts: countBootstrapParts(secondOutput),
  37. staleMentionMapping: bootstrapText(firstOutput).includes('@mention'),
  38. staleTaskMapping: bootstrapText(firstOutput).includes('`Task` tool with subagents'),
  39. mapsSubagentToTask: bootstrapText(firstOutput).includes('`task` with `subagent_type: "general"`'),
  40. mapsMutationToApplyPatch: bootstrapText(firstOutput).includes('`apply_patch`'),
  41. firstReadCount: afterFirst.readCount,
  42. secondReadCount: afterSecond.readCount,
  43. firstExistsCount: afterFirst.existsCount,
  44. secondExistsCount: afterSecond.existsCount,
  45. };
  46. const failures = scenario === 'present'
  47. ? assertPresentBootstrap(result)
  48. : assertMissingBootstrap(result);
  49. if (failures.length > 0) {
  50. console.error(JSON.stringify(result, null, 2));
  51. for (const failure of failures) {
  52. console.error(`FAIL: ${failure}`);
  53. }
  54. process.exit(1);
  55. }
  56. console.log(JSON.stringify(result, null, 2));
  57. function isBootstrapSkillPath(filePath) {
  58. return String(filePath).replaceAll('\\', '/').includes('using-superpowers/SKILL.md');
  59. }
  60. function makeOutput(text) {
  61. return {
  62. messages: [{
  63. info: { role: 'user' },
  64. parts: [{ type: 'text', text }],
  65. }],
  66. };
  67. }
  68. function countBootstrapParts(output) {
  69. return output.messages[0].parts.filter(
  70. (part) => part.type === 'text' && part.text.includes('EXTREMELY_IMPORTANT')
  71. ).length;
  72. }
  73. function bootstrapText(output) {
  74. return output.messages[0].parts.find(
  75. (part) => part.type === 'text' && part.text.includes('EXTREMELY_IMPORTANT')
  76. )?.text || '';
  77. }
  78. function assertPresentBootstrap(result) {
  79. const failures = [];
  80. if (result.firstBootstrapParts !== 1) {
  81. failures.push(`expected first transform to inject one bootstrap part, got ${result.firstBootstrapParts}`);
  82. }
  83. if (result.secondBootstrapParts !== 1) {
  84. failures.push(`expected second transform to inject one bootstrap part, got ${result.secondBootstrapParts}`);
  85. }
  86. if (result.firstReadCount !== 1) {
  87. failures.push(`expected first transform to read SKILL.md once, got ${result.firstReadCount}`);
  88. }
  89. if (result.secondReadCount !== result.firstReadCount) {
  90. failures.push(`expected cached second transform to do no additional reads, got ${result.secondReadCount - result.firstReadCount}`);
  91. }
  92. if (result.secondExistsCount !== result.firstExistsCount) {
  93. failures.push(`expected cached second transform to do no additional exists checks, got ${result.secondExistsCount - result.firstExistsCount}`);
  94. }
  95. if (result.staleMentionMapping) {
  96. failures.push('expected OpenCode bootstrap not to teach @mention subagent syntax');
  97. }
  98. if (result.staleTaskMapping) {
  99. failures.push('expected OpenCode bootstrap not to teach stale Task-tool mapping');
  100. }
  101. if (!result.mapsSubagentToTask) {
  102. failures.push('expected OpenCode bootstrap to map general-purpose subagents to task with subagent_type');
  103. }
  104. if (!result.mapsMutationToApplyPatch) {
  105. failures.push('expected OpenCode bootstrap to map file mutation to apply_patch');
  106. }
  107. return failures;
  108. }
  109. function assertMissingBootstrap(result) {
  110. const failures = [];
  111. if (result.firstBootstrapParts !== 0) {
  112. failures.push(`expected no bootstrap when SKILL.md is missing, got ${result.firstBootstrapParts}`);
  113. }
  114. if (result.secondBootstrapParts !== 0) {
  115. failures.push(`expected no bootstrap on second missing-file transform, got ${result.secondBootstrapParts}`);
  116. }
  117. if (result.firstReadCount !== 0 || result.secondReadCount !== 0) {
  118. failures.push(`expected missing file path to avoid reads, got ${result.secondReadCount}`);
  119. }
  120. if (result.firstExistsCount < 1) {
  121. failures.push('expected first transform to check whether SKILL.md exists');
  122. }
  123. if (result.secondExistsCount !== result.firstExistsCount) {
  124. failures.push(`expected missing-file result to be cached, got ${result.secondExistsCount - result.firstExistsCount} extra exists checks`);
  125. }
  126. return failures;
  127. }