package main // The Notion import's files and gates. const ( importStore = "internal/mapa/import_store.go" importWrite = "internal/mapa/import_write.go" importTitles = "internal/mapa/import_titles.go" importSet = "internal/mapa/import_set.go" importReport = "internal/mapa/import_report.go" importLedger = "internal/mapa/import_ledger.go" importSurvey = "internal/mapa/import_survey.go" importBook = "internal/mapa/import_book.go" importView = "internal/mapa/import_view.go" importGuard = "internal/mapa/import_handlers.go" importJob = "internal/mapa/import_job.go" importImages = "internal/mapa/import_images.go" importPlace = "internal/mapa/import_images_place.go" importCredential = "internal/mapa/import_credential.go" importRetention = "internal/mapa/import_retention.go" pagesFile = "internal/mapa/pages.go" handlersPages = "internal/mapa/handlers_pages.go" notionmdTree = "internal/mapa/notionmd/render.go" mapaPackage = "./internal/mapa/" ) // inMapa is a mutation whose gate is in package mapa. func inMapa(gate, name, test string, edits ...edit) mutation { return mutation{gate: gate, name: name, test: test, edits: edits, pkg: mapaPackage} } // importMutations break the Notion import's gates (Notion import spec // §18: I2 to I19), one at a time. I1's are the mapper's // (mutations_mapper.go). I14 waits for images. I16 has none: it can go // red only if the module gains a write, which M4 forbids. var importMutations = []mutation{ inMapa("I2", "an unchanged page committed again", "TestI2ARerun", edit{importWrite, "\tif entry.current.Title == entry.title &&", "\tif false && entry.current.Title == entry.title &&"}, edit{importStore, "\tif record.Body == body {", "\tif false {"}), // I3 removes both checks; each is also held alone, the store's by // M3's gates, the run's by M2's (round 2: the note that once said // the run's could not be held alone was wrong; a kept page's title // and a kept page's images both depend on it). inMapa("I3", "any live page read as untouched, run and store", "TestI3", edit{importTitles, "return entry.live && entry.revision == entry.place.Revision", "return entry.live"}, edit{importStore, "\t\tpage.Revision != write.expect {", "\t\tfalse {"}), inMapa("I4", "absence counted before the survey is complete", "TestI4", edit{importReport, "if !entry.place.mapped(entry.row) || !tally.view.SurveyDone {", "if !entry.place.mapped(entry.row) {"}), inMapa("I5", "a link to a page's Notion title, not its planned one", "TestI5", edit{importSet, "return entry.title, notionmd.Live", "return plainRuns(entry.staged.Title), notionmd.Live"}), inMapa("I6", "a candidate leaves no room for its suffix", "TestI6", edit{importTitles, "room := maxTitle - utf8.RuneCountInString(suffix)", "room := maxTitle + 0*utf8.RuneCountInString(suffix)"}), inMapa("H1", "the candidates bounded at 64", "TestI6Seventy", edit{importTitles, "\tfor index := skip; ; index++ {", "\tfor index := skip; index < 64; index++ {"}, edit{importTitles, "\t\t\treturn candidate, err\n\t\t}\n\t}\n}", "\t\t\treturn candidate, err\n\t\t}\n\t}\n" + "\treturn \"\", errors.New(\"bounded\")\n}"}), inMapa("L4", "a page deleted in Mapa holds its title", "TestI6ADeleted", edit{importTitles, "\t\tif entry.place.mapped(entry.row) && !entry.live {", "\t\tif false {"}), inMapa("I7", "ImportUpdate names the page's owner", "TestI7", edit{importStore, "if page, record, err = tx.importable(write); err != nil {\n" + "\t\t\treturn err\n\t\t}", "if page, record, err = tx.importable(write); err != nil {\n" + "\t\t\treturn err\n\t\t}\n\t\tactor = page.OwnerID"}), inMapa("I7", "ImportCreate names the connection's creator", "TestI7", edit{importStore, "\t\torigin := write.origin\n\t\tpage, err =", "\t\tif held, ok, _ := connection(tx.db); ok {\n" + "\t\t\tactor = held.CreatedBy\n\t\t}\n" + "\t\torigin := write.origin\n\t\tpage, err ="}), {gate: "I8", name: "an http.Get in a handler", copy: true, test: "TestI8", edits: []edit{{handlersPages, "// seeOther ends a POST that worked.", "var _ = http.Get\n\n// seeOther ends a POST that worked."}}}, {gate: "I8", name: "the module imported from notionmd", copy: true, test: "TestI8", edits: []edit{{notionmdTree, "import (\n\t\"context\"", "import (\n\t_ \"amadan.net/oficina/notion.git/notion\"\n" + "\t\"context\""}}}, {gate: "I9", name: "the staged tree written unsealed", test: "TestI9", edits: []edit{{importLedger, "return store.sealer.Seal(seal.PurposeNotionStaged, plain)", "return plain, nil"}}}, inMapa("I10", "a staged page's title logged", "TestI10", edit{importSurvey, "staged := stagedPage(page, blocks, ref.Parent, seq)", "staged := stagedPage(page, blocks, ref.Parent, seq)\n" + "\tjob.logger.Info(\"staged\", \"title\", " + "plainRuns(staged.Title))"}), {gate: "I11", name: "the token's tail shown beside the workspace", test: "TestI11", edits: []edit{{importView, "data.Workspace = app.workspaceName(row)", "token, _ := app.store.token(request.Context())\n" + "\t\tdata.Workspace = app.workspaceName(row) + " + "token[len(token)-8:]"}}}, inMapa("I12", "a resumed survey reads every page again", "TestI12ResumeContinuesTheSurvey", edit{importSurvey, "if row.SurveyedRun == job.lease.run {", "if false {"}), inMapa("I12", "a write by a run whose lease moved", "TestI12StopRefusesTheOldGoroutine", edit{importBook, "if held.RowsAffected != 1 {", "if false {"}), {gate: "I13", name: "every member let through", test: "TestI13", edits: []edit{{importGuard, "if !app.config.Importer(address) {", "if !app.config.Importer(address) && false {"}}}, inMapa("I17", "ImportCreate leaves its map row to a later write", "TestI17ACrash", edit{importStore, "return tx.placeRow(write, page, mapCreated)", "return nil"}), {gate: "I17", name: "a handler calls ImportCreate", copy: true, pkg: mapaPackage, test: "TestI17OnlyTheRunWritesImports", edits: []edit{{handlersPages, "// seeOther ends a POST that worked.", "var _ = (*Store).ImportCreate\n\n" + "// seeOther ends a POST that worked."}}}, inMapa("I18", "tx.commit keeps imported for every caller", "TestI18", edit{pagesFile, "record.Imported = record.Imported && tx.importing", "_ = tx.importing"}), inMapa("I19", "the bookkeeping fence checks no table", "TestI19BookkeepingIsFenced", edit{importBook, "if !strings.HasPrefix(table, \"notion_\") {", "if false {"}), inMapa("M3", "ImportUpdate's revision check alone removed", "TestM3ASaveMidRunIsKept", edit{importStore, "\t\tpage.Revision != write.expect {", "\t\tfalse {"}), inMapa("M3", "ImportUpdate's gone check alone removed", "TestM3ADeleteMidRunIsNotWritten", edit{importStore, "if err != nil || page.Gone || page.Kind", "if err != nil || page.Kind"}), // The deleted page is decided in early() now, before anything is // rendered (L15), so sending it to create means both: early lets it // through, and commit takes "not live" for "not mapped". inMapa("M3", "a page deleted in Mapa sent to create", "TestM3ADeletedPageIsNotRecreated", edit{importWrite, "\tcase !entry.live:\n\t\treturn mapGone\n", ""}, edit{importWrite, "\tif !entry.place.mapped(entry.row) {\n" + "\t\treturn job.create(", "\tif !entry.live {\n\t\treturn job.create("}), inMapa("M4", "ImportUpdate without the lease", "TestM4EveryRunWriteHoldsTheLease", edit{importStore, "\t\tif err := holdLease(tx.db, write.lease); err != nil {\n" + "\t\t\treturn err\n\t\t}\n" + "\t\tactor, err := tx.member(write.who)\n" + "\t\tif err != nil {\n\t\t\treturn err\n\t\t}\n" + "\t\tvar record Record", "\t\tactor, err := tx.member(write.who)\n" + "\t\tif err != nil {\n\t\t\treturn err\n\t\t}\n" + "\t\tvar record Record"}), inMapa("M4", "staging without the lease", "TestM4EveryRunWriteHoldsTheLease", edit{importSurvey, "bookkeep(ctx, job.lease, func(db *gorm.DB) error {\n" + "\t\trow.State, row.ErrorCode, row.SurveyedRun = mapStaged", "bookkeep(ctx, runLease{}, func(db *gorm.DB) error {\n" + "\t\trow.State, row.ErrorCode, row.SurveyedRun = mapStaged"}), inMapa("M4", "an outcome without the lease", "TestM4EveryRunWriteHoldsTheLease", edit{importWrite, "bookkeep(ctx, job.lease, func(db *gorm.DB) error {\n" + "\t\tif err := job.recordOutcome", "bookkeep(ctx, runLease{}, func(db *gorm.DB) error {\n" + "\t\tif err := job.recordOutcome"}), inMapa("M4", "the run's end without the lease", "TestM4EveryRunWriteHoldsTheLease", edit{importJob, "bookkeep(ctx, job.lease, func(db *gorm.DB) error {\n" + "\t\terr := db.Model(&NotionPage{})", "bookkeep(ctx, runLease{}, func(db *gorm.DB) error {\n" + "\t\terr := db.Model(&NotionPage{})"}), inMapa("M4", "a new token leaves resumable runs", "TestM4ANewTokenEndsAResumableRun", edit{importCredential, "\t\t\tif err := tx.endResumable(runDisconnected); " + "err != nil {\n\t\t\t\treturn err\n\t\t\t}\n\t\t}\n" + "\t\treturn tx.saveConnection", "\t\t}\n\t\treturn tx.saveConnection"}), inMapa("M4", "the heartbeat carries on without its lease", "TestM4TheHeartbeatStops", edit{importJob, "\t\t\t\tstop()\n\t\t\t\treturn\n", "\t\t\t\tcontinue\n"}), inMapa("M5", "no connection ever idle", "TestM5AnIdleToken", edit{importRetention, "return now.Sub(last) >= IdleTokenWindow", "return false && now.Sub(last) >= IdleTokenWindow"}), inMapa("M6", "Forget leaves the connection", "TestM6ForgetDeletes", edit{importCredential, "err = tx.db.Where(\"1 = 1\").Delete(&NotionConnection{}).Error", "err = nil"}), inMapa("L5", "a page with no planned title answered live", "TestL5", edit{importSet, "\t\tif entry.row.State == mapFailed || entry.title == \"\" ||\n" + "\t\t\t(entry.place.mapped(entry.row) && !entry.live) {\n" + "\t\t\treturn \"\", notionmd.Dead\n\t\t}\n" + "\t\treturn entry.title, notionmd.Live", "\t\tif entry.row.State == mapFailed {\n" + "\t\t\treturn \"\", notionmd.Dead\n\t\t}\n" + "\t\treturn firstOf(entry.title, " + "plainRuns(entry.staged.Title)), notionmd.Live"}), // The check's marker clause: Low3 in mutations_mapper.go. inMapa("L6", "the fix-up writes a created page as updated", "TestL6", edit{importJob, "\twrite.state = entry.row.State\n", "\n"}), inMapa("L7", "a rename collision fails the page", "TestL7", edit{importWrite, "if code != \"title_taken\" || attempt > 8 {", "if true {"}), inMapa("L8", "a failed launch leaves the run", "TestL8", edit{importJob, "\t\t_ = app.store.endRun(context.Background(), lease, " + "runFailed,\n\t\t\trunInternal)", "\t\t_ = lease"}), inMapa("L9", "a finished row decided unstaged keeps its page", "TestL9", edit{importSurvey, "\tif row.State == mapFinished {\n" + "\t\t// A finished import's page that this import cannot read", "\tif false {\n\t\t// A finished import's page " + "that this import cannot read"}), {gate: "L12", name: "an aliased net/http Get in a handler", copy: true, test: "TestI8MapaHasNoEgressOfItsOwn", edits: []edit{ {handlersPages, "\t\"net/http\"\n\t\"strconv\"", "\t\"net/http\"\n\tweb \"net/http\"\n\t\"strconv\""}, {handlersPages, "// seeOther ends a POST that worked.", "var _ = web.Get\n\n// seeOther ends a POST that worked."}}}, {gate: "L12", name: "os/exec in a handler", copy: true, test: "TestI8MapaHasNoEgressOfItsOwn", edits: []edit{ {handlersPages, "\t\"net/http\"\n\t\"strconv\"", "\t\"net/http\"\n\t\"os/exec\"\n\t\"strconv\""}, {handlersPages, "// seeOther ends a POST that worked.", "var _ = exec.Command\n\n" + "// seeOther ends a POST that worked."}}}, {gate: "L12", name: "a DNS lookup in config", copy: true, test: "TestI8MapaHasNoEgressOfItsOwn", edits: []edit{ {"internal/mapa/config.go", "func isLocal(origin string) bool {", "var _ = net.LookupHost\n\n" + "func isLocal(origin string) bool {"}}}, inMapa("L11", "History ignores imported", "TestL11", edit{"internal/mapa/import_history.go", "if err != nil || !record.Imported {", "if err != nil || true {"}), inMapa("M2", "untouched alone reads any live page as untouched", "TestM2AKeptPageKeepsItsTitleForLinks", edit{importTitles, "return entry.live && entry.revision == entry.place.Revision", "return entry.live"}), inMapa("M2", "a kept page not decided before its images", "TestL15", edit{importWrite, "\tcase !untouched(entry):\n" + "\t\tentry.place.Kept = job.keptReason(ctx, entry.place.PageID)\n" + "\t\treturn mapKept\n", ""}), inMapa("M1", "a page's render runs without its budget", "TestM1ASlowPageFailsAlone", edit{importWrite, "renderCtx, cancel := context.WithTimeout(ctx,\n" + "\t\trenderBudgetFor(entry.staged))", "renderCtx, cancel := context.WithCancel(ctx)"}), inMapa("Low4", "a page found gone stays live", "TestLowADeleteMidRunIsGoneForLaterPages", edit{importWrite, "\t\t\tentry.live = false\n", "\n"}), inMapa("Low6", "a page refused for being gone reported kept", "TestLowADeleteMidRunIsGoneForLaterPages", edit{importWrite, "\t\t\tentry.live = false\n\t\t\treturn mapGone, \"\", nil", "\t\t\tentry.live = false\n\t\t\treturn mapKept, \"\", nil"}), inMapa("Low6", "Forget leaves resumable runs", "TestLowForgetEndsAResumableRun", edit{importCredential, "\t\tif err := tx.endResumable(runForgotten); err != nil {", "\t\tif err := error(nil); err != nil {"}), inMapa("Low6", "idle ignores runs", "TestLowARecentRunKeepsTheToken", edit{importRetention, "if run, found, err := latestRun(database); err == nil && found {", "if run, found, err := latestRun(database); err == nil && " + "found && false {"}), inMapa("I14", "ImportCreate commits with attachNothing", "TestI14AnImageIsAddedAndAttached", edit{importStore, "actor, VerbCreated, attachOwn(0))", "actor, VerbCreated, attachNothing)"}), inMapa("I14", "ImportUpdate's edited revision attaches nothing", "TestI14AnImageGoneOrChangedIsFetchedAgain", edit{importStore, "VerbEdited, attachOwn(0))", "VerbEdited, attachNothing)"}), inMapa("I14", "reuse skips the Held check", "TestI14AnImageGoneOrChangedIsFetchedAgain", edit{importImages, "job.images.Held(ctx, prior.PublicID, entry.place.PageID) {", "true {"}), inMapa("I14", "reuse ignores the file's key", "TestI14AnImageGoneOrChangedIsFetchedAgain", edit{importImages, "if found && sameFile(prior.Key, placed.key) {", "if found {"}), // What comparing the minute-rounded edit time did, for a file // replaced in that minute (§25 note 17). inMapa("I14", "reuse ignores the file's key, the same minute", "TestI14AnImageReplacedInTheSameMinuteIsFetched", edit{importImages, "if found && sameFile(prior.Key, placed.key) {", "if found {"}), inMapa("I14", "a permanent refusal outlives its file", "TestI14ARefusalIsALineAndRemembered", edit{importImages, "if found && sameFile(prior.Key, placed.key) {", "if found {"}), inMapa("I14", "the key kept is the staged file's", "TestI14AFileReplacedMidRunIsKeyedAsDownloaded", edit{importImages, "\t\tplaced.key = fetched\n", "\t\t_ = fetched\n"}), // Fable M1: the fake signs every read afresh, as Notion does, so a // key that keeps the signature fetches every image on every run. inMapa("I14", "the file key is the signed URL", "TestI14AnImageIsAddedAndAttached", edit{importImages, "key: notion.FileKey(block.File.URL),", "key: block.File.URL,"}, edit{importImages, "\tkey := notion.FileKey(fresh.File.URL)\n", "\tkey := fresh.File.URL\n"}), inMapa("I14", "an empty file key matches", "TestAnEmptyFileKeyMatchesNothing", edit{importImages, "return kept != \"\" && kept == now", "return kept == now"}), inMapa("I2", "an unchanged Notion edit time read as an unchanged page", "TestI2AnEditInTheSameMinuteIsPickedUp", edit{importWrite, "\tif entry.current.Title == entry.title &&\n", "\tif entry.current.Origin != nil &&\n" + "\t\tentry.current.Origin.Edited == entry.staged.Edited ||\n" + "\t\tentry.current.Title == entry.title &&\n"}), inMapa("I14", "no copy is tried", "TestI14ABlockOnASecondPageIsCopied", edit{importImages, "if added, ok := job.copyImage(ctx, entry, block); ok {", "if added, ok := job.copyImage(ctx, entry, block); ok && false {"}), inMapa("I14", "a permanent refusal not remembered", "TestI14ARefusalIsALineAndRemembered", edit{ "internal/mapa/import_images_place.go", "\tcase placed.permanent:", "\tcase false:"}), inMapa("I14", "a quota refusal does not stop downloads", "TestI14TheFirstQuotaRefusalStopsDownloads", edit{importImages, "\t\tjob.imagesStopped = quotaReason(err)\n" + "\t\treturn refusal{text: job.imagesStopped}", "\t\treturn refusal{text: quotaReason(err)}"}), inMapa("I14", "a kept page's images left pending", "TestI14ANotWrittenPagesImagesGoAtOnce", edit{importWrite, "if state == mapKept || state == mapFailed || state == mapGone {", "if state == mapFailed || state == mapGone {"}), inMapa("Low5", "a deleted page's images left pending", "TestI14ANotWrittenPagesImagesGoAtOnce", edit{importWrite, "if state == mapKept || state == mapFailed || state == mapGone {", "if state == mapKept || state == mapFailed {"}), inMapa("Low5", "the fix-up renders with no image lines", "TestLow5TheFixUpKeepsImagesAfterAResume", edit{importJob, "\t\timages = storedImages(entry)", "\t\timages = &pageImages{}"}), inMapa("R3M2", "a copy ignores the file's key", "TestI14AChangedSharedImageIsFetchedOnce", edit{importPlace, "other.Page == 0 || !sameFile(other.Key, key) ||", "other.Page == 0 || !sameFile(key, key) ||"}), inMapa("R3M3", "Forget leaves notion_images", "TestM6ForgetDeletes", edit{importCredential, "err = tx.db.Where(\"1 = 1\").Delete(&NotionImage{}).Error", "err = nil"}), inMapa("R3L1", "placeRow keeps no image places", "TestL1ACrashAfterTheWriteKeepsImagePlaces", edit{importStore, "return keepPlaces(tx.db, tx.store, page.ID, write.images)", "return nil"}), inMapa("R3L2", "a block twice on a page is two images", "TestL2ABlockTwiceOnAPageIsOneImage", edit{importImages, "if _, done := prepared.lines[block.ID]; done {", "if _, done := prepared.lines[block.ID]; done && false {"}), inMapa("R3L3", "a kept page carries its old image counts", "TestL3TheStopIsThisRuns", edit{importWrite, "\t\tentry.place.Images = nil\n", "\n"}), inMapa("R3L5", "a refused host remembered as permanent", "TestL5ARefusedHostIsTriedAgain", edit{importImages, "permanent: notion.CodeOf(err) == notion.CodeTooLarge}", "permanent: true}"}), {gate: "R3L6", name: "net.Dial in the images bucket", copy: true, test: "TestI8MapaHasNoEgressOfItsOwn", edits: []edit{ {"internal/mapa/images_bucket.go", "\t\"net/http\"\n", "\t\"net\"\n\t\"net/http\"\n"}, {"internal/mapa/images_bucket.go", "// bucket is CARLOS's bucket as Mapa writes to it", "var _ = net.Dial\n\n" + "// bucket is CARLOS's bucket as Mapa writes to it"}}}, inMapa("R3L7", "a copy from a page that no longer holds it", "TestL7ACopyNeedsAHeldSource", edit{importPlace, "!job.images.Held(ctx, other.PublicID, other.Page) {", "false {"}), inMapa("R3L7", "no lease check before a copy", "TestL7BothImageWritesHoldTheLease", edit{importImages, "\tif err := job.store.bookkeep(ctx, job.lease, noChange); " + "err != nil {\n\t\treturn nil, \"\", refusal{}, err\n\t}\n" + "\tif added, ok := job.copyImage", "\tif added, ok := job.copyImage"}), inMapa("R3L7", "no lease check before an add", "TestL7BothImageWritesHoldTheLease", edit{importImages, "\tif err := job.store.bookkeep(ctx, job.lease, noChange); " + "err != nil {\n\t\treturn nil, \"\", refusal{}, err\n\t}\n" + "\tadded, err := job.images.Add", "\tadded, err := job.images.Add"}), inMapa("R3L7", "quotaReason always names the person's", "TestL7TheTeamAllowanceIsNamed", edit{ "internal/mapa/import_images_store.go", "if errors.As(err, &refusal) && refusal.team {", "if errors.As(err, &refusal) && refusal.team && false {"}), inMapa("R4L3", "Finish deletes where the images went", "TestFinishKeepsTheMap", edit{importCredential, "\t\tif err := tx.endResumable(runFinished); err != nil {", "\t\ttx.db.Where(\"1 = 1\").Delete(&NotionImage{})\n" + "\t\tif err := tx.endResumable(runFinished); err != nil {"}), }