file-watch.json 2.6 KB

123456789101112131415161718192021222324252627282930
  1. {
  2. "name": "verify-file-watch",
  3. "settings": {
  4. "storagePermissions": {
  5. "collections": { "watch_cursors": "read-write" }
  6. }
  7. },
  8. "nodes": [
  9. {"id": "n1", "name": "Trigger", "type": "click-trigger", "position": {"x": 0, "y": 0}, "config": {}},
  10. {"id": "setup", "name": "Setup", "type": "code", "position": {"x": 0, "y": 100},
  11. "config": {"code": "const dir = '/tmp/sb-file-watch-test';\nconst listing = smartbotic.fs.readdir(dir);\nif (listing.success) {\n for (const e of listing.entries) { smartbotic.fs.unlink(e.path); }\n}\nsmartbotic.fs.mkdir(dir);\nsmartbotic.fs.writeFile(dir + '/existing.txt', smartbotic.utils.base64Encode('old'));\nsmartbotic.storage.delete('watch_cursors', context.workflowId + ':first');\nsmartbotic.storage.delete('watch_cursors', context.workflowId + ':second');\nconst before = smartbotic.fs.readdir(dir);\nconst seen = {};\nfor (const e of before.entries) { seen[e.name] = e.modifiedAt + ':' + e.size; }\nsmartbotic.storage.insert('watch_cursors', { seen: seen, updatedAt: Date.now(), directory: dir }, context.workflowId + ':second');\nconst st = smartbotic.fs.stat(dir + '/existing.txt');\nconst statMtimeSane = st.mtime > 1600000000000 && st.mtime < Date.now() + 60000;\nreturn { dir: dir, statMtimeSane: statMtimeSane };"}},
  12. {"id": "first", "name": "First Run", "type": "file-watch", "position": {"x": 0, "y": 200},
  13. "config": {"directory": "/tmp/sb-file-watch-test", "emitOnFirstRun": false}},
  14. {"id": "add", "name": "Add A File", "type": "code", "position": {"x": 0, "y": 300},
  15. "config": {"code": "smartbotic.fs.writeFile('/tmp/sb-file-watch-test/fresh.txt', smartbotic.utils.base64Encode('new'));\nreturn { added: 'fresh.txt' };"}},
  16. {"id": "second", "name": "Second Run", "type": "file-watch", "position": {"x": 0, "y": 400},
  17. "config": {"directory": "/tmp/sb-file-watch-test", "emitOnFirstRun": false}}
  18. ],
  19. "connections": [
  20. {"sourceNodeId": "n1", "sourceOutput": "main", "targetNodeId": "setup", "targetInput": "data"},
  21. {"sourceNodeId": "setup", "sourceOutput": "main", "targetNodeId": "first", "targetInput": "data"},
  22. {"sourceNodeId": "first", "sourceOutput": "main", "targetNodeId": "add", "targetInput": "data"},
  23. {"sourceNodeId": "add", "sourceOutput": "main", "targetNodeId": "second", "targetInput": "data"}
  24. ],
  25. "expect": {
  26. "setup": {"status": "completed", "output": {"result": {"statMtimeSane": true}}},
  27. "first": {"status": "completed", "output": {"count": 0, "isFirstRun": true}},
  28. "second": {"status": "completed", "output": {"count": 1, "isFirstRun": false, "files": [{"name": "fresh.txt"}]}}
  29. }
  30. }