test-bootstrap-caching.mjs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. firstReadCount: afterFirst.readCount,
  38. secondReadCount: afterSecond.readCount,
  39. firstExistsCount: afterFirst.existsCount,
  40. secondExistsCount: afterSecond.existsCount,
  41. };
  42. const failures = scenario === 'present'
  43. ? assertPresentBootstrap(result)
  44. : assertMissingBootstrap(result);
  45. if (failures.length > 0) {
  46. console.error(JSON.stringify(result, null, 2));
  47. for (const failure of failures) {
  48. console.error(`FAIL: ${failure}`);
  49. }
  50. process.exit(1);
  51. }
  52. console.log(JSON.stringify(result, null, 2));
  53. function isBootstrapSkillPath(filePath) {
  54. return String(filePath).replaceAll('\\', '/').includes('using-superpowers/SKILL.md');
  55. }
  56. function makeOutput(text) {
  57. return {
  58. messages: [{
  59. info: { role: 'user' },
  60. parts: [{ type: 'text', text }],
  61. }],
  62. };
  63. }
  64. function countBootstrapParts(output) {
  65. return output.messages[0].parts.filter(
  66. (part) => part.type === 'text' && part.text.includes('EXTREMELY_IMPORTANT')
  67. ).length;
  68. }
  69. function assertPresentBootstrap(result) {
  70. const failures = [];
  71. if (result.firstBootstrapParts !== 1) {
  72. failures.push(`expected first transform to inject one bootstrap part, got ${result.firstBootstrapParts}`);
  73. }
  74. if (result.secondBootstrapParts !== 1) {
  75. failures.push(`expected second transform to inject one bootstrap part, got ${result.secondBootstrapParts}`);
  76. }
  77. if (result.firstReadCount !== 1) {
  78. failures.push(`expected first transform to read SKILL.md once, got ${result.firstReadCount}`);
  79. }
  80. if (result.secondReadCount !== result.firstReadCount) {
  81. failures.push(`expected cached second transform to do no additional reads, got ${result.secondReadCount - result.firstReadCount}`);
  82. }
  83. if (result.secondExistsCount !== result.firstExistsCount) {
  84. failures.push(`expected cached second transform to do no additional exists checks, got ${result.secondExistsCount - result.firstExistsCount}`);
  85. }
  86. return failures;
  87. }
  88. function assertMissingBootstrap(result) {
  89. const failures = [];
  90. if (result.firstBootstrapParts !== 0) {
  91. failures.push(`expected no bootstrap when SKILL.md is missing, got ${result.firstBootstrapParts}`);
  92. }
  93. if (result.secondBootstrapParts !== 0) {
  94. failures.push(`expected no bootstrap on second missing-file transform, got ${result.secondBootstrapParts}`);
  95. }
  96. if (result.firstReadCount !== 0 || result.secondReadCount !== 0) {
  97. failures.push(`expected missing file path to avoid reads, got ${result.secondReadCount}`);
  98. }
  99. if (result.firstExistsCount < 1) {
  100. failures.push('expected first transform to check whether SKILL.md exists');
  101. }
  102. if (result.secondExistsCount !== result.firstExistsCount) {
  103. failures.push(`expected missing-file result to be cached, got ${result.secondExistsCount - result.firstExistsCount} extra exists checks`);
  104. }
  105. return failures;
  106. }